-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswap.ts
188 lines (175 loc) · 5.36 KB
/
swap.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
import {
aws_codepipeline as codepipeline,
aws_codepipeline_actions as actions,
aws_iam as iam,
aws_logs as logs,
aws_s3 as s3,
aws_stepfunctions as sfn,
RemovalPolicy,
ScopedAws,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { IWorkload } from '../workload';
export interface SwapProps {
projectBaseName: string;
artifactBucket: s3.IBucket;
workload: IWorkload;
deployGreenOutput: codepipeline.Artifact;
}
export class Swap {
public readonly output: codepipeline.Artifact;
public readonly action: actions.StepFunctionInvokeAction;
constructor (scope: Construct, props: SwapProps){
const PROJECT_BASE_NAME = props.projectBaseName;
const artifactBucket = props.artifactBucket;
const ecsCluster = props.workload.ecsCluster;
const ecsService = props.workload.ecsService;
const prodListener = props.workload.prodListener;
const prodListenerRule = props.workload.prodListenerRule;
const testListener = props.workload.testListener;
const testListenerRule = props.workload.testListenerRule;
const ecsTaskRole = props.workload.ecsTaskRole;
const ecsTaskExecutionRole = props.workload.ecsTaskExecutionRole;
const deployGreenOutput = props.deployGreenOutput;
const {
accountId,
partition,
region,
} = new ScopedAws(scope);
// -----------------------------
// Swap Blue/Green
// -----------------------------
const projectName = `${PROJECT_BASE_NAME}-Swap`;
// LogGroup for Step Functions
const logGroup = new logs.LogGroup(scope, 'LogsStateMachineSwap', {
logGroupName: `/aws/statemachine/${projectName}`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS
});
// IAM Policy for Step Functions
const policyDocument = new iam.PolicyDocument({
statements:[
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
's3:PutObject',
's3:GetBucketAcl',
's3:GetBucketLocation'
],
resources: [
artifactBucket.bucketArn,
`${artifactBucket.bucketArn}/*`
]
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents'
],
resources: [
logGroup.logGroupArn,
`${logGroup.logGroupArn}:*`
],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'ecs:CreateTaskSet',
],
resources: ['*'],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'ecs:DeleteTaskSet',
],
resources: [
`arn:${partition}:ecs:${region}:${accountId}:task-set/${ecsCluster.clusterName}/${ecsService.serviceName}/*`,
],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'ecs:UpdateServicePrimaryTaskSet',
],
resources: [
ecsService.serviceArn,
],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'elasticloadbalancing:DeleteTargetGroup',
],
resources: [
`arn:${partition}:elasticloadbalancing:${region}:${accountId}:targetgroup/tg-*/*`,
],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'elasticloadbalancing:DescribeRules',
],
resources: ['*'],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'elasticloadbalancing:ModifyRule',
],
resources: [
prodListener.listenerArn,
prodListenerRule.listenerRuleArn,
testListener.listenerArn,
testListenerRule.listenerRuleArn,
],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'iam:PassRole',
],
resources: [
ecsTaskRole.roleArn,
ecsTaskExecutionRole.roleArn,
],
}),
]
});
// IAM Role for Step Functions
const role = new iam.Role(scope, 'RoleSwap', {
assumedBy: new iam.ServicePrincipal('states.amazonaws.com'),
description: `Role for Step Functions of ${PROJECT_BASE_NAME}`,
inlinePolicies: {
'policy': policyDocument,
},
});
// Step Functions State Machine
const stateMachine = new sfn.StateMachine(scope, 'StateMachineSwap', {
definitionBody: sfn.DefinitionBody.fromFile('./assets/state-machine/swap.asl.json', {}),
role: role,
logs: {
destination: logGroup,
includeExecutionData: true,
level: sfn.LogLevel.ALL
}
});
// Artifact
const output = new codepipeline.Artifact('Swap');
// Action
const action = new actions.StepFunctionInvokeAction({
actionName: 'Swap-BlueGreen',
stateMachine:stateMachine,
stateMachineInput: actions.StateMachineInput.filePath({
artifact: deployGreenOutput,
fileName: '',
location: 'output.json'
}),
output: output,
});
this.output = output;
this.action = action;
}
}