-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudfomation.yaml
266 lines (241 loc) · 9.15 KB
/
cloudfomation.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
AWSTemplateFormatVersion: '2010-09-09'
Description: Lambda for Pipeline Integrations.
Parameters:
IntegrationType:
Type: String
AllowedValues:
- "Bitbucket"
- "GitHub"
Description: Enter Bitbucket or GitHub
IntegrationUser:
NoEcho: true
Type: String
MinLength: 1
Description: Enter Bitbucket or GitHub API username
IntegrationPass:
NoEcho: true
Type: String
MinLength: 1
Description: Enter Bitbucket or GitHub API password
PipelineName:
Type: String
MinLength: 1
Description: Name of CodePipeline pipeline
EncryptionAtRest:
Type: String
Default: "Yes"
AllowedValues:
- "Yes"
- "No"
Description: Enable encryption at rest for SNS topic
Conditions:
UseCMK: !Equals
- !Ref EncryptionAtRest
- "Yes"
Resources:
SNSTopicEncryptionKey:
Type: AWS::KMS::Key
Condition: UseCMK
Properties:
Description: !Sub "CMK for SNS Topic for encryption at rest in ${AWS::StackName}"
KeyPolicy:
Version: '2012-10-17'
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: kms:*
Resource: '*'
- Sid: Allow administration of the key
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:role/Admin'
Action:
- kms:Create*
- kms:Describe*
- kms:Enable*
- kms:List*
- kms:Put*
- kms:Update*
- kms:Revoke*
- kms:Disable*
- kms:Get*
- kms:Delete*
- kms:ScheduleKeyDeletion
- kms:CancelKeyDeletion
Resource: '*'
- Sid: Allow codestar
Effect: Allow
Principal:
Service: "codestar-notifications.amazonaws.com"
Action:
- kms:GenerateDataKey*
- kms:Decrypt
Resource: "*"
Condition:
StringEquals:
"kms:ViaService": !Sub 'sns.${AWS::Region}.amazonaws.com'
PipelineNotificationSNSTopic:
Type: AWS::SNS::Topic
Properties:
KmsMasterKeyId:
Fn::If:
- UseCMK
- !Ref SNSTopicEncryptionKey
- !Ref AWS::NoValue
Subscription:
- Endpoint: !GetAtt PipelineNotificationFunction.Arn
Protocol: "lambda"
PipelineNotificationSNSTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: SnsTopicPolicy
Effect: Allow
Principal:
Service: "codestar-notifications.amazonaws.com"
Action:
- sns:Publish
Resource: !Ref PipelineNotificationSNSTopic
Topics:
- !Ref PipelineNotificationSNSTopic
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action:
- codepipeline:GetPipelineExecution
Resource: arn:aws:codepipeline:*:*:*
PipelineNotificationRule:
Type: 'AWS::CodeStarNotifications::NotificationRule'
Properties:
Name: !Sub 'Notification for ${PipelineName}'
DetailType: FULL
Resource: !Sub 'arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${PipelineName}'
EventTypeIds:
- codepipeline-pipeline-pipeline-execution-failed
- codepipeline-pipeline-pipeline-execution-canceled
- codepipeline-pipeline-pipeline-execution-started
- codepipeline-pipeline-pipeline-execution-resumed
- codepipeline-pipeline-pipeline-execution-succeeded
- codepipeline-pipeline-pipeline-execution-superseded
Targets:
- TargetType: SNS
TargetAddress: !Ref PipelineNotificationSNSTopic
SnsPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt PipelineNotificationFunction.Arn
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Ref PipelineNotificationSNSTopic
PipelineNotificationFunction:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.8
Handler: index.lambda_handler
Role: !GetAtt LambdaExecutionRole.Arn
Timeout: 30
Environment:
Variables:
INTEGRATION_AUTH_USER: !Ref IntegrationUser
INTEGRATION_AUTH_PASS: !Ref IntegrationPass
INTEGRATION_TYPE: !Ref IntegrationType
Code:
ZipFile: |
from __future__ import print_function
import json
import boto3
import os
import urllib3
from base64 import b64encode
codepipeline_client = boto3.client('codepipeline')
integration_user = os.environ['INTEGRATION_AUTH_USER']
integration_pass = os.environ['INTEGRATION_AUTH_PASS']
integration_type = os.environ['INTEGRATION_TYPE']
user_pass = integration_user+":"+integration_pass
region = os.environ['AWS_REGION']
def lambda_handler(event, context):
message = event['Records'][0]['Sns']['Message']
data = json.loads(message)
print (data)
#Push only notifications about Pipeline Execution State Changes
if data.get("detailType") != "CodePipeline Pipeline Execution State Change":
return()
response = codepipeline_client.get_pipeline_execution(
pipelineName=data['detail']['pipeline'],
pipelineExecutionId=data['detail']['execution-id']
)
print(response)
short_commit_od = response['pipelineExecution']['artifactRevisions'][0]['revisionId'][0:7]
commit_id = response['pipelineExecution']['artifactRevisions'][0]['revisionId']
revision_url = response['pipelineExecution']['artifactRevisions'][0]['revisionUrl']
if "FullRepositoryId=" in revision_url:
repo_id = revision_url.split("FullRepositoryId=")[1].split("&")[0]
else: #gitbub v1 integration
repo_id = revision_url.split("/")[3] + "/" + revision_url.split("/")[4]
if integration_type == "Bitbucket":
#Based on https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/
if data['detail']['state'].upper() in [ "SUCCEEDED" ]:
state = "SUCCESSFUL"
elif data['detail']['state'].upper() in [ "STARTED", "STOPPING", "STOPPED", "SUPERSEDED" ]:
state = "INPROGRESS"
else:
state = "FAILED"
url = "https://api.bitbucket.org/2.0/repositories/" + repo_id + "/commit/" + commit_id + "/statuses/build"
build_status={}
build_status['key'] = data['detail']['execution-id']
build_status['state'] = state
build_status['name'] = "CodePipeline: " + data['detail']['pipeline']
build_status['url'] = "https://" + region + ".console.aws.amazon.com/codesuite/codepipeline/pipelines/" + data['detail']['pipeline'] + "/executions/" + data['detail']['execution-id'] + "?region="+region
elif integration_type == "GitHub":
#Based on https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#statuses
if data['detail']['state'].upper() in [ "SUCCEEDED" ]:
state = "success"
elif data['detail']['state'].upper() in [ "STARTED", "STOPPING", "STOPPED", "SUPERSEDED" ]:
state = "pending"
else:
state = "error"
url = "https://api.github.com/repos/" + repo_id + "/statuses/" + commit_id
build_status={}
build_status['state'] = state
build_status['context'] = "CodePipeline"
build_status['description'] = data['detail']['pipeline']
build_status['target_url'] = "https://" + region + ".console.aws.amazon.com/codesuite/codepipeline/pipelines/" + data['detail']['pipeline'] + "/executions/" + data['detail']['execution-id'] + "?region="+region
else:
return()
encode_user_pass = b64encode(user_pass.encode()).decode()
http = urllib3.PoolManager()
r = http.request('POST', url,
headers={'Accept': 'application/json', 'Content-Type': 'application/json', 'User-Agent': 'Curl/0.1', 'Authorization' : 'Basic %s' % encode_user_pass},
body=json.dumps(build_status).encode('utf-8')
)
print(r.data)
return message
Outputs:
SnsTopicArn:
Value: !Ref PipelineNotificationSNSTopic