DEV Community

Darko Mesaroš ⛅️ for AWS

Posted on

4 2

Build on AWS Weekly - S1 E4 - Static S3 Site

Welcome to episode 4, yes 4 it's already has been a month of Build On Weekly! 🥳

Unfortunately today you only get to listen to a bald man yell into the camera, as Jacquie is away. But he (Darko), will be showing you how to some magical things with the latest amazing tool - Cloud Development Kit (CDK) for Terraform. Today is all about building the infrastructure for that static website of yours. All with the power of CDK and Terraform. 🪣

We will be posting here, on dev.to, to share show notes, links, socials, code, and any other things mentioned during the live stream with you! 🚀

CDK Terraform

If you have any questions, feedback or comments - feel free to put them in the comments of this post! 😇

Deployed Weekly

Today, on Deployed Weekly, we will cover AWS Power Tools, again, for all of you C# fans. Terraform and CDK have something to tell you. Sharing some code for Site to Site VPN over Private IPs. A cool workshop you can take next week, and a World Championship for all of you developers, and builders out there!

Static Website with CDK for Terraform

Today we are checking out the newly released CDK (Cloud Development Kit) for Terraform. 🥳 And with it, we will be building a static website hosting system using Amazon S3 🪣

The goal is to setup an S3 bucket with all the required configurations to host a static website. This was Darko's first foray with CDK for Terraform, so cut him some slack.

What is CDK for Terraform? Well it's Cloud Development Kit for Terraform... Okay, but what does that mean? Well this means that you can write Terraform configurations in your choice of TypeScript, Python, C#, Java, or Go, and still benefit from the full ecosystem of HashiCorp Terraform providers and modules. Wonderful.

This means having the infrastructure for a static website on AWS, defined in these lines of code:

import { Construct } from "constructs";
import * as path from "path";
import { sync as glob } from "glob";
import { lookup as mime } from "mime-types";
import { App, TerraformStack, TerraformOutput } from "cdktf";
import { AwsProvider, s3 } from "@cdktf/provider-aws"

class MyStack extends TerraformStack {
  constructor(scope: Construct, name: string) {
    super(scope, name);

    // AWS Provider
    new AwsProvider(this, 'AWS', {
      region: "us-west-2",
    });
    // Bucket
    const cobucket = new s3.S3Bucket(this, "cobus-website-bucket", {
      bucket: "cobus-website-bucket",
    });
    // Configure the bucket for a website
    new s3.S3BucketWebsiteConfiguration(this, "cobus-websiteconfig", {
      bucket: cobucket.bucket,
      indexDocument: {
        suffix: "index.html"
      },
      errorDocument: {
        key: "error.html"
      },
    });
    // Open up the bucket
    new s3.S3BucketPolicy(this, "cobus-policy", {
      bucket: cobucket.bucket,
      policy: JSON.stringify({
        Version: "2012-10-17",
        Id: "public-website-access",
        Statement: [
          {
            Sid: "PublicRead",
            Effect: "Allow",
            Principal: "*",
            Action: ["s3:GetObject"],
            Resource: [`${cobucket.arn}/*`, `${cobucket.arn}`],
          },
        ],
      }),
    });
    // Add files
    const absolutePath = path.resolve(__dirname, "website/");
    const files = glob("**/*.html", {
      cwd: path.resolve(__dirname, "website/"),
    });

    // file loop
    files.forEach((f) => {
      const filePath = path.join(absolutePath, f);

      new s3.S3Object(this, `${f}`, {
        bucket: cobucket.bucket,
        key: f,
        source: filePath,
        contentType: mime(path.extname(f)) || "text/html",
      });
    });

    // outputs
    new TerraformOutput(this, 'bucketname', {
      value: cobucket.bucket,
    });
  }
}

const app = new App();
new MyStack(app, "staticwebsite-with-cdktf");
app.synth();
Enter fullscreen mode Exit fullscreen mode

💾 Check out the rest of the code Darko has written today, as you follow along with the video: https://github.com/darko-mesaros/cdktf-s3-website

Links from the discussion:

🐦 Reach out to the hosts and guests:

Jacquie: https://twitter.com/devopsjacquie
Darko: https://twitter.com/darkosubotica

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay