Skip to main content

Command Palette

Search for a command to run...

Simple Website Hosting on AWS S3 Using Terraform

Published
4 min readView as Markdown
A

I am a passionate DevOps enthusiast with a strong foundation in Linux, containerization, orchestration, automation, and cloud technologies. My goal is to continuously learn and implement DevOps best practices to streamline development and operations processes.

Introduction

In this blog post, we'll explore the process of hosting a simple static website on Amazon Web Services (AWS) S3 using Terraform. Static websites are cost-effective and efficient, making them an excellent choice for personal portfolios, blogs, or small business websites. By the end of this guide, you'll have your website up and running on AWS S3, all managed through Terraform.

Step 1: Install Terraform and AWS CLI

Before we begin, ensure that you have Terraform and the AWS CLI installed on your system. You can find installation instructions for both tools on the official Terraform website and the AWS CLI documentation.

Step 2: Create an IAM User

To interact with AWS services, we'll need an IAM (Identity and Access Management) user with administrative access. Follow these steps:

  1. Log in to your AWS account.

  2. Navigate to the IAM service.

  3. Click "Users" and then "Add User."

  4. Give your user a name, and under permissions, choose "Attach policies directly" and select the "AdministratorAccess" policy.

  5. Create an Access Key and save the CSV file containing the access key and secret access key for later use.

Step 3: Authenticate with IAM Credentials

Open your terminal and run the following command, replacing ACCESS_KEY and SECRET_KEY with your IAM user's credentials:

$ aws configure

Step 4: Configuration using Terraform

Create a folder for your project and open it in Visual Studio Code.My folder name is "terraform-s3-static-webhost".

  1. Create a provider.tf file. This is where we configure the AWS provider. Refer to the Terraform documentation for provider configuration details.

    Initialize Terraform using:

    $ terraform init

    It will help us to download all the dependencies or files for the provider.

  2. Create a main.tf file where we'll define our S3 bucket configuration. You can copy and paste the S3 bucket configuration from the Terraform documentation.

    Ensure your bucket name is unique globally.

  3. Create a variable.tf file to define variables, like your bucket name.

    Run terraform plan and terraform apply -auto-approve to create the S3 bucket.

    (-auto-approve -use this only if you want to skip typing yes to apply)

    Now navigate to your AWS account>s3 and check if the bucket is successfully created or not.

  4. Manage S3 Bucket Ownership

    To manage S3 bucket ownership, add the S3 bucket ownership controls configuration to your main.tf file

  5. Make the Bucket Public

    To make your bucket public and add an ACL resource, use the S3 bucket public access block configuration. Set all values to 'false'.

    With 'public-read' ACL, this disables the default S3 bucket security settings.

    https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl

    Run terraform plan and terraform apply. Check the changes on AWS S3 bucket permissions.

  6. Enable Static Website Hosting

    To enable static website hosting, use the S3 bucket website configuration resource. Before that, You'll need to create index.html and error.html files in your project folder and add them to your S3 bucket using the S3 object configuration.

    1. index.html: The index.html file is the main entry point of a website. It contains the content and structure of the webpage that visitors see first when they access the site.

    2. error.html: The error.html file is a custom error page that is displayed when a visitor encounters an issue or error while navigating the website. It provides a user-friendly message to help users understand and resolve the problem.

Edit index.html to customize your website. Retrieve the object URL for profile.jpg in your AWS S3 bucket(AWS >S3 > your bucket>profile.jpeg >copy object URL).

Go to VSC>index.html add this object URL to img src.

After making changes, run terraform plan and terraform apply. Navigate to AWS S3 > your bucket > properties > static web hosting to see your portfolio.

Project

You can find the Project on my github:

https://github.com/Anju12345hub/Terraform-s3-static-webhosting-project

Reference

https://youtu.be/wFWg0Y68Owo?si=gYRjlTg0xFUQSF4q

Conclusion

You've successfully hosted a simple static website on AWS S3 using Terraform. This setup is not only cost-effective but also scalable and reliable. Feel free to customize your website further and explore more advanced AWS features to enhance your hosting experience. Happy hosting!

More from this blog

Untitled Publication

23 posts