-
Notifications
You must be signed in to change notification settings - Fork 0
/
coldstart-zero-stack.ts
127 lines (116 loc) · 4.17 KB
/
coldstart-zero-stack.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as cdk from "aws-cdk-lib";
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
import * as origins from "aws-cdk-lib/aws-cloudfront-origins";
import * as s3 from "aws-cdk-lib/aws-s3";
import { Construct } from "constructs";
import * as fs from "fs";
export class ColdstartZeroStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const ENABLE_PRESIGN = true;
//this origin is never actually hit, but every cloudfront distribution needs an origin
const origin = new s3.Bucket(this, "Origin", {
bucketName: `coldstart-zero-origin-${this.account}-${this.region}`,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const intrinsicFunctions = new cloudfront.Function(
this,
"IntrinsicFunctions",
{
code: cloudfront.FunctionCode.fromFile({
filePath: "cloudfront-functions/intrinsicFunctions.js",
}),
runtime: cloudfront.FunctionRuntime.JS_2_0,
functionName: "czIntrinsicFunctions",
}
);
let presignFunction: cloudfront.Function | undefined;
if (ENABLE_PRESIGN) {
assert(
isDefined(process.env.COLDSTART_ZERO_USER_AK),
"env variable COLDSTART_ZERO_USER_AK must be set to your AWS access key for presigning S3 URLs"
);
assert(
isDefined(process.env.COLDSTART_ZERO_USER_SK),
"env variable COLDSTART_ZERO_USER_SK must be set to your AWS secret key for presigning S3 URLs"
);
const kvStore = new cloudfront.KeyValueStore(this, "KeyValueStore", {
keyValueStoreName: "ColdstartZeroKVStore",
source: cloudfront.ImportSource.fromInline(
JSON.stringify({
data: [
{
key: "ACCESS_KEY",
value: process.env.COLDSTART_ZERO_USER_AK,
},
{
key: "SECRET_KEY",
value: process.env.COLDSTART_ZERO_USER_SK,
},
],
})
),
});
presignFunction = new cloudfront.Function(this, "PreSignFunction", {
code: cloudfront.FunctionCode.fromInline(
fs
.readFileSync("cloudfront-functions/presign.js", "utf8")
.toString()
.replace("KVSTORE_ID", kvStore.keyValueStoreId)
),
runtime: cloudfront.FunctionRuntime.JS_2_0,
functionName: "czPreSign",
keyValueStore: kvStore,
});
}
let distro = new cloudfront.Distribution(this, "distro", {
defaultBehavior: {
origin: new origins.S3Origin(origin),
functionAssociations: [
{
function: intrinsicFunctions,
eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
},
],
cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED,
},
additionalBehaviors: ENABLE_PRESIGN
? {
"presign/*": {
origin: new origins.S3Origin(origin),
functionAssociations: [
{
function: presignFunction!,
eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
},
],
cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED,
},
}
: undefined,
});
new cdk.CfnOutput(this, "Distribution", {
value: `https://${distro.distributionDomainName}`,
});
new cdk.CfnOutput(this, "DistributionULID", {
value: `https://${distro.distributionDomainName}/ulid`,
});
new cdk.CfnOutput(this, "DistributionSortByPath", {
value: `https://${distro.distributionDomainName}/sortByPath?arr=[{%22date%22:%2212-31-06%22},{%22date%22:%2212-31-23%22},{%22date%22:%2212-31-19%22}]&path=date`,
});
if (ENABLE_PRESIGN) {
new cdk.CfnOutput(this, "DistributionPresign", {
value: `https://${distro.distributionDomainName}/presign/<bucket>.s3.<region>.amazonaws.com/<path>`,
});
}
}
}
function isDefined(x: string | undefined | null) {
return x !== undefined && x !== null;
}
function assert(
condition: boolean,
message: string | undefined = undefined
): void {
if (!condition) throw Error("Assert failed: " + (message || ""));
}