Piers Karsenbarg

Setting Up S3

Posted at — Apr 27, 2019

Since CloudFront needs to know what S3 bucket to point to and the remaining DNS changes need to know about the CloudFront distribution domain, next step is S3.

In this, we’re setting up the bucket name, adding versioning (just in case), ensuring that the bucket can act as a static website and adding an IAM policy to allow the files to be read.

Finally, I’m adding a file to be the index whilst I’m setting things up.

s3.tf

resource "aws_s3_bucket" "www" {
  bucket = "${var.root_domain_name}-site"

  versioning {
    enabled = true
  }

  website {
      index_document = "index.html"
  }

  policy = <<EOF
{
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"AddPerm",
      "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::${var.root_domain_name}-site/*"]
    }
  ]
}
EOF
}

resource "aws_s3_bucket_object" "object" {
  bucket = "${aws_s3_bucket.www.bucket}"
  key    = "index.html"
  source = "./files/index.html"
  # The filemd5() function is available in Terraform 0.11.12 and later
  # For Terraform 0.11.11 and earlier, use the md5() function and the file() function:
  # etag = "${md5(file("path/to/file"))}"
  #etag = "${filemd5("./files/index.html")}"
  content_type = "text/html"
}