-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.yaml
More file actions
237 lines (214 loc) · 7.49 KB
/
Setup.yaml
File metadata and controls
237 lines (214 loc) · 7.49 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
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
# Copyright 2019 Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the 'License').
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/asl/
#
# or in the 'license' file accompanying this file. This file is distributed
# on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
AWSTemplateFormatVersion: "2010-09-09"
Description: "Create Lambda Function"
Parameters:
MyGitToken:
Type: String
NoEcho: true
Description: Your Git Personal Access Token
Resources:
Secret:
Type: AWS::SecretsManager::Secret
Properties:
Name: MyGitToken
SecretString: !Ref MyGitToken
#----------------------------------------------------------------------#
# Role for lambda execution
#----------------------------------------------------------------------#
LambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName: LambdaRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSLambdaFullAccess
- arn:aws:iam::aws:policy/AWSCloudFormationFullAccess
- arn:aws:iam::aws:policy/AWSCodePipelineFullAccess
- arn:aws:iam::aws:policy/SecretsManagerReadWrite
Path: /
#----------------------------------------------------------------------#
# Role for Pipeline Execution
#----------------------------------------------------------------------#
CodePipelineRole:
Type: AWS::IAM::Role
Properties:
RoleName: CodePipelineRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSCodePipelineFullAccess
- arn:aws:iam::aws:policy/AWSCodeCommitFullAccess
- arn:aws:iam::aws:policy/AWSCodeBuildDeveloperAccess
- arn:aws:iam::aws:policy/AmazonS3FullAccess
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
Action:
- 'sts:AssumeRole'
#----------------------------------------------------------------------#
# Role for CodeBuild Execution
#----------------------------------------------------------------------#
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
RoleName: CodeBuildRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Action:
- 'sts:AssumeRole'
#----------------------------------------------------------------------#
# S3 Bucket to store template
#----------------------------------------------------------------------#
TemplateBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Sub ${AWS::AccountId}-cfn
TemplateBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref TemplateBucket
PolicyDocument:
Statement:
-
Action:
- s3:*
Effect: Allow
Resource:
- !Sub arn:aws:s3:::${TemplateBucket}
- !Sub arn:aws:s3:::${TemplateBucket}/*
Principal:
AWS:
- !Sub arn:aws:iam::${AWS::AccountId}:root
#----------------------------------------------------------------------#
# Lambda for Stack Creation
#----------------------------------------------------------------------#
CreatePipeline:
DependsOn: LambdaRole
Type: "AWS::Lambda::Function"
Properties:
FunctionName: CreatePipeline
Handler: "index.lambda_handler"
Role: !GetAtt LambdaRole.Arn
Runtime: "python3.6"
Timeout: 25
Code:
ZipFile: |
import boto3
import json
import os
def lambda_handler(event, context):
print('## ENVIRONMENT VARIABLES')
print(os.environ)
print('## EVENT')
print(event)
data = json.loads(event['body'])
EventType = event['headers']['X-GitHub-Event']
NewBranch = data['ref']
RefType = data['ref_type']
Account = (data['repository']['owner']['login'])
RepositoryName = (data['repository']['name'])
AccountID = boto3.client('sts').get_caller_identity()['Account']
print('## VARIABLES')
print('EventType:', EventType)
print('NewBranch:', NewBranch)
print('RefType:', RefType)
print('Account:', Account)
print('RepositoryName:', RepositoryName)
print('AccountID:', AccountID)
if NewBranch == "master":
quit()
if EventType == "create":
cf_client = boto3.client('cloudformation')
cf_client.create_stack(
StackName= f'Pipeline-{RepositoryName}-{NewBranch}',
TemplateURL= f'https://s3.amazonaws.com/{AccountID}-cfn/TemplatePipeline.yaml',
Parameters=[
{
'ParameterKey': 'RepositoryName',
'ParameterValue': RepositoryName,
'UsePreviousValue': False
},
{
'ParameterKey': 'BranchName',
'ParameterValue': NewBranch,
'UsePreviousValue': False
}
],
OnFailure='ROLLBACK',
Capabilities=['CAPABILITY_NAMED_IAM']
)
else:
cf_client = boto3.client('cloudformation')
cf_client.delete_stack(
StackName= f'Pipeline-{RepositoryName}-{NewBranch}'
)
RestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Description: API used by the AWS CodePipeline integration with GitHub
EndpointConfiguration:
Types:
- REGIONAL
Name: CodePipeline-GitHub-Integration
RestApiMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: POST
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${CreatePipeline.Arn}/invocations'
ResourceId: !GetAtt
- RestApi
- RootResourceId
RestApiId: !Ref RestApi
RestApiDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: RestApiMethod
Properties:
Description: Stage Deployment
RestApiId: !Ref RestApi
StageName: prod
LambdaPermissionAPIGw:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref CreatePipeline
Principal: apigateway.amazonaws.com
Outputs:
Endpoint:
Value: !Sub "https://${RestApi}.execute-api.${AWS::Region}.amazonaws.com/prod/"
S3Bucket:
Value: !Ref TemplateBucket