CloudWarGames.com - Challenge - Beginner 2 - Static S3 Website


CloudWarGames.com - Challenge - Beginner 2 - Static S3 Website

Brief:

Explode Me Inc had a static S3 website but recently it stared throwing an 403 Forbidden error.

Your job is to figure out how to get it back up and running.

PS: The image for this post is NOT a broken thumbnail. That is what you will see if you get this Scenario booted up correctly before you figure out a fix.

Terraform:


variable "bucket_name" {
  description = "The name of the S3 bucket"
  type        = string
}

variable "domain_name" {
  description = "The custom domain name for the S3 website"
  type        = string
  default = ""
}
data aws_route53_zone my_hosted_zone{
  count = var.domain_name == "" ? 0 : 1
  name = var.domain_name
}



resource "aws_s3_bucket" "website" {
  bucket = var.bucket_name
}
resource "aws_route53_record" "website" {
  count = var.domain_name == "" ? 0 : 1
  zone_id = data.aws_route53_zone.my_hosted_zone[0].zone_id
  name    = var.domain_name
  type    = "A"

  alias {
    name                   = aws_s3_bucket_website_configuration.website.website_domain
    zone_id                = "Z3AQBSTGFYJSTF" # AWS global S3 website zone ID
    evaluate_target_health = false
  }
}


resource "aws_s3_bucket_website_configuration" "website" {
  bucket = aws_s3_bucket.website.id

  index_document {
    suffix = "index.html"
  }
}
resource  "aws_s3_object" "important_stuff_s3_object" {
  bucket = aws_s3_bucket.website.id
  key    = "index.html"
  content  =  "<html><body>I am a website!</body></html>"
  content_type = "text/html"
}

resource "aws_s3_bucket_public_access_block" "website" {
  bucket = aws_s3_bucket.website.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}
resource "time_sleep" "wait_30_seconds" {
  depends_on = [aws_s3_bucket_public_access_block.website]

  create_duration = "10s"
}



resource "aws_s3_bucket_ownership_controls" "website" {
  bucket = aws_s3_bucket.website.id
  rule {
    object_ownership = "ObjectWriter"
  }
}


output "website_url" {
  description = "The website endpoint"
  value       = aws_s3_bucket_website_configuration.website.website_endpoint
}

Brief:

Explode Me Inc had a static S3 website but recently it stared throwing an 403 Forbidden error. Your job is to figure out how to get it back up and running.

Setup Instructions:

Setup 1 - With A Domain:

Run the following command but replace {domain_name} with a domain you own.

terraform apply -var='bucket_name={domain_name}' -var='domain_name={domain_name}'

Tip: You can also make it a subdomain so if you only have one domain (mydomain.com) just pass in something like explodeme.mydomain.com.

Setup 2 - With No Domain:

Run the following command but replace {bucket_name} with a globally unique bucket name.

terraform apply -var='bucket_name={bucket_name}'

This will output 1 line at the end that will have the URL you will need to hit to test this.

It should look something like this:

website_url = "{bucket_name}.com.s3-website-us-east-1.amazonaws.com"

If you get stuck shoot me an email and I will drop a hint.

Upcoming Event!

Before I forget the next Cloud War Games Live event should be February 28th at 1PM ​Chicago Time​. To get the event link signup at CloudWarGames.com

~Best of luck!

Matt