-
Notifications
You must be signed in to change notification settings - Fork 85
/
deployment-setup.ts
166 lines (158 loc) · 5.88 KB
/
deployment-setup.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
import { App, Duration, Fn, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
import {
aws_cloudwatch as cloudwatch,
aws_codedeploy as codedeploy,
aws_ec2 as ec2,
aws_ecr as ecr,
aws_ecs as ecs,
aws_elasticloadbalancingv2 as elbv2,
aws_ssm as ssm,
} from 'aws-cdk-lib';
interface TriviaDeploymentResourcesStackProps extends StackProps {
infrastructureStackName: string;
}
/**
* Set up the resources needed to do blue-green deployments, including the ECS service and CodeDeploy deployment group.
* This stack is effectively "create-only": once the ECS service is created, it cannot be updated through CloudFormation,
* only through CodeDeploy.
*/
class TriviaDeploymentResourcesStack extends Stack {
constructor(parent: App, name: string, props: TriviaDeploymentResourcesStackProps) {
super(parent, name, props);
// Lookup existing resources
const repo = ecr.Repository.fromRepositoryName(this, 'Repo', 'reinvent-trivia-backend');
const ecsApplication = codedeploy.EcsApplication.fromEcsApplicationName(
this,
'App',
Fn.importValue(props.infrastructureStackName + 'CodeDeployApplication'),
);
const vpcId = ssm.StringParameter.valueFromLookup(this, `/${props.infrastructureStackName}/VPC`);
const vpc = ec2.Vpc.fromLookup(this, 'Vpc', { vpcId });
const cluster = ecs.Cluster.fromClusterAttributes(this, 'Cluster', {
clusterName: 'default',
vpc,
securityGroups: [],
});
const serviceSGId = ssm.StringParameter.valueFromLookup(this, `/${props.infrastructureStackName}/ServiceSecurityGroup`);
const serviceSG = ec2.SecurityGroup.fromLookupById(this, 'ServiceSG', serviceSGId);
const blueTG = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(
this,
'BlueTG',
{targetGroupArn: Fn.importValue(props.infrastructureStackName + 'BlueTargetGroup')},
);
const greenTG = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(
this,
'GreenTG',
{targetGroupArn: Fn.importValue(props.infrastructureStackName + 'GreenTargetGroup')},
);
const lbSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(
this,
'LBSecurityGroup',
Fn.importValue(props.infrastructureStackName + 'LoadBalancerSecurityGroup'),
{ allowAllOutbound: true },
);
const prodListener = elbv2.ApplicationListener.fromApplicationListenerAttributes(
this,
'ProdRoute',
{
listenerArn: Fn.importValue(props.infrastructureStackName + 'ProdTrafficListener'),
securityGroup: lbSecurityGroup,
},
);
const testListener = elbv2.ApplicationListener.fromApplicationListenerAttributes(
this,
'TestRoute',
{
listenerArn: Fn.importValue(props.infrastructureStackName + 'TestTrafficListener'),
securityGroup: lbSecurityGroup,
},
);
const blueUnhealthyHostsAlarm = cloudwatch.Alarm.fromAlarmArn(
this,
'BlueUnhealthyHostsAlarm',
Fn.importValue(props.infrastructureStackName + 'BlueUnhealthyHostsAlarm'),
);
const blueApiFailureAlarm = cloudwatch.Alarm.fromAlarmArn(
this,
'BlueApiFailureAlarm',
Fn.importValue(props.infrastructureStackName + 'BlueApiFailureAlarm'),
);
const greenUnhealthyHostsAlarm = cloudwatch.Alarm.fromAlarmArn(
this,
'GreenUnhealthyHostsAlarm',
Fn.importValue(props.infrastructureStackName + 'GreenUnhealthyHostsAlarm'),
);
const greenApiFailureAlarm = cloudwatch.Alarm.fromAlarmArn(
this,
'GreenApiFailureAlarm',
Fn.importValue(props.infrastructureStackName + 'GreenApiFailureAlarm'),
);
// ECS resources
const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
family: 'trivia-backend',
});
taskDefinition.addContainer('Container', {
containerName: 'web',
image: ecs.ContainerImage.fromEcrRepository(repo, 'latest'),
portMappings: [{
protocol: ecs.Protocol.TCP,
containerPort: 80,
hostPort: 80,
}],
});
const cfnTaskDef = taskDefinition.node.defaultChild as ecs.CfnTaskDefinition;
cfnTaskDef.applyRemovalPolicy(RemovalPolicy.RETAIN, { applyToUpdateReplacePolicy: true });
const service = new ecs.FargateService(this, 'Service', {
serviceName: props.infrastructureStackName,
cluster,
taskDefinition,
securityGroups: [serviceSG],
desiredCount: 3,
deploymentController: {
type: ecs.DeploymentControllerType.CODE_DEPLOY,
},
propagateTags: ecs.PropagatedTagSource.SERVICE,
});
service.attachToApplicationTargetGroup(blueTG);
// CodeDeploy resources
const deploymentConfig = codedeploy.EcsDeploymentConfig.fromEcsDeploymentConfigName(this, 'DC', 'CodeDeployDefault.ECSCanary10Percent5Minutes');
new codedeploy.EcsDeploymentGroup(this, 'DeploymentGroup', {
application: ecsApplication,
deploymentGroupName: 'DgpECS-' + props.infrastructureStackName,
deploymentConfig,
alarms: [
blueUnhealthyHostsAlarm,
blueApiFailureAlarm,
greenUnhealthyHostsAlarm,
greenApiFailureAlarm,
],
service,
blueGreenDeploymentConfig: {
blueTargetGroup: blueTG,
greenTargetGroup: greenTG,
listener: prodListener,
testListener,
terminationWaitTime: Duration.minutes(10),
},
autoRollback: {
stoppedDeployment: true,
},
});
}
}
const app = new App();
new TriviaDeploymentResourcesStack(app, 'TriviaDeploymentResourcesTest', {
infrastructureStackName: 'TriviaBackendTest',
env: { account: process.env['CDK_DEFAULT_ACCOUNT'], region: 'us-east-1' },
tags: {
project: "reinvent-trivia"
}
});
new TriviaDeploymentResourcesStack(app, 'TriviaDeploymentResourcesProd', {
infrastructureStackName: 'TriviaBackendProd',
env: { account: process.env['CDK_DEFAULT_ACCOUNT'], region: 'us-east-1' },
tags: {
project: "reinvent-trivia"
}
});
app.synth();