Skip to content

Commit 9425323

Browse files
authored
Merge pull request #9 from toricls/cloudwatch-events
Replace Step Functions with CloudWatch Events
2 parents e38a5b2 + 8e4ed5c commit 9425323

File tree

16 files changed

+79
-564
lines changed

16 files changed

+79
-564
lines changed

sam.yml

+30-73
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ Parameters:
1919
Type: String
2020

2121
Resources:
22-
# AWS SAM doesn't support `Transform` in nested templates, we includes all children into main template
23-
# see https://github.com/awslabs/serverless-application-model/issues/90
2422
##########################
25-
# SNSStack
23+
# SNS
2624
##########################
2725
GitHubEventSNSTopic:
2826
Type: "AWS::SNS::Topic"
2927
##########################
30-
# IAMStack
28+
# IAM
3129
##########################
3230
GitHubIAMUser:
3331
Type: "AWS::IAM::User"
@@ -44,7 +42,7 @@ Resources:
4442
Properties:
4543
UserName: !Ref GitHubIAMUser
4644
##########################
47-
# GitHubWebhookStack
45+
# GitHubWebhook(CustomResource)
4846
##########################
4947
GitHubWebhookCustomResourceRole:
5048
Type: "AWS::IAM::Role"
@@ -87,7 +85,7 @@ Resources:
8785
Type: "Custom::GitHubWebhook"
8886
Properties:
8987
ServiceToken: !GetAtt GitHubWebhookCustomResource.Arn
90-
# Define all variables to re-create via `make deploy` when parameters have changed
88+
# Define all variables to re-create GitHub's webhook configuration via `make deploy` when parameters have changed
9189
GITHUB_TOKEN: !Ref GitHubPersonalAccessToken
9290
GITHUB_REPOSITORY_URL: !Ref GitHubRepositoryUrl
9391
GITHUB_TARGET_RESOURCE: !Ref GitHubTargetResource
@@ -96,7 +94,7 @@ Resources:
9694
SNS_REGION: !Ref "AWS::Region"
9795
SNS_TOPIC: !Ref GitHubEventSNSTopic
9896
##########################
99-
# LambdaStack
97+
# Lambda (BuildStateNotifier)
10098
##########################
10199
LambdaExecutionRole:
102100
Type: "AWS::IAM::Role"
@@ -118,53 +116,36 @@ Resources:
118116
- "logs:CreateLogStream"
119117
- "logs:PutLogEvents"
120118
Resource: "arn:aws:logs:*:*:*"
121-
- Effect: Allow
122-
Action:
123-
- "codebuild:StartBuild"
124-
- "codebuild:BatchGetBuilds"
125-
Resource: !Sub "arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/${CodeBuildProjectName}"
126-
BuildDispatcher:
119+
BuildStateNotifier:
127120
Type: "AWS::Serverless::Function"
128121
Properties:
129122
Role: !GetAtt LambdaExecutionRole.Arn
130123
Handler: index.handler
131124
Runtime: nodejs6.10
132-
CodeUri: ./src/functions/build-dispatcher
133-
Timeout: 10
134-
MemorySize: 128
135-
Environment:
136-
Variables:
137-
CODEBUILD_PROJECT_REGION: !Ref CodeBuildRegion
138-
CODEBUILD_PROJECT_NAME: !Ref CodeBuildProjectName
139-
GITHUB_TOKEN: !Ref GitHubPersonalAccessToken
140-
GITHUB_REPOSITORY_URL: !Ref GitHubRepositoryUrl
141-
BuildResultExporter:
142-
Type: "AWS::Serverless::Function"
143-
Properties:
144-
Role: !GetAtt LambdaExecutionRole.Arn
145-
Handler: index.handler
146-
Runtime: nodejs6.10
147-
CodeUri: ./src/functions/build-result-exporter
148-
Timeout: 10
149-
MemorySize: 128
150-
BuildResultNotifier:
151-
Type: "AWS::Serverless::Function"
152-
Properties:
153-
Role: !GetAtt LambdaExecutionRole.Arn
154-
Handler: index.handler
155-
Runtime: nodejs6.10
156-
CodeUri: ./src/functions/build-result-notifier
125+
CodeUri: ./src/functions/build-state-notifier
157126
Timeout: 10
158127
MemorySize: 128
159128
Environment:
160129
Variables:
161130
CODEBUILD_PROJECT_REGION: !Ref CodeBuildRegion
162131
GITHUB_TOKEN: !Ref GitHubPersonalAccessToken
163132
GITHUB_REPOSITORY_URL: !Ref GitHubRepositoryUrl
133+
Events:
134+
CodeBuildStatusChange:
135+
Type: CloudWatchEvent
136+
Properties:
137+
Pattern:
138+
source:
139+
- "aws.codebuild"
140+
detail-type:
141+
- !Sub "CodeBuild Build State Change"
142+
detail:
143+
project-name:
144+
- !Ref CodeBuildProjectName
164145
##########################
165-
# StepFunctionsStack
146+
# Lambda (WebhookHandler)
166147
##########################
167-
SFLambdaExecutionRole:
148+
WebhookHandlerExecutionRole:
168149
Type: "AWS::IAM::Role"
169150
Properties:
170151
AssumeRolePolicyDocument:
@@ -175,7 +156,7 @@ Resources:
175156
Service: lambda.amazonaws.com
176157
Action: "sts:AssumeRole"
177158
Policies:
178-
- PolicyName: !Sub "${CodeBuildProjectName}-sf-lambda-execution-role"
159+
- PolicyName: !Sub "${CodeBuildProjectName}-webhook-handler-execution-role"
179160
PolicyDocument:
180161
Statement:
181162
- Effect: Allow
@@ -185,12 +166,13 @@ Resources:
185166
- "logs:PutLogEvents"
186167
Resource: "arn:aws:logs:*:*:*"
187168
- Effect: Allow
188-
Action: "states:StartExecution"
189-
Resource: !Ref BuildStateMachine
169+
Action:
170+
- "codebuild:StartBuild"
171+
Resource: !Sub "arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/${CodeBuildProjectName}"
190172
GitHubWebhookHandler:
191173
Type: "AWS::Serverless::Function"
192174
Properties:
193-
Role: !GetAtt SFLambdaExecutionRole.Arn
175+
Role: !GetAtt WebhookHandlerExecutionRole.Arn
194176
Handler: index.handler
195177
Runtime: nodejs6.10
196178
CodeUri: ./src/functions/github-webhook-handler
@@ -204,13 +186,14 @@ Resources:
204186
Environment:
205187
Variables:
206188
DO_NOT_RUN: false
207-
STEP_FUNCTIONS_ARN: !Ref BuildStateMachine
208189
CODEBUILD_PROJECT_REGION: !Ref CodeBuildRegion
190+
CODEBUILD_PROJECT_NAME: !Ref CodeBuildProjectName
209191
GITHUB_TOKEN: !Ref GitHubPersonalAccessToken
210192
GITHUB_REPOSITORY_URL: !Ref GitHubRepositoryUrl
211193
GITHUB_TARGET_RESOURCE: !Ref GitHubTargetResource
212194
GITHUB_IGNORE_BRANCH_REGEX: !Ref GitHubIgnoreBranchRegex
213195
BUILD_SKIPPED_BY: !Ref BuildSkippedBy
196+
# We don't use followings anymore but they have to be kept as a workaround for an AWS SAM's bug? that CFn reports 'Circular dependency error' while updating the stack to modify the WebhookHandler's execution role.
214197
StatesExecutionRole:
215198
Type: "AWS::IAM::Role"
216199
Properties:
@@ -228,7 +211,7 @@ Resources:
228211
PolicyDocument:
229212
Version: "2012-10-17"
230213
Statement:
231-
- Effect: Allow
214+
- Effect: Deny
232215
Action:
233216
- "lambda:InvokeFunction"
234217
Resource: "*"
@@ -238,37 +221,11 @@ Resources:
238221
DefinitionString: !Sub
239222
|-
240223
{
241-
"StartAt": "Dispatch Build",
224+
"StartAt": "Wait 10 Seconds",
242225
"States": {
243-
"Dispatch Build": {
244-
"Type": "Task",
245-
"Resource": "${BuildDispatcher.Arn}",
246-
"Next": "Wait 10 Seconds"
247-
},
248226
"Wait 10 Seconds": {
249227
"Type": "Wait",
250228
"Seconds": 10,
251-
"Next": "Export Build Result"
252-
},
253-
"Export Build Result": {
254-
"Type": "Task",
255-
"Resource": "${BuildResultExporter.Arn}",
256-
"Next": "Test If Build Finished"
257-
},
258-
"Test If Build Finished": {
259-
"Type": "Choice",
260-
"Choices": [
261-
{
262-
"Variable": "$.buildComplete",
263-
"BooleanEquals": true,
264-
"Next": "Notify Build Result"
265-
}
266-
],
267-
"Default": "Wait 10 Seconds"
268-
},
269-
"Notify Build Result": {
270-
"Type": "Task",
271-
"Resource": "${BuildResultNotifier.Arn}",
272229
"End": true
273230
}
274231
}

src/functions/build-dispatcher/index.js

-63
This file was deleted.

src/functions/build-dispatcher/package.json

-16
This file was deleted.

src/functions/build-result-exporter/.yarnclean

-42
This file was deleted.

src/functions/build-result-exporter/index.js

-22
This file was deleted.

src/functions/build-result-exporter/package.json

-12
This file was deleted.

0 commit comments

Comments
 (0)