DEV Community

Philcz
Philcz

Posted on

Create a Simple Static Website on AWS S3 bucket using Terraform.

You can use Amazon S3 to host a static website. This is a simple way for you to host your portfolio online or for you to serve static content to your users.

Getting Started

Before we begin, you will need to have an AWS account and have terraform installed on your local machine. You can get started on AWS with a free tier.

Setting Up

Image description
Our main.tf

Variables.tf

index.html

error.html

Putting it together

After installing AWS CLI run the command below to config your AWS environment so we can run terraform.

aws configure

Image description

Terraform init
The terraform init command is used to initialize a working directory containing Terraform configuration files

Terraform fmt
The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style (My favourite terraform command)

Terraform validate
The terraform validate command validates the configuration files in a directory

Terraform plan
The terraform plan command evaluates a Terraform configuration to determine the desired state of all the resources it declares, then compares that desired state to the real infrastructure objects being managed with the current working directory and workspace.

terraform apply
The terraform apply command executes the actions proposed in a Terraform plan to create, update, or destroy infrastructure.

Enter the bucket name and yes to create the bucket.

Once the bucket is created you can upload the index.html and error.html files or use
aws s3 sync $DESTINATION s3://$BUCKET_NAME
I achieved this by placing the index.html and error.html in a $DESTINATION directory.

Click on your bucket, go to properties and scroll to the end.
Click on Bucket website endpoint to see your static website.

Top comments (0)