Table of contents
📍Introduction
In this Blog, we'll walk through the process of hosting a static website on Amazon S3 using Terraform. Amazon S3 (Simple Storage Service) is a scalable and cost-effective storage solution offered by Amazon Web Services (AWS), and Terraform is an infrastructure-as-code tool that enables you to define and manage your infrastructure in a declarative manner. By the end of this tutorial, you'll have a solid understanding of how to set up and deploy a static website on S3 using Terraform.
📝Prerequisites
An AWS account with the necessary permissions to create S3 buckets and IAM roles.
Terraform installed on your local machine. You can download it from the official Terraform website.
Basic familiarity with AWS services and concepts.
✔Steps
Step 1: Create a new directory for your project.
Step 2: Inside the project directory, create a file named providers.tf to configure the specific cloud providers or services that your Terraform configuration will interact with.
Step 3: run terraform init
in terminal. It initializes your Terraform configuration, sets up the environment, and prepares your working directory.
Step 4: Create a main.tf file. Here you define the resources you want to create or manage using Terraform.
for bucket name, create a variables.tf and call it in main.tf.
Step 5: Resource: aws_s3_bucket_ownership_controls.
it is used to define and manage ownership controls for an Amazon S3 bucket,
this ensures everything in the bucket is owned by us.
Step 6: Resource: aws_s3_bucket_public_access_block.
allows you to enforce certain rules on an S3 bucket to control its public access.
setting all to false to make bucket public.
Step 7: Resource: aws_s3_bucket_acl.
The purpose of an S3 bucket ACL is to define who has access to the objects in the bucket and what level of access they have. ACLs can be used to control both public and private access to the objects. So, we have to make acl public-read.
Step 8: If we do terraform apply
now, we can see that the bucket is accessible to public.
Step 9: Now to enable Static Website Hosting first we need to put index.html and error.html in S3 bucket using "aws_s3_bucket_object" because both these files are needed in "aws_s3_bucket_website_configuration".
run terraform apply
to add resources to bucket.
now we are all set and ready to configure our website.
Step 10: Resource: aws_s3_bucket_website_configuration
to host static web content that can be accessed via a web browser.
depends_on = [ aws_s3_bucket_acl.example ] means the website will only host if the acl is set to public-read.
Step 11: Create output.tf to get website endpoint in the terminal itself.
Step 12: run terraform apply
and go to your bucket's properties to find the URL to your website.
Step 13: Click on the link to see the website.
The website is up and running.
-> You can find the code for this project on my Github.
Github: https://github.com/Chekit06
LinkedIn: https://www.linkedin.com/in/chekitsharma/
📍Conclusion
In this blog, we have successfully hosted a static portfolio website on Amazon S3 through the use of Terraform. Alongside this achievement, we've explored vital Terraform resources essential for effective website hosting. Also gained valuable insights into harnessing Terraform's capabilities for seamless infrastructure management.