/
index.ts
57 lines (47 loc) · 1.5 KB
/
index.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
import ecs = require('@aws-cdk/aws-ecs');
import ec2 = require('@aws-cdk/aws-ec2');
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import cdk = require('@aws-cdk/core');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-ecs-integ-ecs');
// Create a cluster
const vpc = new ec2.Vpc(stack, 'Vpc', { maxAZs: 2 });
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO)
});
// Create Task Definition
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');
const container = taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
memoryLimitMiB: 256,
});
container.addPortMappings({
containerPort: 80,
hostPort: 8080,
protocol: ecs.Protocol.TCP
});
// Create Service
const service = new ecs.Ec2Service(stack, "Service", {
cluster,
taskDefinition,
});
// Create ALB
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', {
vpc,
internetFacing: true
});
const listener = lb.addListener('PublicListener', { port: 80, open: true });
// Attach ALB to ECS Service
listener.addTargets('ECS', {
port: 80,
targets: [service],
// include health check (default is none)
healthCheck: {
interval: cdk.Duration.seconds(60),
path: "/health",
timeout: cdk.Duration.seconds(5),
}
});
new cdk.CfnOutput(stack, 'LoadBalancerDNS', { value: lb.loadBalancerDnsName, });
app.synth();