Boost Your Website Performance: Hosting on S3 with CloudFront and Route 53 (Terraform version)

(AWS ACM public certificate included)

Jessada.Srm
3 min readMay 29, 2024
Generated image by DALL-E

Objective

I’ll demonstrate how to leverage Infrastructure as Code (IaC) to modernize an existing project, making it more reliable and repeatable. We’ll transform the traditional approach of hosting a website on S3 with CloudFront and Route 53 into a streamlined, automated process.

Recommended For

  • Students and AWS users with Terraform experience
  • Developers

Prerequisites

Agenda

Introduction

In a previous session, I demonstrated how to host a static website in an S3 bucket, distribute content worldwide for low latency using CloudFront, and secure the connection with a custom domain name using Route 53 and ACM. In this blog, I will show you how to use Terraform to provision the same project, specifically focusing on S3, CloudFront, and Route 53.

Architecture Overview

Code Repository

Code Structure Overview

I desire to separate my services into modules and main directory.

  • main
    This is where all modules are imported and each module’s input and output are managed.
  • modules
    Each module has its own independent files.

Code Explanation

Reminder: You need to request a Route 53 domain name and validate it with an ACM certificate before using my Terraform code.

You need to modify these data and variables based on your specific case. I have provided mock values as examples.

route53 module

  • hosted_zone_id
  • a_alias_record_to_cloudfront_distribution

s3 module

  • bucket_name
  • index.html

cloudfront module

  • distribution_aliases_domain
  • root_domain_public_certificate_arn

Terraform Apply

When you finish replacing the mentioned data and variables, now you can apply this Terraform to provision AWS services.

1.Init

terraform init

2.Apply

terraform apply
Example of when run apply command.

Terraform Destroy

When you no longer need these services, you should clean up your resources to reduce AWS costs.

1.Destroy

terraform destroy
Example of when run destroy command

That’s it! This blog is quite short because I’ve already explained the project fundamentals in my previous blog. I hope this post gives you a clear understanding of how to provision this project using Terraform. See you in the next blog!

--

--

Jessada.Srm

Ex-Mechanical Engineer, now a Software Engineer. Passionate about cloud and software development. Blogging to document and share my projects.