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.
• Open Visual studio on your local machine, click on the explorer and click open folder to select the folder you created above.
• 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
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)
Please do not share your AWS credentials with anyone
Step2: Write Terraform Code
A. terraform.tf: The code below specifies the provider (AWS).
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.
ii. Bucket Ownership control: To control ownership of objects uploaded to your bucket and to disable or enable access control lists (ACLs)
iii. Bucket Policy: This policy will enable public access to the objects in the bucket.
iv. Amazon S3 public access block: provides settings for access points, buckets, and accounts to help you manage public access to Amazon S3 resources.
v. Bucket ACL: Amazon S3 access control lists (ACLs) enable you to manage access to buckets and objects. ACL is set to “Public-Read”
vi. Bucket Website Configuration: Specifies website configuration parameters for an Amazon S3 bucket.
vii. create the website file and name it index.html and put your website code in it.
vii. Upload to S3 bucket: Code below uploads the file to the s3 bucket.
C. output.tf: makes information about your infrastructure available on the command line. We will output our website URL on the terminal
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
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.
Step3: Deploy with Terraform
A. Run “terraform init” command: This will initialize Terraform and download any necessary providers plugins.
B. Run “terraform plan” command: This previews the changes that Terraform will make to your infrastructure
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”
All resources have been created successfully. Let's check our AWS Account
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
• 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;
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)