-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteg.codepipeline.ts
More file actions
201 lines (185 loc) · 6.46 KB
/
Copy pathinteg.codepipeline.ts
File metadata and controls
201 lines (185 loc) · 6.46 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
import cdk = require("aws-cdk-lib");
import { Construct } from 'constructs';
import codecommit = require("aws-cdk-lib/aws-codecommit");
import codebuild = require("aws-cdk-lib/aws-codebuild");
import codepipeline = require("aws-cdk-lib/aws-codepipeline");
import codepipeline_actions = require("aws-cdk-lib/aws-codepipeline-actions");
import iam = require("aws-cdk-lib/aws-iam");
import ecr = require("aws-cdk-lib/aws-ecr");
import sns = require("aws-cdk-lib/aws-sns");
import sns_subscriptions = require("aws-cdk-lib/aws-sns-subscriptions");
import targets = require("aws-cdk-lib/aws-events-targets");
import yaml = require('js-yaml');
import fs = require('fs');
export interface EksIntegCodePipelineDeployStackProps extends cdk.StackProps {
readonly name?: string;
readonly codecommit_repo?: string;
readonly codecommit_branch?: string;
readonly codebuild_project?: string;
readonly codepipeline_name?: string;
readonly notifications_email?: string;
}
export class EksIntegCodePipelineDeployStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: EksIntegCodePipelineDeployStackProps) {
super(scope, id, props);
const name = 'game-2048'
// -------------------------------------------------------------
// ### buildspec.yml
// version: 0.2
// env:
// variables:
// ECR_URI: "<your-ecr-repo-uri>"
// phases:
// install:·
// runtime-versions:·
// docker: 18
// build:
// commands:
// - $(aws ecr get-login --no-include-email --region us-east-1)
// - docker build -t ecr-image-sample:latest .
// - docker tag ecr-image-sample:latest ${ECR_URI}:latest
// - docker push $ECR_URI
// artifacts:
// files:
// - '**/*'
// -------------------------------------------------------------
// ### Dockerfile
// FROM python:3.7-alpine
// COPY . /srv
// RUN pip install -r /srv/requirements.txt
// CMD [ "sh", "-c", "python /srv/app.py" ]
// EXPOSE 80
// -------------------------------------------------------------
// ### app.py
// from flask import Flask
//
// app = Flask(__name__)
//
// @app.route('/')
// def hello_world():
// return 'Hello, World!'
//
// if __name__ == "__main__":
// app.run(host='0.0.0.0', port=80, threaded=True)
// -------------------------------------------------------------
const ecrRepository = new ecr.Repository(this, "image", {
repositoryName: 'amazon-eks-' + name
});
/**
* CodeCommit: create repository
**/
const codecommitRepository = new codecommit.Repository(this, "source", {
repositoryName: name
});
const kubectlExecutionRole = iam.Role.fromRoleArn(this, 'amazon-eks-kubectl-role', "arn:aws:iam::" + this.account + ":role/AmazonEksKubectlRole")
/**
* CodeBuild:
* 1. create codebuild project
* 2. create policy of ECR and Codecommit
**/
const codebuildProject = new codebuild.PipelineProject(this, "build", {
projectName: name,
role: kubectlExecutionRole,
buildSpec: codebuild.BuildSpec.fromObject(
yaml.load(
fs.readFileSync(
'samples/src/kubernetes/deploy-eks-game2048.yml',
'utf8'
)
) as Record<string, any>[]
),
environment: {
computeType: codebuild.ComputeType.SMALL,
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_3,
privileged: true,
environmentVariables: {
AWS_ACCOUNT_ID: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: cdk.Aws.ACCOUNT_ID
},
IMAGE_URI: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: ecrRepository.repositoryUri
},
EKS_CLUSTER_NAME: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: 'eks-sample'
}
}
}
});
codecommitRepository.onCommit('OnCommit', {
target: new targets.CodeBuildProject(codebuildProject),
});
ecrRepository.grantPullPush(codebuildProject.role!);
/**
* CodePipeline:
* 1. create codebuild project
* 2. create policy of ECR and Codecommit
**/
// trigger of `CodeCommitTrigger.POLL`
const sourceOutput = new codepipeline.Artifact();
const sourceAction = new codepipeline_actions.CodeCommitSourceAction({
actionName: "Source-CodeCommit",
branch: props.codecommit_branch ?? 'main',
trigger: codepipeline_actions.CodeCommitTrigger.POLL,
repository: codecommitRepository,
output: sourceOutput
});
// when codecommit input then action of codebuild
const buildOutput = new codepipeline.Artifact();
const buildAction = new codepipeline_actions.CodeBuildAction({
actionName: "Build",
input: sourceOutput,
outputs: [
buildOutput
],
project: codebuildProject
});
// create pipeline, and then add both codecommit and codebuild
const pipeline = new codepipeline.Pipeline(this, "pipeline", {
pipelineName: props.codepipeline_name ?? name + '-pipeline'
});
pipeline.addStage({
stageName: "Source",
actions: [sourceAction]
});
pipeline.addStage({
stageName: "Build",
actions: [buildAction]
});
/**
* SNS: Monitor pipeline state change then notifiy
**/
if ( props.notifications_email ) {
const pipelineSnsTopic = new sns.Topic(this, 'pipeline-stage-change');
pipelineSnsTopic.addSubscription(new sns_subscriptions.EmailSubscription(props.notifications_email))
pipeline.onStateChange("PipelineStateChange", {
target: new targets.SnsTopic(pipelineSnsTopic),
description: 'Listen for codepipeline change events',
eventPattern: {
detail: {
state: [ 'FAILED', 'SUCCEEDED', 'STOPPED' ]
}
}
});
}
/**
* Output:
* - CodeCommit clone path of HTTP and SSH
* - ECR Repository URI
**/
new cdk.CfnOutput(this, 'CodeCommitCloneUrlHttp', {
description: 'CodeCommit Repo CloneUrl HTTP',
value: codecommitRepository.repositoryCloneUrlHttp
});
new cdk.CfnOutput(this, 'CodeCommitCloneUrlSsh', {
description: 'CodeCommit Repo CloneUrl SSH',
value: codecommitRepository.repositoryCloneUrlSsh
});
new cdk.CfnOutput(this, 'EcrRepositoryUri', {
description: 'ECR Repository URI',
value: ecrRepository.repositoryUri
});
}
}