-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDash-Step-Functions-Template.yml
225 lines (205 loc) · 6.79 KB
/
Dash-Step-Functions-Template.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: 2010-09-09
Description: A Step Function to run a Glue Job to pull data into a staging table, push it onto prod and then clear staging.
Parameters:
GlueJobName:
Type: String
Description: Name of glue job
Default: staging-csat
AWSLambdaExecuteArn:
Type: String
Description: ARN of the AWSLambdaExecute policy
Default: arn:aws:iam::aws:policy/AWSLambdaExecute
AWSLambdaInvokeArn:
Type: String
Description: ARN of the AWSLambdaRole role
Default: arn:aws:iam::aws:policy/service-role/AWSLambdaRole
MySQLEndpoint:
Type: String
Description: Endpoint of the MySQL database
Default: "127.0.0.1"
MySQLStagingDbName:
Type: String
Description: Name of the MySQL staging database
Default: staging_db
MySQLPort:
Type: String
Description: Port of the MySQL database
Default: "3306"
MySQLUsername:
Type: String
Description: Username to connect to the MySQL database
Default: mysql_user
MySQLPassword:
Type: String
Description: Password to connect to the MySQL database
Default: password123
Resources:
MySQLEndpointParameter:
Type: AWS::SSM::Parameter
Properties:
Type: String
Name: mysql-endpoint
Value: !Ref MySQLEndpoint
MySQLDBNameParameter:
Type: AWS::SSM::Parameter
Properties:
Type: String
Name: mysql-staging-db-name
Value: !Ref MySQLStagingDbName
MySQLUsernameParameter:
Type: AWS::SSM::Parameter
Properties:
Type: String
Name: mysql-username
Value: !Ref MySQLUsername
MySQLPortParameter:
Type: AWS::SSM::Parameter
Properties:
Type: String
Name: mysql-port
Value: !Ref MySQLPort
MySQLPasswordParameter:
Type: AWS::SSM::Parameter
Properties:
Type: String
Name: mysql-password
Value: !Ref MySQLPassword
StepFunctionRole:
Type: AWS::IAM::Role
Properties:
Description: Role for Step Functions to invoke Lambda functions
Path: /
ManagedPolicyArns:
- !Ref AWSLambdaInvokeArn
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action:
- sts:AssumeRole
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
Description: Role for Lambda functions to use when executing
Path: /
ManagedPolicyArns:
- !Ref AWSLambdaExecuteArn
Policies:
- PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ssm:GetParameter
Resource:
- !Sub
- "arn:aws:ssm:${region}:${accountid}:parameter/mysql-endpoint"
- region: !Ref AWS::Region
accountid: !Ref AWS::AccountId
- !Sub
- "arn:aws:ssm:${region}:${accountid}:parameter/mysql-staging-db-name"
- region: !Ref AWS::Region
accountid: !Ref AWS::AccountId
- !Sub
- "arn:aws:ssm:${region}:${accountid}:parameter/mysql-port"
- region: !Ref AWS::Region
accountid: !Ref AWS::AccountId
- !Sub
- "arn:aws:ssm:${region}:${accountid}:parameter/mysql-username"
- region: !Ref AWS::Region
accountid: !Ref AWS::AccountId
- !Sub
- "arn:aws:ssm:${region}:${accountid}:parameter/mysql-password"
- region: !Ref AWS::Region
accountid: !Ref AWS::AccountId
PolicyName: iamp_allow_ssm_get_parameter
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
UpdateProdFromStagingDataLambda:
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: python3.8
Handler: lambda_function.lambda_handler
Code: lambdas/updateProdFromStagingTable/
ClearStagingTableLambda:
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: python3.8
Handler: lambda_function.lambda_handler
Code: lambdas/deleteStagingTableData/
ClearProdTableLambda:
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: python3.8
Handler: lambda_function.lambda_handler
Code: lambdas/deleteProdTableData/
StateMachine:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineName:
!Sub
- '${GlueJob}-state-machine'
- GlueJob: !Ref GlueJobName
RoleArn: !GetAtt StepFunctionRole.Arn
DefinitionString: |-
{
"Comment": "A simple AWS Step Functions state machine that runs a glue job to update the employees prod table.",
"StartAt": "Clear Staging Table",
"States": {
"Clear Staging Table": {
"Type": "Task",
"Resource": "${ClearStagingFunc}",
"ResultPath": null,
"Next": "Run Glue Job"
},
"Run Glue Job": {
"Type": "Task",
"Resource": "arn:aws:states:::glue:startJobRun.sync",
"Parameters": {
"JobName": "${GlueJobName}"
},
"ResultPath": null,
"Next": "Update Prod Table From Staging"
},
"Update Prod Table From Staging": {
"Type": "Task",
"Resource": "${UpdateProdFunc}",
"ResultPath": null,
"Retry": [ {
"ErrorEquals" : ["Lambda.Unknown"],
"IntervalSeconds": 5,
"MaxAttempts": 3,
"BackoffRate": 2
}],
"Next": "Wait 15 seconds"
},
"Wait 15 seconds": {
"Comment": "A Wait state delays the state machine from continuing for a specified time.",
"Type": "Wait",
"Seconds": 15,
"Next": "Delete Staging Table Data"
},
"Delete Staging Table Data": {
"Type": "Task",
"Resource": "${ClearStagingFunc}",
"End": true
}
}
}
DefinitionSubstitutions:
GlueJobName: !Ref GlueJobName
UpdateProdFunc: !GetAtt UpdateProdFromStagingDataLambda.Arn
ClearStagingFunc: !GetAtt ClearStagingTableLambda.Arn