This task focuses on deploying a static website using AWS S3, CloudFront, and Route 53 with Terraform for infrastructure as code (IaC). The setup includes domain configuration via Route 53, enabling HTTPS with an AWS Certificate Manager (ACM) SSL certificate, and leveraging CloudFront as a CDN for performance and security.
Create a project directory:
mkdir static-website-terraform && cd static-website-terraform
Create Terraform files:
touch main.tf variables.tf outputs.tf
provider "aws" {
region = "us-east-1" # ACM must be in us-east-1 for CloudFront
}
resource "aws_s3_bucket" "static_site" {
bucket = var.s3_bucket_name
}
resource "aws_s3_bucket_policy" "allow_public_read" {
bucket = aws_s3_bucket.static_site.id
policy = <
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = aws_s3_bucket.static_site.bucket_regional_domain_name
origin_id = "S3-${var.s3_bucket_name}"
}
enabled = true
default_root_object = "index.html"
default_cache_behavior {
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-${var.s3_bucket_name}"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.cert.arn
ssl_support_method = "sni-only"
}
}
resource "aws_acm_certificate" "cert" {
domain_name = var.domain_name
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "www" {
zone_id = var.hosted_zone_id
name = var.domain_name
type = "A"
alias {
name = aws_cloudfront_distribution.s3_distribution.domain_name
zone_id = aws_cloudfront_distribution.s3_distribution.hosted_zone_id
evaluate_target_health = false
}
}
variables.tf
variable "s3_bucket_name" {
description = "The name of the S3 bucket"
type = string
}
variable "domain_name" {
description = "The domain name for the website"
type = string
}
variable "hosted_zone_id" {
description = "The Route 53 hosted zone ID"
type = string
}
outputs.tf
output "s3_bucket_name" {
value = aws_s3_bucket.static_site.bucket
}
output "cloudfront_domain" {
value = aws_cloudfront_distribution.s3_distribution.domain_name
}
output "website_url" {
value = "https://${var.domain_name}"
}
Run the following commands:
terraform init
terraform validate
terraform plan -out=tfplan
terraform apply tfplan
After Terraform creates the infrastructure, upload website files:
aws s3 cp index.html s3://your-bucket-name/
aws s3 cp style.css s3://your-bucket-name/
https://yourdomain.com
This project demonstrates how to deploy a static website efficiently using AWS S3, CloudFront, and Route 53 with Terraform. It provides a scalable, secure, and cost-effective hosting solution.