-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhey-amplify-app.ts
288 lines (255 loc) · 9.02 KB
/
hey-amplify-app.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import type * as s3 from 'aws-cdk-lib/aws-s3'
import type { AmplifyAwsSubdomain } from './amplify-aws-subdomain'
import type * as efs from 'aws-cdk-lib/aws-efs'
import type * as ssm from 'aws-cdk-lib/aws-ssm'
import { Construct } from 'constructs'
import { Port } from 'aws-cdk-lib/aws-ec2'
import * as cdk from 'aws-cdk-lib'
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'
import * as ecs from 'aws-cdk-lib/aws-ecs'
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns'
import * as elb from 'aws-cdk-lib/aws-elasticloadbalancingv2'
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'
import * as route53 from 'aws-cdk-lib/aws-route53'
import * as route53Targets from 'aws-cdk-lib/aws-route53-targets'
import { v4 as uuid } from 'uuid'
import { WAF } from './waf'
interface DockerProps {
name: string
context: string
dockerfile?: string
environment?: { [key: string]: string }
}
export interface HeyAmplifyAppProps {
/**
* S3 bucket to store SQLite backups and logs
*/
bucket: s3.Bucket
/**
* ECS Cluster the Application Load Balanced Fargate Service will be deployed to.
*/
cluster: ecs.Cluster
/**
* Asset path to the directory with the Dockerfile.
*/
docker: DockerProps
/**
* Filesystem to be used for storing the application's data.
*/
filesystem: efs.FileSystem
/**
* Filesystem container mount point
*/
filesystemMountPoint: string
/**
* Discord Bot secrets
*
* Discord bots can be managed in the {@link https://discord.com/developers/applications Developer Portal}.
*/
secrets: {
// DISCORD_BOT_TOKEN: ssm.IParameter
[name: string]: ssm.IParameter
}
/**
* Amplify AWS Subdomain (if exists)
*/
subdomain: AmplifyAwsSubdomain | undefined
}
export class HeyAmplifyApp extends Construct {
private readonly appName: string = this.node.tryGetContext('name')
private readonly envName: string = this.node.tryGetContext('env')
constructor(scope: Construct, id: string, props: HeyAmplifyAppProps) {
super(scope, id)
const {
bucket,
cluster,
filesystem,
filesystemMountPoint,
docker,
subdomain,
} = props
const secrets = {}
for (const [name, param] of Object.entries(props.secrets)) {
secrets[name] = ecs.Secret.fromSsmParameter(param)
}
const albFargateService =
new ecs_patterns.ApplicationLoadBalancedFargateService(
this,
`AlbFargateService`,
{
cluster,
cpu: 256,
memoryLimitMiB: 512,
desiredCount: 1,
circuitBreaker: { rollback: true },
taskImageOptions: {
containerName: docker.name,
image: ecs.ContainerImage.fromAsset(docker.context, {
file: docker.dockerfile || 'Dockerfile',
// https://github.com/aws/aws-cdk/issues/14395
buildArgs: docker.environment,
}),
environment: {
...docker.environment,
BUCKET_NAME: bucket.bucketName,
ORIGIN: docker.environment?.VITE_NEXTAUTH_URL || '',
NEXTAUTH_URL: docker.environment?.VITE_NEXTAUTH_URL || '',
DATABASE_FILE_PATH: docker.environment!.DATABASE_URL.replace(
'file:',
''
),
ENABLE_DATABASE_BACKUP: 'true', // this is set to `true` so we can build the container locally without backups auto-enabled
AWS_REGION: process.env.CDK_DEFAULT_REGION as string,
},
enableLogging: true,
secrets,
containerPort: 3000,
},
publicLoadBalancer: true, // needed for bridge to CF
}
)
// grant read/write to bucket for Litestream backups
bucket.grantReadWrite(albFargateService.service.taskDefinition.taskRole)
albFargateService.targetGroup.setAttribute(
'deregistration_delay.timeout_seconds',
'30'
)
albFargateService.targetGroup.configureHealthCheck({
path: '/healthcheck',
interval: cdk.Duration.seconds(5),
healthyHttpCodes: '200',
healthyThresholdCount: 2,
unhealthyThresholdCount: 3,
timeout: cdk.Duration.seconds(4),
})
const volumeName = 'efs-volume'
albFargateService.service.taskDefinition.addVolume({
name: volumeName,
efsVolumeConfiguration: {
fileSystemId: filesystem.fileSystemId,
},
})
const container = albFargateService.service.taskDefinition.findContainer(
docker.name
) as ecs.ContainerDefinition
cdk.Tags.of(container).add(
'app:version',
this.node.tryGetContext('version')
)
// mount the filesystem
container.addMountPoints({
containerPath: filesystemMountPoint,
sourceVolume: volumeName,
readOnly: false,
})
// grant access to the filesystem
filesystem.grant(
container.taskDefinition.taskRole,
'elasticfilesystem:ClientRootAccess',
'elasticfilesystem:ClientWrite',
'elasticfilesystem:ClientMount',
'elasticfilesystem:DescribeMountTargets'
)
// allow inbound connections to the filesystem
filesystem.connections.allowDefaultPortFrom(albFargateService.service)
// allow outbound connections to the filesystem
albFargateService.service.connections.allowTo(filesystem, Port.tcp(2049))
// add WAF to the LoadBalancer
const waf = new WAF(this, 'WAFLoadBalancer', {
name: 'WAFLoadBalancer',
scope: 'REGIONAL',
})
waf.addAssociation(
'WebACLAssociationLoadBalancer',
albFargateService.loadBalancer.loadBalancerArn
)
const xAmzSecurityTokenHeaderName = 'X-HeyAmplify-Security-Token'
const xAmzSecurityTokenHeaderValue = uuid()
const headerAllowlist = [
'X-GitHub-Delivery',
'X-GitHub-Event',
'X-GitHub-Hook-ID',
'X-GitHub-Hook-Installation-Target-ID',
'X-GitHub-Hook-Installation-Target-Type',
'X-Hub-Signature',
'X-Hub-Signature-256',
'X-Auth-Return-Redirect', // support auth.js client-side redirect
]
// set up CloudFront
const distribution = new cloudfront.Distribution(this, 'CFDistribution', {
// domainNames and certificate needed for amplify.aws subdomain (connected to a Route53 hosted zone)
domainNames: subdomain?.domainNames ? subdomain.domainNames : undefined,
certificate: subdomain?.certificate ? subdomain.certificate : undefined,
defaultBehavior: {
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cachePolicy: new cloudfront.CachePolicy(this, 'CachePolicy', {
headerBehavior: cloudfront.CacheHeaderBehavior.allowList(
...headerAllowlist
),
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
cookieBehavior: cloudfront.CacheCookieBehavior.all(),
}),
origin: new origins.LoadBalancerV2Origin(
albFargateService.loadBalancer,
{
customHeaders: {
// send the X-HeyAmplify-Security-Token header to the ALB
[xAmzSecurityTokenHeaderName]: xAmzSecurityTokenHeaderValue,
},
protocolPolicy: cloudfront.OriginProtocolPolicy.HTTP_ONLY,
}
),
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
},
// add Web Application Firewall (WAF)
webAclId: new WAF(this, 'WAFCloudFront', {
name: 'WAFCloudFront',
}).attrArn,
})
for (const listener of albFargateService.loadBalancer.listeners) {
// create listener rule for Security headers
new elb.ApplicationListenerRule(this, 'SecurityListenerRule', {
listener,
priority: 1,
conditions: [
// verify the X-HeyAmplify-Security-Token header is set and valid
elb.ListenerCondition.httpHeader(xAmzSecurityTokenHeaderName, [
xAmzSecurityTokenHeaderValue,
]),
],
targetGroups: [albFargateService.targetGroup],
})
// modify default action to send 403
listener.addAction('default', {
action: elb.ListenerAction.fixedResponse(403, {
messageBody: 'Forbidden',
}),
})
}
// enable access logging for load balancer
albFargateService.loadBalancer.logAccessLogs(bucket, 'alb-access')
// enable deletion protection for load balancer
albFargateService.loadBalancer.setAttribute(
'deletion_protection.enabled',
'true'
)
albFargateService.loadBalancer.applyRemovalPolicy(cdk.RemovalPolicy.RETAIN)
// set up DNS record for the CloudFront distribution if subdomain exists
if (subdomain) {
const record = new route53.ARecord(this, 'AliasRecordApp', {
target: route53.RecordTarget.fromAlias(
new route53Targets.CloudFrontTarget(distribution)
),
zone: subdomain.hostedZone,
})
new route53.CnameRecord(this, 'CnameRecordApp', {
recordName: 'www',
zone: subdomain.hostedZone,
domainName: subdomain.domainName,
})
new cdk.CfnOutput(this, 'HeyAmplifyAppURL', {
value: record.domainName,
})
}
}
}