Creating a static website with Terraform and AWS’ S3

Camilo Matajira Avatar

In this project I want to provision an S3 bucket with Terraform, and then use the AWS cli to copy the content of the web page to the bucket.

You can check the source code here.

Steps

The steps to run the project are the following:

#! /bin/bash

pip3 install awscli==1.18.74

export AWS_ACCESS_KEY_ID=XXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXX
export S3_BUCKET_NAME=camilomatajira-bucket.com
export AWS_REGION=eu-west-3

# Launch terraform
terraform init
terraform plan --out my_plan
terraform apply my_plan

# Copy the content
aws s3 cp ./index.html s3://${S3_BUCKET_NAME}/index.html

# To test if it works
curl http://${S3_BUCKET_NAME}.s3-website.${AWS_REGION}.amazonaws.com

# TO DESTROY
# terraform destroy

The content of the terraform file is the following:

resource "aws_s3_bucket" "b" {
  bucket = "camilomatajira-bucket.com"
  acl    = "public-read"
  force_destroy = true
  policy = <<EOF
{ 
  "Version":"2012-10-17",
  "Statement":[{
    "Sid":"PublicReadGetObject",
        "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::camilomatajira-bucket.com/*"
      ]
    } 
  ] 
} 
EOF

  website {
    index_document = "index.html"
    error_document = "error.html"curl http://${S3_BUCKET_NAME}.s3-website.${AWS_REGION}.amazonaws.com
  }
}

Tagged in :

Camilo Matajira Avatar