Logo

dev-resources.site

for different kinds of informations.

Building an S3 Static Website with CloudFront Using Terraform

Published at
12/22/2024
Categories
s3
cloudfront
terraform
ssl
Author
jajera
Categories
4 categories in total
s3
open
cloudfront
open
terraform
open
ssl
open
Author
6 person written this
jajera
open
Building an S3 Static Website with CloudFront Using Terraform

πŸš€ Building an S3 Static Website with CloudFront Using Terraform

Static websites are powerful and cost-effective for hosting content. By integrating Amazon S3 and CloudFront, you can deliver static assets globally, backed by scalability and reliability. This article will guide you through creating an S3 static website and integrating it with CloudFront using the tf-aws-s3-static-website module.


🌟 Why Use CloudFront with S3?

Amazon CloudFront enhances S3 static websites by:

  • HTTPS Support: Secure content delivery with SSL/TLS.
  • Global Distribution: Reduced latency via edge locations worldwide.
  • Custom Domains: Seamlessly integrate with Route 53 for personalized branding.
  • Enhanced Caching: Improve performance and reduce costs.

Note: Ensure your Route 53 domain has been validated to avoid issues with ACM certificate validation.


πŸ”§ Prerequisites

  • AWS Account: Access to AWS services.
  • Terraform Installed: Version 1.0+ is recommended.
  • AWS CLI Configured: With appropriate IAM permissions.

πŸ“ Repository Overview

The tf-aws-s3-static-website module automates the creation of an S3 bucket configured for static website hosting and integrates CloudFront for enhanced performance. This module is authored by me, and you can find more detailed documentation and examples on the Terraform Registry.

Key Features

  • Creates an S3 bucket with website hosting.
  • Integrates with CloudFront for CDN and HTTPS support.
  • Configures Route 53 for custom domain records.
  • Supports optional versioning, logging, and custom tagging.

πŸ› οΈ Setup and Configuration

Step 1: Clone the Repository

git clone https://github.com/jdevto/tf-aws-s3-static-website.git
cd tf-aws-s3-static-website
Enter fullscreen mode Exit fullscreen mode

This repository contains the Terraform module for setting up an S3 static website with CloudFront integration. Follow the instructions below to configure and deploy your static site.

Step 2: Configure Variables

Modify the main.tf file. Here's a minimal working example integrating S3, CloudFront, and Route 53. Ensure to replace cdn_config.domain.name with a valid domain name you own. If you don't have a valid domain, set cdn_config.enable to false to skip CloudFront configuration:

provider "aws" {
  region = "ap-southeast-2"
}

resource "random_string" "suffix" {
  length  = 8
  special = false
  upper   = false
}

module "s3_static_website" {
  source = "tfstack/s3-static-website/aws"

  s3_config = {
    bucket_name          = "s3-static-site"
    bucket_acl           = "public-read"
    bucket_suffix        = random_string.suffix.result
    enable_force_destroy = true
    object_ownership     = "BucketOwnerPreferred"
    enable_versioning    = true
    index_document       = "index.html"
    error_document       = "error.html"
    public_access = {
      block_public_acls       = false
      block_public_policy     = false
      ignore_public_acls      = false
      restrict_public_buckets = false
    }
    source_file_path = "${path.module}/external"
  }

  cdn_config = {
    enable = true
    domain = {
      name     = "example.com"
      sub_name = "web"
    }
  }

  logging_config = {
    enable = true
  }

  tags = {
    Name = "s3-static-site-${random_string.suffix.result}"
  }
}

output "cloudfront_website_url" {
  value = module.s3_static_website.cloudfront_website_url
}

output "website_url" {
  value = module.s3_static_website.website_url
}

