DEV Community

Rajeshwari Vakharia
Rajeshwari Vakharia

Posted on

1

Host a Static Website in LocalStack using Terraform with S3 and Route 53

Link to the Github Repo

LocalStack is an emulator for AWS Services. It let's AWS like environment to run on our localmachine.
LocalStack is really helpful for beginners to not just understand AWS but to also work with it.

In this blog, we will discover how to host a static S3 website using Route53 in LocalStack with the help of another tool that is Terraform.
Terraform is a IaC tool that helps in defining the infrastrusture.

To use Terraform with LocalStack, we have to install tflocal, which is a wrapper for Terraform.

So, let's get started.

Configuring S3

Create a bucket

provider "aws" {
    secret_key = "test" 
    region = "us-east-1"
    access_key = "test"
}

resource "aws_s3_bucket" "tf_bucket" {
    bucket = "websitebucket"
}
Enter fullscreen mode Exit fullscreen mode

First, we will define the provider block and will put the required configurations.
And in the resource block we have defined the S3 bucket with the name websitebucket. This bucket will contain the files of our website.

Define the object and Remove Public access blocks from the bucket

resource "aws_s3_object" "tf_bucket_object" {
    bucket = aws_s3_bucket.tf_bucket.id
    key = "index.html"
    source = "./index.html"
    content_type = "text/html"
    acl = "public-read"
}

resource "aws_s3_bucket_public_access_block" "tf_pab"{
    bucket = aws_s3_bucket.tf_bucket.id 

    block_public_acls = false
    block_public_policy = false 
    ignore_public_acls = false
    restrict_public_buckets = false 
}

Enter fullscreen mode Exit fullscreen mode

The first resource is used to create an object in the bucket which contains the souorce file path i.e. ./folder name.
And the other resource is used to remove the public access block from the bucket so that it is not restricted to the internet.

Configure the Bucket Policy

resource "aws_s3_bucket_policy" "tf_bucketpolicy" {
    bucket = aws_s3_bucket.tf_bucket.id

    policy = jsonencode({
        Version = "2012-10-17"
        Statement = [{
                Sid = "PublicReadGetObject"
                Effect = "Allow"
                Principal = "*"
                Action = "s3:GetObject"
                Resource = "${aws_s3_bucket.tf_bucket.arn}/*" 
        }]
    })
} 
Enter fullscreen mode Exit fullscreen mode

This is a very important configuration to make. By defining this bucket policy, it makes the objects in the bucket to be accessible by other people.
Even after removing the acl, it is important to apply the bucket policy only then the objects will be accessible or else we will get error 403.

Set the bucket Website Configuration

resource "aws_s3_bucket_website_configuration" "tf_bucket_config" {
    bucket = aws_s3_bucket.tf_bucket.id

    index_document {
        suffix = "index.html"
    }
} 
Enter fullscreen mode Exit fullscreen mode

AWS provides a great way to host the bucket as a website through this configuration.

Configure the Route53

⚠️DNS Service in LocalStack comes with their Pro Version, which I don't have and hence the we will not be able to fetch the website using the domain name.

Create a Hosted Zone

resource "aws_route53_zone" "tf_zone" {
    name = "mylocalwebsite.test"  
}
Enter fullscreen mode Exit fullscreen mode

This block is used in creating a Hosted Zone, which will host our domain name.

Create an Alias Record with the S3 bucket endpoint.

resource "aws_route53_record" "tf_record"{
    zone_id = aws_route53_zone.tf_zone.zone_id
    name = "mylocalwebsite.test"
    type = "A"
    alias {
        name = aws_s3_bucket_website_configuration.tf_bucket_config.website_endpoint
        zone_id = aws_s3_bucket.tf_bucket.hosted_zone_id
        evaluate_target_health = false 
    }
}
Enter fullscreen mode Exit fullscreen mode

This block will create the alias with our S3 bucekt endpoint.

And with this at the end we have to do three things to run the infrastructure code:-

tflocal init
Enter fullscreen mode Exit fullscreen mode

This syntax initiates the resources in the file.

tflocal plan
Enter fullscreen mode Exit fullscreen mode

It helps in viewing the plan of the resources and all its configuration that we can check before applying.

tflocal apply
Enter fullscreen mode Exit fullscreen mode

And finally applying the code after all the checks.

And when we access the S3 endpoint using the path :-

http://localhost:4566/websitebucket/index.html

Static Website in LocalStack using Terraform with S3 and Route 53

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay