In this step-by-step guide, we’ll dive into deploying a static website on AWS S3 using Terraform! We’ll
walk through the process of:
- Automating S3 Bucket Creation: Terraform will handle creating the S3 bucket where your website files will reside.
- Effortless Website Upload: We’ll configure Terraform to skip manual uploads by referencing your website files locally.
- Public Access for All: Terraform will configure the S3 bucket policy to grant public read access, ensuring anyone can access your website.
- Enabling Web Hosting: Terraform will transform your S3 bucket into a fully functional static website, ready to serve your content to the world. By the end, you’ll have a Terraform script that automates the entire deployment process, saving you time and ensuring a secure and accessible website. Step 1: Setup Terraform
- Create a
terraform.tf
file to set up the Terraform and provider: terraform { required_version = "1.7.4" required_providers { aws = { source = "hashicorp/aws" version = "5.40.0" } } } provider "aws" { profile = "default" region = "ap-south-1" } Terraform Configuration Block: terraform { ... }: This block defines the Terraform configuration itself. required_version = "1.7.4": Specifies the minimum Terraform version required to run this configuration. AWS Provider Block: profile = "default": Uses the default AWS profile configured in your local AWS credentials. region = "ap-south-1": Specifies the AWS region where your infrastructure will be deployed. Step 2: Configuration for S3 Bucket: - Create a
bucket.tf
file to store the Terraform configuration related to the S3 bucket: resource "aws_s3_bucket" "terraform_demo" { bucket = "terraform-demo-43234" } resource "aws_s3_object" "terraform_index" { bucket = aws_s3_bucket.terraform_demo.id key = "index.html" source = "index.html" content_type = "text/html" etag = filemd5("index.html") } resource "aws_s3_bucket_website_configuration" "terraform_hosting" { bucket = aws_s3_bucket.terraform_demo.id index_document { suffix = "index.html" } } Step 3: Configuration for Bucket Policy: - Create a
policy.tf
file to store the Terraform configuration related to the bucket policy for public access: # S3 public access resource "aws_s3_bucket_public_access_block" "terraform_demo" { bucket = aws_s3_bucket.terraform_demo.id block_public_acls = false block_public_policy = false } resource "aws_s3_bucket_policy" "open_access" { bucket = aws_s3_bucket.terraform_demo.id policy = jsonencode({ Version = "2012-10-17" Id = "Public_access" Statement = [ { Sid = "IPAllow" Effect = "Allow" Principal = "" Action = ["s3:GetObject"] Resource = "${aws_s3_bucket.terraform_demo.arn}/" }, ] }) depends_on = [aws_s3_bucket_public_access_block.terraform_demo] } Step 4: Configuration for Output Variable -
Create an
output.tf
file to print out the URL to access the website: # Website URL output "website_url" { value = "http://${aws_s3_bucket.terraform_demo.bucket}.s3- website.${aws_s3_bucket.terraform_demo.region}.amazonaws.com" }. Step 5: Initialize Terraform - Open the command prompt or terminal, navigate to the folder where the Terraform file is located, and run the below command: terraform init
- Prepares Terraform’s working directory for managing infrastructure. Step 6: Terraform Validate
- Run the below command to validate the Terraform configuration: terraform validate
- Performs a static analysis of your Terraform configuration files. Step 7: Terraform Plan
- Run the below command to review the intended changes to the infrastructure terraform plan
- Used for understanding and reviewing the intended changes to your infrastructure. Step 8: Terraform Apply
- Run the below command to execute the changes terraform apply
- Executes the actions outlined in the plan generated by the Terraform plan. Step 9: Destroy:
- Run the below command to tear down the infrastructure resources terraform destroy
- The terraform destroy command tears down the infrastructure resources that your Terraform configuration currently manages. Conclusion: In this comprehensive guide, we have walked through the process of deploying a static website on AWS S3 using Terraform. By following the step-by-step instructions outlined above, you can automate the creation of S3 buckets, effortlessly upload your website files, configure public access policies, and enable web hosting with ease
Top comments (1)
This looks like you've pasted aws.plainenglish.io/host-your-stat... into the markdown editor here. Was that the case?