-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchurl-stack.ts
179 lines (161 loc) · 5.92 KB
/
searchurl-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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as cdk from 'aws-cdk-lib';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
import { Reference } from 'aws-cdk-lib';
import { S3OriginProps, S3Origin } from 'aws-cdk-lib/aws-cloudfront-origins';
import { CfnOriginAccessControl } from 'aws-cdk-lib/aws-cloudfront';
import { Source, BucketDeployment } from 'aws-cdk-lib/aws-s3-deployment';
// To use OAC instead of OAI, we need to patch the S3Origin class
// https://github.com/aws/aws-cdk/issues/21771#issuecomment-2081710100
type S3OriginWithOACPatchProps = S3OriginProps & {
originAccessControlId: Reference;
};
class S3OriginWithOACPatch extends S3Origin {
private readonly originAccessControlId: Reference;
constructor(bucket: s3.IBucket, props: S3OriginWithOACPatchProps) {
super(bucket, props);
this.originAccessControlId = props.originAccessControlId;
}
public bind(
scope: Construct,
options: cloudfront.OriginBindOptions
): cloudfront.OriginBindConfig {
const originConfig = super.bind(scope, options);
if (!originConfig.originProperty)
throw new Error('originProperty is required');
return {
...originConfig,
originProperty: {
...originConfig.originProperty,
originAccessControlId: this.originAccessControlId.toString(),
s3OriginConfig: {
...originConfig.originProperty.s3OriginConfig,
originAccessIdentity: '',
},
},
};
}
}
function createCFFunction(scope: Construct, id: string) {
return new cloudfront.Function(scope, `${id}Fn`, {
code: cloudfront.FunctionCode.fromFile({
filePath: `cloudfront-functions/${id}.js`,
}),
runtime: cloudfront.FunctionRuntime.JS_2_0,
functionName: id,
});
}
export class SearchUrlStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const origin = new s3.Bucket(this, 'Origin', {
bucketName: `coldstart-zero-lootdrop-origin-${this.account}-${this.region}`,
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
lifecycleRules: [
{
prefix: 'r/',
expiration: cdk.Duration.days(1),
abortIncompleteMultipartUploadAfter: cdk.Duration.days(1),
},
],
});
new BucketDeployment(this, 'DeployWebsite', {
sources: [Source.asset('./website/dist')],
destinationBucket: origin,
prune: false,
});
const s3BucketOAC = new CfnOriginAccessControl(this, 's3-bucket-OAC', {
originAccessControlConfig: {
name: 's3-bucket-OAC',
originAccessControlOriginType: 's3',
signingBehavior: 'always',
signingProtocol: 'sigv4',
},
});
// By using s3.Bucket.fromBucketName instead of origin, it doesn't add the OAI policy to the bucket resource policy
// https://github.com/aws/aws-cdk/issues/21771#issuecomment-2094497498
const s3BucketOrigin = new S3OriginWithOACPatch(
s3.Bucket.fromBucketName(this, 'OriginBucket', origin.bucketName),
{
originAccessControlId: s3BucketOAC.getAtt('Id'),
}
);
const apiRequestFn = createCFFunction(this, 'czldAPIRequest');
const apiResponseFn = createCFFunction(this, 'czldAPIResponse');
const redirectFn = createCFFunction(this, 'czldRedirect');
let distro = new cloudfront.Distribution(this, 'distro', {
priceClass: cloudfront.PriceClass.PRICE_CLASS_100,
defaultBehavior: {
origin: s3BucketOrigin,
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.HTTPS_ONLY,
compress: true,
},
defaultRootObject: 'index.html',
comment: 'Coldstart Zero Loot Drop Distribution',
additionalBehaviors: {
'/r/*': {
origin: s3BucketOrigin,
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
functionAssociations: [
{
function: redirectFn,
eventType: cloudfront.FunctionEventType.VIEWER_RESPONSE,
},
],
},
'/api': {
origin: s3BucketOrigin,
//Allow PUT requests to the /api path
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED,
originRequestPolicy:
cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
functionAssociations: [
{
function: apiRequestFn,
eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
},
{
function: apiResponseFn,
eventType: cloudfront.FunctionEventType.VIEWER_RESPONSE,
},
],
},
},
});
// Add OAC policy to the bucket that allows read of all objects, and write of objects in the r/ prefix
origin.addToResourcePolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:GetObject'],
principals: [new iam.ServicePrincipal('cloudfront.amazonaws.com')],
resources: [origin.arnForObjects('*')],
conditions: {
StringEquals: {
'AWS:SourceArn': `arn:aws:cloudfront::${this.account}:distribution/${distro.distributionId}`,
},
},
})
);
origin.addToResourcePolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:PutObject'],
principals: [new iam.ServicePrincipal('cloudfront.amazonaws.com')],
resources: [origin.arnForObjects('r/*')],
conditions: {
StringEquals: {
'AWS:SourceArn': `arn:aws:cloudfront::${this.account}:distribution/${distro.distributionId}`,
},
},
})
);
new cdk.CfnOutput(this, 'Url', {
value: `https://${distro.distributionDomainName}`,
});
}
}