Lesson 21 of 22
In Progress

[LAB] Create a Static S3 Website Using Terraform

Tyler Petty July 10, 2024
Hands-On Lab

Responses

Your email address will not be published. Required fields are marked *

  1. Ran into a few issues on this one while trying to follow long by manually typing and incrementally adding / applying as the lab went along.

    When you have the writeup and a terminal side by side the code “aws_s3_bucket_website_configuration.website_config.website_endpo
    int” looks more like “aws_s3_bucket_website_configuration.website_config.website_
    int” which presented an error on terraform apply.

    While following along incrementally, I had created a bucket that and all steps were working except for 1 error:
    aws_s3_bucket_policy.bucket-policy: Creating…
    aws_s3_bucket_public_access_block.public_access_block: Creating…
    aws_s3_bucket_website_configuration.website_config: Creating…
    aws_s3_object.index: Creating…
    aws_s3_object.index: Creation complete after 1s [id=index.html]
    aws_s3_bucket_public_access_block.public_access_block: Creation complete after 1s [id=add-unique-bucket-lizzie]
    aws_s3_bucket_website_configuration.website_config: Creation complete after 1s [id=add-unique-bucket-lizzie]
    â•·
    │ Error: putting S3 Bucket (add-unique-bucket-lizzie) Policy: operation error S3: PutBucketPolicy, https response error StatusCode: 403, RequestID: FA6KY5PWKZ5ZWAWA, HostID: sCP2cVc7ifxvkXDQ/bgiPwOGxwky5Q4KoD/XNTlJD3SEmolrJzDmYjoVVT+2cl34sN/KXQ2NMNs=, api error AccessDenied: User: arn:aws:iam::921234892411:user/create-a-static-s3-website-using-terrafor-MyIAMUser-hwdtLj8mlDDn is not authorized to perform: s3:PutBucketPolicy on resource: “arn:aws:s3:::add-unique-bucket-lizzie” because public policies are blocked by the BlockPublicPolicy block public access setting.

    I believe this was because the bucket was already created and terraform tried to put a policy rather than just creating the resource with that policy already.

    1. Hey Lizzie, I thought it was due to our wordwrapping, but actually, it seems that a line break was accidentally added when we copy/pasted the code here. Sorry about that! Fixed the line and also disabled wordwrap for good measure

  2. In the main.tf file, while updating it follow the proper sequence for the Step 2 as mentioned below

    2. Enabling S3 public access

    # make the bucket publicly accessible
    resource “aws_s3_bucket_public_access_block” “public_access_block” {
    bucket = aws_s3_bucket.bucket.id

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

    # allow public read access to all objects in the bucket
    resource “aws_s3_bucket_policy” “bucket_policy” {
    bucket = aws_s3_bucket.bucket.id

    policy = jsonencode({
    Version = “2012-10-17”
    Statement = [
    {
    Sid = “PublicReadGetObject”,
    Effect = “Allow”,
    Principal = “*”,
    Action = “s3:GetObject”,
    Resource = “${aws_s3_bucket.bucket.arn}/*”,
    },
    ],
    })
    }

    Otherwise you could get an error

    │ Error: putting S3 Bucket (terraformbuckets324aug4) Policy: operation error S3: PutBucketPolicy, https response error StatusCode: 403, RequestID: FYMNFYE78AMZVAAR, HostID: Bt1r5C06JsHSFEDmiZ4yR7pfWcrHdll4aTXJczoWd52ZmnNtudSRb6Tpw59bd+SrrD/X3jLKGsw=, api error AccessDenied: User: arn:aws:iam::872863893867:user/create-a-static-s3-website-using-terrafor-MyIAMUser-M575LpMOdbFA is not authorized to perform: s3:PutBucketPolicy on resource: “arn:aws:s3:::terraformbuckets324aug4” because public policies are prevented by the BlockPublicPolicy setting in S3 Block Public Access.