Skip to content

Instantly share code, notes, and snippets.

@uurtech
Created February 1, 2024 13:25
Show Gist options
  • Save uurtech/20e71265632f13119be2bdbc709b831a to your computer and use it in GitHub Desktop.
Save uurtech/20e71265632f13119be2bdbc709b831a to your computer and use it in GitHub Desktop.
S3 - Cloudfront test
provider "aws" {
region = "us-east-1" # Update with your desired AWS region
}
# Create an S3 bucket for storing website files
resource "aws_s3_bucket" "website_bucket" {
bucket = "your-unique-bucket-name" # Update with your desired bucket name
website {
index_document = "index.html"
error_document = "error.html"
}
}
# Set up Route 53 for managing domain and DNS routing
resource "aws_route53_zone" "main" {
name = "example.com" # Update with your domain name
}
resource "aws_route53_record" "website_record" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com" # Update with your domain name
type = "A"
alias {
name = aws_cloudfront_distribution.web_distribution.domain_name
zone_id = aws_cloudfront_distribution.web_distribution.hosted_zone_id
evaluate_target_health = true
}
}
# Create a CloudFront distribution for secure content delivery
resource "aws_cloudfront_distribution" "web_distribution" {
origin {
domain_name = aws_s3_bucket.website_bucket.bucket_regional_domain_name
origin_id = aws_s3_bucket.website_bucket.arn
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path
}
}
enabled = true
default_root_object = "index.html"
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD", "OPTIONS"]
target_origin_id = aws_s3_bucket.website_bucket.arn
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
viewer_certificate {
cloudfront_default_certificate = true
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
}
# Create an origin access identity for CloudFront
resource "aws_cloudfront_origin_access_identity" "origin_access_identity" {
comment = "Origin access identity for S3 bucket"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment