Cloud Intermediate
AWS S3 + CloudFront¶
AWSS3CloudFront 3 min read
Static hosting with S3 and CDN distribution via CloudFront.
S3 Bucket¶
resource "aws_s3_bucket" "site" {
bucket = "my-website-bucket"
}
resource "aws_s3_bucket_website_configuration" "site" {
bucket = aws_s3_bucket.site.id
index_document { suffix = "index.html" }
error_document { key = "404.html" }
}
CloudFront¶
resource "aws_cloudfront_distribution" "cdn" {
origin {
domain_name = aws_s3_bucket.site.bucket_regional_domain_name
origin_id = "S3"
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.oai.cloudfront_access_identity_path
}
}
enabled = true
default_root_object = "index.html"
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3"
viewer_protocol_policy = "redirect-to-https"
compress = true
}
}
Summary¶
S3 + CloudFront = cheap, fast, and scalable hosting for static sites and SPAs.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.