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] 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 Add region to the copied code. Initialize the terraform by running terraform init 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. Apply the variable in your main.tf file Apply the changes by running terraform apply Define the ownership of the bucket by adding the code here to your main.tf file. Replace example with resource name. 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. Change bucket acl to public. Copy the code under public-read ACL. Run terraform apply command to apply the changes. 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. Next you need to upload two html files as object into your S3 bucket. To do this, make use of s3 object Run terraform apply to upload the objects into your bucket. Configure website Use the code here to configure your website Apply the changes by running terraform apply command Confirm that the static website has been created by navigating to your s3 bucket in AWS console and select properties tab. Scroll down to Static website hosting section and select Bucket website endpoint Opening the endpoint will open a new tab for your website, in my case it is resume template. 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 } Thanks for staying till the end

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]
- 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
- Add region to the copied code.
- Initialize the terraform by running
terraform init
- 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.
- Apply the variable in your main.tf file
- Apply the changes by running
terraform apply
- Define the ownership of the bucket by adding the code here to your main.tf file.
- Replace example with resource name.
- 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.
- Change bucket acl to public. Copy the code under public-read ACL.
- Run terraform apply command to apply the changes.
- 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.
- Next you need to upload two html files as object into your S3 bucket. To do this, make use of s3 object
- Run terraform apply to upload the objects into your bucket.
Configure website
- Use the code here to configure your website
- Apply the changes by running terraform apply command
- Confirm that the static website has been created by navigating to your s3 bucket in AWS console and select properties tab.
- Scroll down to Static website hosting section and select Bucket website endpoint
- Opening the endpoint will open a new tab for your website, in my case it is resume template.
- 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
}
Thanks for staying till the end