Build AWS Cloud Infrastructure as Code (IaC) using Terraform and deploy a Static Website on it.
This application creates (and destroys) the necessary cloud infrastructure resources to host a static website on AWS. It also uploads (and destroys) the static website (located in the directory: s3_content/) to the S3 backend.
A combination of AWS CloudFront and AWS S3 is used to implement:
- high availability of the website,
- no latency for visitors worldwide,
- automatic scaling of infrastructure components to serve high amounts of user traffic.
This section describes the steps and installations that were necessary to create and run the code in Visual Studio Code on macOS Sonoma.
https://formulae.brew.sh/formula/terraform
https://developer.hashicorp.com/terraform/install?product_intent=terraform
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
https://formulae.brew.sh/formula/awscli
Go to IAM console, create IAM User, do not click checkbox "Provide access to AWS Console", select "attach policy directly" and give AdministratorAccess rights, review and create user
click on the user, click on tab "Security credentials" and generate "new access key", select for (CLI) and create access key.
Open Terminal (i.e. terminal inside VS Code) and enter "aws configure", provide both keys from #6 and default zone "eu-central-1", then click enter.
When using an ami (amazon machine image - i.e. when creating an EC2 instance) in the Terraform code, the region (i.e. eu-central-1) of the provider in the file config.tf must be the same region as selected in the AWS console (website) when you are choosing an ami (amazon machine image) number from the AWS Console.
After cloning the project to your computer and setting up the described configurations, use the following commands.
run this after writing a new terraform configuration or cloning the project from github
to validate the terraform code
to preview the execution plan
confirm with "yes" - to create the cloud infrastructure
and wait for the deployment to complete
In the AWS Console navigate to the newly created "CloudFront" distribution
copy the "Domain name" (i.e. abc123123123xyz.cloudfront.net) and access it using a browser
The result should look similar to this image:
In order to destroy all resources (i.e. the static website files) as well as all created cloud infrastructure resources, run the following command.
confirm with "yes" - to destroy the cloud infrastructure
It is recommended to verify successful deletion of all resources by navigating through the AWS Console and making sure of it.