logo
Menu

Static S3 Website | S01 E04 | Build On Weekly

A simple task as any, host a static website on Amazon S3

Darko Mesaros
Darko Mesaros
Amazon Employee
Published Mar 31, 2023

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 Community.aws, to share show notes, links, socials, code, and any other things mentioned during the live stream with you! 🚀

CDK Terraform

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!

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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();

💾 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

🐦 Reach out to the hosts and guests:

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