-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
201 lines (190 loc) · 6.75 KB
/
template.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Serverless stack for automated data processing tasks
Metadata:
AWS::ServerlessRepo::Application:
Name: Athena Query execution
Description: Implementation of Serverless Stack to schedule queries
Author: Jose Arevalo
Labels:
- state-machine
- athena-query
SemanticVersion: 0.1.0
Parameters:
DeploymentId:
Type: String
Description: Unique identifier for each deployment
ScheduleGroupName:
Type: String
Default: "AthenaScheduledQueries"
Description: "SCHEDULE GROUP NAME created"
QueryOutputLocation:
Type: String
Description: "S3 URI to put Athena Query results location, like (s3://aws-athena-query-results-{AWS_ACCOUNT_ID}-eu-west-1/)"
Resources:
##########################################################################
# IAM Role for StateMachine
##########################################################################
DataProcessingStateMachineRole:
Type: AWS::IAM::Role
Properties:
#RoleName: latana.role.serverless-data-pipe.state-machine
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonAthenaFullAccess
- arn:aws:iam::aws:policy/AmazonS3FullAccess
##########################################################################
# IAM Role for Lambda Function
##########################################################################
EventBridgeSyncFunctionRole:
Type: AWS::IAM::Role
Properties:
#RoleName: latana.role.lambda.EventBridgeSyncRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/CloudWatchFullAccess
- arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess
##########################################################################
# IAM Role for Scheduler Execution
##########################################################################
SchedulerExecutionRole:
Type: AWS::IAM::Role
Properties:
#RoleName: latana.role.lambda.SchedulerExecutionRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: scheduler.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: "scheduler_start_execution_state_function_athena_queries"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- states:StartExecution
Resource:
- !Ref AthenaQueriesStateMachine
##########################################################################
# State Machine
##########################################################################
AthenaQueriesStateMachine:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineName: StateMachine-Athena-QueryExecution
RoleArn: !GetAtt DataProcessingStateMachineRole.Arn
DefinitionString: |-
{
"Comment": "A description of my state machine",
"StartAt": "Athena StartQueryExecution",
"States": {
"Athena StartQueryExecution": {
"Type": "Task",
"Resource": "arn:aws:states:::athena:startQueryExecution",
"Parameters": {
"QueryString.$": "$.QueryString",
"WorkGroup": "primary",
"ResultConfiguration": {
"OutputLocation.$": "$.OutputLocation"
}
},
"Next": "Wait",
"ResultSelector": {
"QueryExecutionId.$": "$.QueryExecutionId"
},
"ResultPath": "$.QueryExecution"
},
"Wait": {
"Type": "Wait",
"Next": "Athena GetQueryExecution",
"SecondsPath": "$.WaitSeconds"
},
"Athena GetQueryExecution": {
"Type": "Task",
"Resource": "arn:aws:states:::athena:getQueryExecution",
"Parameters": {
"QueryExecutionId.$": "$.QueryExecution.QueryExecutionId"
},
"Next": "Choice"
},
"Choice": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.QueryExecution.Status.State",
"StringEquals": "SUCCEEDED",
"Next": "Success"
},
{
"Variable": "$.QueryExecution.Status.State",
"StringEquals": "FAILED",
"Next": "Fail"
}
],
"Default": "Wait"
},
"Success": {
"Type": "Succeed"
},
"Fail": {
"Type": "Fail"
}
}
}
##########################################################################
# Lambda Function
##########################################################################
EventBridgeSyncFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: main.lambda_handler
Runtime: python3.10
Timeout: 180
Role: !GetAtt EventBridgeSyncFunctionRole.Arn
Architectures:
- x86_64
Environment:
Variables:
STM_ARN: !Ref AthenaQueriesStateMachine
ROLE_ARN: !GetAtt SchedulerExecutionRole.Arn
SCHEDULE_GROUP_NAME: !Ref ScheduleGroupName
OUTPUT_LOCATION: !Ref QueryOutputLocation
Layers:
- !Ref libs
##########################################################################
# Layer to the Lambda Function
##########################################################################
libs:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: lambda-dependencies
Description: Dependencies for Lambda, include YAML
ContentUri: libs/
##########################################################################
# Invoke the Lambda Function to sync the schedules
##########################################################################
EventBridgeSyncFunctionCustom:
Type: Custom::EventBridgeSyncFunction
Properties:
ServiceToken: !GetAtt EventBridgeSyncFunction.Arn
DeploymentId: !Ref DeploymentId # This line uses the DeploymentId parameter