-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathSetup.yaml
198 lines (181 loc) · 6.31 KB
/
Setup.yaml
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
# 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"
Resources:
#----------------------------------------------------------------------#
# EventRule to trigger CreatePipeline lambda
#----------------------------------------------------------------------#
CreatePipelineRule:
Type: AWS::Events::Rule
Properties:
Description: "EventRule"
EventPattern:
source:
- aws.codecommit
detail-type:
- 'CodeCommit Repository State Change'
detail:
event:
- referenceDeleted
- referenceCreated
referenceType:
- branch
State: ENABLED
Targets:
- Arn: !GetAtt CreatePipeline.Arn
Id: CreatePipeline
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref CreatePipeline
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- CreatePipelineRule
- Arn
#----------------------------------------------------------------------#
# 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
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}-templates
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
def lambda_handler(event, context):
Region=event['region']
Account = event['account']
RepositoryName = event['detail']['repositoryName']
NewBranch = event['detail']['referenceName']
Event = event['detail']['event']
if NewBranch == "master":
quit()
if Event == "referenceCreated":
cf_client = boto3.client('cloudformation')
cf_client.create_stack(
StackName= f'Pipeline-{RepositoryName}-{NewBranch}',
TemplateURL= f'https://s3.amazonaws.com/{Account}-templates/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}'
)