-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_pipeline.yml
250 lines (242 loc) · 7.85 KB
/
template_pipeline.yml
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation Template of Pipeline
Parameters:
Owner:
Type: String
Repo:
Type: String
OAuthToken:
Type: String
NoEcho: true
Branch:
Type: String
ModuleName:
Type: String
ModuleStackName:
Type: String
PackagedTemplateFilePath:
Type: String
Default: packaged.yml
DeployParamFile:
Type: String
Default: param.json
BuildSpec:
Type: String
Default: pipeline_settings/buildspec.yml
Resources:
ArtifactStoreBucket:
Type: AWS::S3::Bucket
BuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: !Ref ModuleName
ServiceRole: !GetAtt CodeBuildRole.Arn
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/ubuntu-base:14.04
EnvironmentVariables:
- Name: PACKAGED_TEMPLATE_FILE_PATH
Value: !Ref PackagedTemplateFilePath
- Name: S3_BUCKET
Value: !Ref ArtifactStoreBucket
Source:
Type: CODEPIPELINE
BuildSpec: !Ref BuildSpec
PipelineDeployRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: cloudformation.amazonaws.com
Path: /
Policies:
- PolicyName: !Sub ${ModuleName}DeployPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- dynamodb:*
Resource: !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/*
- Effect: Allow
Action:
- lambda:*
Resource: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:*
- Effect: Allow
Action:
- iam:CreateRole
- iam:DeleteRole
- iam:GetRole
- iam:PassRole
- iam:DeleteRolePolicy
- iam:PutRolePolicy
- iam:GetRolePolicy
Resource: !Sub arn:aws:iam::${AWS::AccountId}:role/*
- Effect: Allow
Action: s3:GetObject
Resource:
- !Sub arn:aws:s3:::${ArtifactStoreBucket}
- !Sub arn:aws:s3:::${ArtifactStoreBucket}/*
PipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codepipeline.amazonaws.com
Path: /
Policies:
- PolicyName: CodePipelineAccess
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: S3GetObject
Effect: Allow
Action: s3:*
Resource:
- !Sub arn:aws:s3:::${ArtifactStoreBucket}
- !Sub arn:aws:s3:::${ArtifactStoreBucket}/*
- Sid: S3PutObject
Effect: Allow
Action: s3:*
Resource:
- !Sub arn:aws:s3:::${ArtifactStoreBucket}
- !Sub arn:aws:s3:::${ArtifactStoreBucket}/*
- Sid: CodeBuildStartBuild
Effect: Allow
Action:
- codebuild:StartBuild
- codebuild:BatchGetBuilds
Resource: !Sub arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/${ModuleName}
- Sid: CFnActions
Effect: Allow
Action:
- cloudformation:DescribeStacks
- cloudformation:DescribeChangeSet
- cloudformation:CreateChangeSet
- cloudformation:ExecuteChangeSet
- cloudformation:DeleteChangeSet
Resource:
- !Sub arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${ModuleStackName}/*
- Sid: PassRole
Effect: Allow
Action:
- iam:PassRole
Resource: !GetAtt PipelineDeployRole.Arn
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Path: /
Policies:
- PolicyName: CodeBuildAccess
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: CloudWatchLogsAccess
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
- Sid: S3Access
Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
- s3:GetObjectVersion
Resource:
- !Sub arn:aws:s3:::${ArtifactStoreBucket}
- !Sub arn:aws:s3:::${ArtifactStoreBucket}/*
- Sid: CloudFormationAccess
Effect: Allow
Action: cloudformation:ValidateTemplate
Resource: "*"
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: !Sub pipeline-${ModuleName}
RoleArn: !GetAtt PipelineRole.Arn
ArtifactStore:
Type: S3
Location: !Ref ArtifactStoreBucket
Stages:
- Name: Source
Actions:
- Name: DownloadSource
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
Configuration:
Owner: !Ref Owner
Repo: !Ref Repo
Branch: !Ref Branch
OAuthToken: !Ref OAuthToken
OutputArtifacts:
- Name: SourceOutput
- Name: Build
Actions:
- InputArtifacts:
- Name: SourceOutput
Name: Package
ActionTypeId:
Category: Build
Provider: CodeBuild
Owner: AWS
Version: 1
OutputArtifacts:
- Name: BuildOutput
Configuration:
ProjectName: !Ref BuildProject
- Name: Deploy
Actions:
- Name: CreateChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
InputArtifacts:
- Name: BuildOutput
Configuration:
ActionMode: CHANGE_SET_REPLACE
RoleArn: !GetAtt PipelineDeployRole.Arn
StackName: !Ref ModuleStackName
ChangeSetName: !Sub ${ModuleStackName}-changeset
Capabilities: CAPABILITY_NAMED_IAM
TemplatePath: !Sub BuildOutput::${PackagedTemplateFilePath}
TemplateConfiguration: !Sub BuildOutput::${DeployParamFile}
RunOrder: '1'
- Name: ExecuteChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
InputArtifacts:
- Name: BuildOutput
Configuration:
ActionMode: CHANGE_SET_EXECUTE
ChangeSetName: !Sub ${ModuleStackName}-changeset
StackName: !Ref ModuleStackName
RunOrder: '2'