Hosting your static website on AWS S3 using Terraform (IaC)
Tags: tutorial, aws, vscode
Published: 2023-08-10 12:47:53

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/osi64kcl7hp8hskcycm2.png)


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

![Open vscode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7k1m7b9tnaqdnj06e2n.png)

• 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/475zhiw9rewvtb0x3qnn.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nunt7epj447g1b03gmok.png)

_**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4oh8d21umzwjogld9zx.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/430311p8u95mdrzgow8f.png)
ii. Bucket Ownership control: To control ownership of objects uploaded to your bucket and to disable or enable access control lists (ACLs)


![Bucket ownership](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5zonlc39hd9ucimkg0l.png)
iii. Bucket Policy: This policy will enable public access to the objects in the bucket.

![Bucket Policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yw68401zjy86nk48r4d9.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73055mcao54b8mrhp92n.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iucwsv4pykl29vhf4kp0.png)

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


![website configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eusikn4u8k66h8nit52s.png)

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

![website file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n631hlfsf5vbi715wn2q.png)

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


![upload to s3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw05izkvlmo45he2w4m9.png)

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


![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4ubjz1vogib3qdn774n.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuyrohw7el1vbx0fjfay.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nq5t1kw2t57koa44c93q.png)

**Step3: Deploy with Terraform**

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

![Initialization](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g14zfz0k9sb852e8uxto.png)

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

![terraform plan1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhc41qboggydlbnh92cl.png)

![terraform plan2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejtau6a4d7k6ms6ipult.png)


![terraform plan3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cm411lr662n3opvet3qk.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9qh3zm9so2mtt2kgicz.png)

![deployment complete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lai5qzkdwgc6g9psqtrf.png)

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

![aws console1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dln8yo64ydp6yjgtchtj.png)

![aws console2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8s3xw70py7n8pchy9etf.png)

![aws console3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esah4hyyy8r4kii8th3l.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjpb1774fk4tqh83n5g4.png)

• 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iz4r45fhgl7blo72u2kw.png)

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.