-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.ts
85 lines (78 loc) · 2.24 KB
/
cluster.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
import * as cdk from '@aws-cdk/core'
import * as ec2 from '@aws-cdk/aws-ec2'
import * as ecs from '@aws-cdk/aws-ecs'
import * as iam from '@aws-cdk/aws-iam'
import * as log from '@aws-cdk/aws-logs'
import * as path from 'path'
export default (stack: cdk.Construct) => {
//
//
const vpc = new ec2.Vpc(stack, 'Vpc', {
maxAzs: 2,
natGateways: 1,
})
//
// execution role is required to bootstrap cluster operations
// see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html
const executionRole = new iam.Role(stack, 'EcsBootRole', {
assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com')
})
executionRole.addToPolicy(
new iam.PolicyStatement({
resources: ['*'],
actions: [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents",
],
})
)
//
//
const taskRole = new iam.Role(stack, 'EcsTaskRole', {
assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com')
})
taskRole.addToPolicy(
new iam.PolicyStatement({
notResources: ['*'],
notActions: ['*'],
})
)
//
//
const memoryLimitMiB = 512
const cluster = new ecs.Cluster(stack, 'Ecs', { vpc })
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'EcsTask', {
memoryLimitMiB,
executionRole,
taskRole,
})
const container = taskDefinition.addContainer('blueprint-cdk-fargate', {
image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, '../docker')),
memoryLimitMiB,
logging: ecs.LogDriver.awsLogs({
streamPrefix: 'blueprint-cdk-fargate',
logRetention: log.RetentionDays.ONE_DAY,
}),
})
container.addPortMappings({ containerPort: 8080 })
const service = new ecs.FargateService(stack, 'Service', {
cluster,
assignPublicIp: true,
desiredCount: 1,
taskDefinition,
serviceName: 'blueprint-cdk-fargate',
vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
})
service.connections.allowFromAnyIpv4(
new ec2.Port({
protocol: ec2.Protocol.TCP,
fromPort: 8080,
toPort: 8080,
stringRepresentation: 'http-alt'
})
)
}