Create Basic Static website with AWS S3 and Terraform

zekesebulino

Zeke Sebulino

Posted on May 22, 2023

Create Basic Static website with AWS S3 and Terraform

variables.tf

variable "bucketName" {
  type    = string
  default = "example-web-app-terraform"
}

Enter fullscreen mode Exit fullscreen mode

provider.tf

provider "aws" {
  region  = "ap-southeast-1"
  profile = "zeke"
}

Enter fullscreen mode Exit fullscreen mode

main.tf

resource "aws_s3_bucket" "example" {
  bucket = var.bucketName
}

resource "aws_s3_bucket_public_access_block" "example" {
  bucket                  = aws_s3_bucket.example.id
  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

resource "aws_s3_bucket_policy" "example-policy" {
  bucket     = aws_s3_bucket.example.id
  policy     = templatefile("s3-policy.json", { bucket = var.bucketName })
  depends_on = [aws_s3_bucket_public_access_block.example]
}

resource "aws_s3_bucket_website_configuration" "example-config" {
  bucket = aws_s3_bucket.example.bucket
  index_document {
    suffix = "index.html"
  }
}

resource "aws_s3_object" "example-index" {
  bucket       = aws_s3_bucket.example.id
  key          = "index.html"
  source       = "./index.html"
  content_type = "text/html"
}

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
zekesebulino
Zeke Sebulino

Posted on May 22, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related