/
deploy-ecs-with-ec2-stack.ts
99 lines (91 loc) · 2.85 KB
/
deploy-ecs-with-ec2-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
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
export interface DeployEc2WithFargateStackProps extends cdk.StackProps {
ecrName: string;
imageTag: string;
scope: string;
}
export class DeployEcsWithEc2Stack extends cdk.Stack {
constructor(
scope: Construct,
id: string,
props: DeployEc2WithFargateStackProps
) {
super(scope, id, props);
const ecr = cdk.aws_ecr.Repository.fromRepositoryArn(
this,
"ecr",
`arn:aws:ecr:${props.env!.region!}:${props.env!.account!}:repository/${
props.ecrName
}`
);
const vpc = new cdk.aws_ec2.Vpc(this, "vpc", {
ipAddresses: cdk.aws_ec2.IpAddresses.cidr(
cdk.aws_ec2.Vpc.DEFAULT_CIDR_RANGE
),
enableDnsHostnames: true,
enableDnsSupport: true,
defaultInstanceTenancy: cdk.aws_ec2.DefaultInstanceTenancy.DEFAULT,
availabilityZones: [`${props.env!.region!}a`],
natGateways: 0,
subnetConfiguration: [
{
cidrMask: 16,
name: `ecsWithEc2-${props.scope}`,
subnetType: cdk.aws_ec2.SubnetType.PUBLIC,
},
],
});
const cluster = new cdk.aws_ecs.Cluster(this, "cluster", {
clusterName: `ecsWithEc2Cluster-${props.scope}`,
vpc: vpc,
capacity: {
instanceType: cdk.aws_ec2.InstanceType.of(
cdk.aws_ec2.InstanceClass.T2,
cdk.aws_ec2.InstanceSize.MICRO
),
},
});
const ec2TaskDef = new cdk.aws_ecs.Ec2TaskDefinition(
this,
"ec2TaskDefinition",
{
networkMode: cdk.aws_ecs.NetworkMode.HOST,
family: `ecsWithEc2Family-${props.scope}`,
}
);
ec2TaskDef.addContainer("apiContainer", {
image: cdk.aws_ecs.ContainerImage.fromEcrRepository(ecr, props.imageTag),
containerName: `container-${props.scope}`,
disableNetworking: false,
startTimeout: cdk.Duration.minutes(2),
essential: true,
memoryLimitMiB: 512,
logging: cdk.aws_ecs.LogDrivers.awsLogs({
streamPrefix: `ecsWithEc2ApiLogs-${props.scope}`,
logGroup: new cdk.aws_logs.LogGroup(this, "logGroup", {
logGroupName: `/ecs-with-ec2-api/${props.scope}`,
retention: cdk.aws_logs.RetentionDays.ONE_DAY,
removalPolicy: cdk.RemovalPolicy.DESTROY,
}),
}),
portMappings: [
{
containerPort: 8080,
},
],
});
const ec2Service = new cdk.aws_ecs.Ec2Service(this, "ec2Service", {
taskDefinition: ec2TaskDef,
cluster: cluster,
desiredCount: 1,
serviceName: `ec2Service-${props.scope}`,
});
ec2Service.connections.allowFromAnyIpv4(cdk.aws_ec2.Port.allTcp());
new cdk.CfnOutput(this, "clusterArn", {
description: "The ARN of the Fargate Cluster",
value: cluster.clusterArn,
exportName: `ecsClusterArn-${props.scope}`,
});
}
}