Skip to content

This repo contains the code to create the s3 bucket inside your AWS and also configure the static website inside that.

Notifications You must be signed in to change notification settings

NashTech-Labs/s3-using-terraform-web-hosting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

static-website-hosting-S3-Terraform

S3 static website using Terraform

This Terraform configuration sets up an AWS S3 bucket with the following features:

  • Publicly accessible website hosting.
  • Two HTML files (index.html and error.html) uploaded as S3 objects.
  • Bucket configured to serve as a static website.
  • Public access granted via bucket policy and ACL.
  • Ownership controls set to "BucketOwnerPreferred".

Prerequisites

Ensure you have the following before applying this configuration:

  • Terraform installed on your machine.
  • AWS CLI installed and configured with appropriate credentials.
  • An index.html and error.html file available in the same directory as the Terraform configuration.

Files

  • index.html: The main page of the static website.
  • error.html: The error page displayed for invalid paths.
  • main.tf: Contains the Terraform configuration.

Configuration Details

Resources

  1. S3 Bucket

    • Change the name of default = "replace_me" in variable.tf.
  2. S3 Objects

    • Uploads index.html and error.html to the bucket.
    • Sets Content-Type as text/html and Content-Disposition to inline.
  3. Public Access Block

    • Configures the bucket to allow public access.
  4. Bucket Policy

    • Grants public read access to all objects in the bucket.
  5. Ownership Controls

    • Sets the ownership preference to BucketOwnerPreferred.
  6. Website Configuration

    • Configures the bucket for static website hosting with index.html as the index document and error.html as the error document.
  7. Bucket ACL

    • Sets the ACL to public-read to make the bucket publicly accessible.

Dependencies

  • The bucket policy depends on the public access block to ensure proper sequencing.
  • The ACL configuration depends on the ownership controls to avoid conflicts.

How to Use

  1. Place the index.html and error.html files in the same directory as the Terraform configuration file.
  2. Initialize Terraform:
    terraform init
  3. Validate the configuration:
    terraform validate
  4. Apply the configuration:
    terraform apply
    Review the plan and type yes to confirm.
  5. Access the static website using the bucket's website endpoint (visible in the Terraform output or AWS Management Console).

Notes

  • Ensure your HTML files are present before running terraform apply.
  • Make sure your AWS IAM user or role has sufficient permissions to create S3 resources.
  • To avoid incurring unnecessary costs, remember to destroy the resources when they are no longer needed:
    terraform destroy

About

This repo contains the code to create the s3 bucket inside your AWS and also configure the static website inside that.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published