-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecs-codedeploy-stack.ts
144 lines (131 loc) · 5.88 KB
/
ecs-codedeploy-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
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
import { Stack, StackProps } from 'aws-cdk-lib';
import * as cdk from 'aws-cdk-lib';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';
import * as codecommit from 'aws-cdk-lib/aws-codecommit';
import * as codepipeline_actions from 'aws-cdk-lib/aws-codepipeline-actions';
import { Construct } from 'constructs';
import { CLUSTER_NAME } from '../../ecs-fargate-cluster/lib/cluster-config';
import { SSM_PREFIX } from '../../config';
import { IBaseService } from 'aws-cdk-lib/aws-ecs';
export interface EcsCodeDeployStackProps extends StackProps {
stage: string;
serviceName: string;
}
/**
* SSM parameters:
* /cdk-ecs-fargate/ecr-repo-arn
* /cdk-ecs-fargate/ecr-repo-name
* /cdk-ecs-fargate/cluster-securitygroup-id
* /cdk-ecs-fargate/cluster-name
* /cdk-ecs-fargate/codecommit-arn
*/
export class EcsCodeDeployStack extends Stack {
constructor(scope: Construct, id: string, props: EcsCodeDeployStackProps) {
super(scope, id, props);
const ecrRepo = ecr.Repository.fromRepositoryAttributes(this, 'ecr-repo', {
repositoryArn: cdk.Lazy.string({ produce: () => ssm.StringParameter.valueFromLookup(this, `${SSM_PREFIX}/ecr-repo-arn`) }),
repositoryName: ssm.StringParameter.valueFromLookup(this, `${SSM_PREFIX}/ecr-repo-name`)
});
const vpcId = this.node.tryGetContext('vpcId') || ssm.StringParameter.valueFromLookup(this, `${SSM_PREFIX}/vpc-id`);
const vpc = ec2.Vpc.fromLookup(this, 'vpc', { vpcId });
const clusterSgId = ssm.StringParameter.valueFromLookup(this, `${SSM_PREFIX}/cluster-securitygroup-id`);
const ecsSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(this, 'ecs-security-group', clusterSgId);
const cluster = ecs.Cluster.fromClusterAttributes(this, 'ecs-fargate-cluster', {
clusterName: `${CLUSTER_NAME}-${props.stage}`,
vpc,
securityGroups: [ecsSecurityGroup]
});
const service = ecs.FargateService.fromFargateServiceAttributes(this, 'fargate-cluster', {
cluster,
serviceName: cdk.Lazy.string({ produce: () => ssm.StringParameter.valueFromLookup(this, `${SSM_PREFIX}/cluster-name`) })
}) as IBaseService;
const serviceName = props.serviceName;
const repository = codecommit.Repository.fromRepositoryArn(this, `${serviceName}-codecommit-arn`,
cdk.Lazy.string({ produce: () => ssm.StringParameter.valueFromLookup(this, `${SSM_PREFIX}/codecommit-arn`) }));
/**
* buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_4,
* buildImage: codebuild.LinuxBuildImage.fromDockerRegistry('public.ecr.aws/h1a5s9h8/alpine:latest')
*/
const project = new codebuild.Project(this, `cb-project-${serviceName}`, {
projectName: `${serviceName}-build`,
source: codebuild.Source.codeCommit({ repository }),
environment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_6_0,
privileged: true
},
buildSpec: codebuild.BuildSpec.fromSourceFilename('./buildspec.yaml'),
badge: true,
environmentVariables: {
'ACCOUNT_ID': {
value: props?.env?.account
},
'CLUSTER_NAME': {
value: `${service.cluster.clusterName}`
},
'SERVICE_NAME': {
value: `${service.serviceName}`
},
'ECR_REPO_URI': {
value: `${ecrRepo.repositoryUri}`
}
}
});
ecrRepo.grantPullPush(project.role!);
project.role?.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEC2ContainerRegistryPowerUser'));
const sourceOutput = new codepipeline.Artifact();
const buildOutput = new codepipeline.Artifact();
/**
* aws secretsmanager create-secret --name '/github/token' --secret-string {your-token}
* or set the token with oauthToken: cdk.SecretValue.plainText('<plain-text>'),
*/
const sourceAction = new codepipeline_actions.GitHubSourceAction({
actionName: 'GitHub_Source',
owner: 'engel80',
repo: 'fargate-restapi-local',
branch: 'master',
oauthToken: cdk.SecretValue.secretsManager("/github/token"),
output: sourceOutput
});
const buildAction = new codepipeline_actions.CodeBuildAction({
actionName: 'CodeBuild',
project,
input: sourceOutput,
outputs: [buildOutput],
});
const manualApprovalAction = new codepipeline_actions.ManualApprovalAction({
actionName: 'Approve',
});
const deployAction = new codepipeline_actions.EcsDeployAction({
actionName: 'DeployAction',
service: service,
imageFile: new codepipeline.ArtifactPath(buildOutput, `imagedefinitions.json`)
});
new codepipeline.Pipeline(this, 'ecs-deploy-pipeline', {
pipelineName: `ecs-deploy-${service.serviceName}`,
stages: [
{
stageName: 'Source',
actions: [sourceAction],
},
{
stageName: 'Build',
actions: [buildAction],
},
{
stageName: 'Approve',
actions: [manualApprovalAction],
},
{
stageName: 'Deploy',
actions: [deployAction],
}
]
});
}
}