output "s3_website_url" {
  value = module.s3_static_website.s3_website_url
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize Terraform

terraform init
Enter fullscreen mode Exit fullscreen mode

Step 4: Apply the Configuration

terraform apply
Enter fullscreen mode Exit fullscreen mode

Review the plan, confirm, and let Terraform deploy your resources.

Step 5: Access Your Website

Terraform will output:

  • cloudfront_website_url: URL served via CloudFront with HTTPS.
  • website_url: Dynamic URL based on Route 53 domain.
  • s3_website_url: Direct S3 website URL (HTTP-only).

🎯 Key Points to Remember

  1. Route 53 Validation:

    • Ensure the Route 53 domain and subdomain have been validated to avoid ACM certificate validation errors.
    • Use terraform output to confirm domain settings.
  2. HTTPS Support:

    • S3 natively supports only HTTP. CloudFront adds HTTPS with ACM certificates.
  3. Access Configuration:

    • Use appropriate access settings (public-access and bucket_acl) based on your use case.

πŸ’‘ Conclusion

With Terraform and the tf-aws-s3-static-website module, setting up a globally distributed, secure static website has never been easier. By integrating CloudFront, you enhance your website's performance, security, and scalability.

Check out the repository, try it yourself, and let us know your thoughts in the comments! πŸš€

cloudfront Article's
30 articles in total
Favicon
Lee esto antes de implementar S3 y CloudFront usando Terraform.
Favicon
Lee esto antes de implementar S3 y CloudFront usando Terraform.
Favicon
Building an S3 Static Website with CloudFront Using Terraform
Favicon
AWS CloudFront: A Comprehensive Guide
Favicon
Configuring AWS WAF, CloudFront, and S3 Bucket for Secure Access
Favicon
Setting Up Custom Domain for API Gateway & CloudFront
Favicon
Apply SSL Certificate on AWS ACM (also Cloudflare)
Favicon
How to Fix Next.js CloudFront Permission Denied: Complete Guide to Static Export URL Issues
Favicon
Building Testable CloudFront Functions with TypeScript
Favicon
CloudFront and S3: SignatureDoesNotMatch , the request signature we calculated does not match the signature you provided
Favicon
Mastering Custom Responses in Amazon CloudFront to Block Behavior β€” Default(*)
Favicon
Unlock CloudFront's New Logging Potential with Athena Partition Projection
Favicon
Discovering the Latest Features of AWS CloudFront: Enhancing Performance andΒ Security
Favicon
Create an Asset Store with a Custom Domain using AWS CDK, Route53, S3 and CloudFront
Favicon
New Feature: Amazon CloudFront no longer charges (No Billing) for requests blocked by AWS WAF
Favicon
Reduce the amount of code in AWS CDK: Apply OAC in Amazon CloudFront L2 constructs
Favicon
Amazon Web Services (AWS) has announced today a new edge location in #Qatar
Favicon
Hosting a Static Website On S3 bucket With CloudFront.
Favicon
Setting up AWS S3 and CloudFront with Signed URLs using CDK
Favicon
Leveraging Lambda@Edge to seamlessly manage frontend maintenance window like a pro
Favicon
How to Resolve 403 Access Issues When Deploying a SPA on AWS with S3 and CloudFront
Favicon
Cloud Resume Challenge: Hosting a React CV using S3, CloudFront, Route 53, Lambda, DynamoDB and GitHub actions for CI/CD
Favicon
Deploying Static Website to AWS: A Step-by-Step Guide with S3, Route 53, and CloudFront
Favicon
Resolve Lambda URL Error - signature not match when using POST/PUT
Favicon
Setting Up and Securing CloudFront for S3 Static Sites with Custom Subdomains Using AWS Cloud Development Kit(CDK)
Favicon
How to Host a Static Website on AWS Using S3, Route 53, CloudFront, and Certificate Manager
Favicon
RTT Reduction Strategies for Enhanced Network Performance
Favicon
Dynamically Choosing Origin Based on Host Header and Path with AWS CloudFront and Lambda@Edge
Favicon
Understanding Speed: A Beginner's Guide to AWS CloudFront
Favicon
Deploy a Static React Site Using AWS S3 and CloudFront

Featured ones: