Static S3 Website | S01 E04 | Build On Weekly
A simple task as any, host a static website on Amazon S3
- AWS PowerTools for dotnet is in alpha: https://github.com/awslabs/aws-lambda-powertools-dotnet
- CDK for TF is now GA: https://www.hashicorp.com/blog/cdk-for-terraform-now-generally-available
- AWS Site to Site VPN Over Private IP, via Direct connect. All in Terraform:
https://github.com/aws-samples/aws-site-to-site-vpn-private-ip-vpn - Pulumi’s Introduction to Infrastructure as Code with Python - Workshop: https://www.pulumi.com/resources/introduction-to-infrastructure-as-code-with-python/
- Game Day World championship: https://pages.awscloud.com/GLOBAL-gamedev-OE-AWS-GameDay-WorldChampionship-2022-reg-event.html
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();
- CDK for Terraform is now generally available: https://www.hashicorp.com/blog/cdk-for-terraform-now-generally-available
- Terraform AWS Provider on Construct hub: https://constructs.dev/packages/@cdktf/provider-aws/v/9.0.10/api/S3Bucket?lang=typescript&submodule=s3
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.