DEV Community

Gbenga Ojo-Samuel
Gbenga Ojo-Samuel

Posted on

Hosting your static website on AWS S3 using Terraform (IaC)

Introduction
This article lays more emphasis on the use of Terraform an infrastructure as code tool to deploy a static website on Amazon S3.
Terraform is one of the most popular Infrastructure-as-code (IaC) tool, used by DevOps teams to automate infrastructure tasks. It is used to automate the provisioning of your cloud resources. Terraform is an open-source, cloud-agnostic provisioning tool developed by HashiCorp.

Benefits of using Terraform:
• Does orchestration, not just configuration management.
• Supports multiple providers such as AWS, Azure, Oracle, GCP, and many more.
• Provide immutable infrastructure where configuration changes smoothly.
• Uses easy to understand language, HCL (HashiCorp configuration language).
• Easily portable to any other provider.

Here’s a quick overview of the Terraform workflow:
• Write Code: Write Terraform configuration files that define the infrastructure you want to create.
• Init: Initialize Terraform and download any necessary providers.
• Plan: Preview the changes that Terraform will make to your infrastructure.
• Apply: Apply the changes to your infrastructure.
• Destroy: Destroy the infrastructure that you created.

This project will guide us through the steps of deploying a static website on AWS using Terraform IaC tool.

Prerequisite:
• Terraform basics and script knowledge
• IDE (This Project, I am using VScode)
• Terraform installed on your local machine
• An AWS account with appropriate permissions
• Terraform CLI
• AWS CLI

_*Step1: Setting up your Project Directory and Authentication with AWS credentials *

A. Setting up Project Directory
• Create a project folder on your local machine.

project folder

• Open Visual studio on your local machine, click on the explorer and click open folder to select the folder you created above.

Open vscode

• Create terraform configuration files in the project folder by clicking on the new file icon for each file. Please note that your terraform files name must end with a .tf

terraform files

B. Authentication with AWS credentials
• Open the terminal to the project directory folder
• Run “aws configure” command to pass your aws credentials, specify your default region and your output format (Json)

Authentication

Please do not share your AWS credentials with anyone

Step2: Write Terraform Code

A. terraform.tf: The code below specifies the provider (AWS).

terraform.tf

B. main.tf: This body of codes specifies the infrastructure we intend to deploy on AWS. We are deploying and Amazon S3 buckets with all configuration requirements to enable static website hosting.

i. Create S3 bucket: Defines an S3 bucket to store your static website files. Update the bucket name to a globally unique name.

create s3 bucket
ii. Bucket Ownership control: To control ownership of objects uploaded to your bucket and to disable or enable access control lists (ACLs)

Bucket ownership
iii. Bucket Policy: This policy will enable public access to the objects in the bucket.

Bucket Policy

iv. Amazon S3 public access block: provides settings for access points, buckets, and accounts to help you manage public access to Amazon S3 resources.

Public access block

v. Bucket ACL: Amazon S3 access control lists (ACLs) enable you to manage access to buckets and objects. ACL is set to “Public-Read”

Bucket ACL

vi. Bucket Website Configuration: Specifies website configuration parameters for an Amazon S3 bucket.

website configuration

vii. create the website file and name it index.html and put your website code in it.

website file

vii. Upload to S3 bucket: Code below uploads the file to the s3 bucket.

upload to s3

C. output.tf: makes information about your infrastructure available on the command line. We will output our website URL on the terminal

Output

D. variable.tf: allows us to define and accept a collection of values as inputs for infrastructure deployments. We will be accepting input for our bucket name and the region of deployment

variable

E. terraform.tfvars: You can update your .tfvars file to pass input to your variables. “ . tfvars” file creates a reusable file for all the variables for a project.

tfvarsfile

Step3: Deploy with Terraform

A. Run “terraform init” command: This will initialize Terraform and download any necessary providers plugins.

Initialization

B. Run “terraform plan” command: This previews the changes that Terraform will make to your infrastructure

terraform plan1

terraform plan2

terraform plan3

C. Run “terraform apply” command: This applies the changes and deploy the infrastructure on AWS. you will need to approve the changes by typing “yes”

terraform apply

deployment complete

All resources have been created successfully. Let's check our AWS Account

aws console1

aws console2

aws console3

D. Access your Static Website
After the Terraform deployment is successful, you can access your static website using the provided website URL in the output section as seen below

output

• Copy the website endpoint URL provided in the output section
• Paste the website endpoint URL into the address bar of your browser and hit Enter.
• If everything went smoothly, your website should load up, displaying the content from the index.html file and can be accessed by anyone on the internet as shown below;

website

Congratulations! You have successfully deployed a static website on Amazon S3 using Terraform.

Notes:
• Run terraform destroy to remove all provisioned infrastructures from your AWS account so as not to incur unnecessary bills on your account.
• Use this link to the repository for the terraform codes

Thanks for reading.

Top comments (0)