DEV Community

Cover image for Creating a Public S3 Bucket with Static Website Hosting Using Terraform
Olalekan Oladiran
Olalekan Oladiran

Posted on

Creating a Public S3 Bucket with Static Website Hosting Using Terraform

Requirements

  • AWS account
  • AWS CLI
  • Terraform
  • VS Code

Create S3 Bucket

  • Head over to your VS Code and create a folder for your project by running the command:

mkdir [folder-name]

  • Cd into the folder: cd [folder-name]

Image description

  • Create a provider by creating a provider.tf file which will contain your provider details. You can get your provider details here
  • Click use provider and copy the code into your provider.tf file Image description
  • Add region to the copied code. Image description
  • Initialize the terraform by running terraform init

Image description

  • Create another file named main.tf to house your S3 bucket code.
  • Get the code for creating S3 bucket from Terraform official documentation here
  • Change the resource name of the bucket, remove tags, to avoid hard-coding, use variable to add bucket name by creating variables.tf file and add bucket name to it. Image description
  • Apply the variable in your main.tf file Image description
  • Apply the changes by running terraform apply

Image description

  • Define the ownership of the bucket by adding the code here to your main.tf file.
  • Replace example with resource name. Image description
  • We need to make the bucket public by using public access block. Replace example by resource name of your bucket and change all the boolean values to false. Image description
  • Change bucket acl to public. Copy the code under public-read ACL. Image description
  • Run terraform apply command to apply the changes. Image description
  • Enable static website hosting by using website configuration resource. Creating this requires creating index_document (index.html file) and error_document (error.html). You can generate both index.html and error.html code using chatgpt for testing purpose. Image description
  • Next you need to upload two html files as object into your S3 bucket. To do this, make use of s3 object

Image description

  • Run terraform apply to upload the objects into your bucket. Image description

Configure website

  • Use the code here to configure your website Image description
  • Apply the changes by running terraform apply command Image description
  • Confirm that the static website has been created by navigating to your s3 bucket in AWS console and select properties tab. Image description
  • Scroll down to Static website hosting section and select Bucket website endpoint Image description
  • Opening the endpoint will open a new tab for your website, in my case it is resume template. Image description
  • You can output the endpoint on your terminal by creating outputs.tf file.
output "website_endpoint" {
  description = "The website endpoint"
  value       = aws_s3_bucket_website_configuration.website.website_endpoint
}
Enter fullscreen mode Exit fullscreen mode

Image description

Thanks for staying till the end

Top comments (0)