-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathathenarunner-lambda.yaml
163 lines (149 loc) · 5.64 KB
/
athenarunner-lambda.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
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AWSTemplateFormatVersion: 2010-09-09
Description: The CloudFormation template for AWS resources required by Athena Runner.
Parameters:
DDBTableName:
Type: String
Default: "AthenaRunnerActiveQueries"
Description: "Name of the DynamoDB table for persistence & querying of active Athena queries' statuses."
AthenaRunnerLambdaFunctionName:
Type: String
Default: "athenarunner"
Description: "Name of the Lambda function that mediates between AWS Step Functions and AWS Athena."
ArtifactBucketName:
Type: String
MinLength: "1"
Description: "Name of the S3 bucket containing source .zip files."
LambdaSourceS3Key:
Type: String
MinLength: "1"
Description: "Name of the S3 key of Athena Runner lambda function .zip file."
Resources:
AthenaRunnerLambdaExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
AthenaRunnerPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyDocument: {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:Query",
"dynamodb:PutItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem"
],
"Resource": !Sub "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${DDBTableName}"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*"
},
{
"Effect": "Allow",
"Action": "logs:CreateLogGroup",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"states:SendTaskSuccess",
"states:SendTaskFailure",
"states:SendTaskHeartbeat",
"states:GetActivityTask"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"athena:StartQueryExecution",
"athena:GetQueryExecution",
"athena:GetNamedQuery"
],
"Resource": "*"
}
]
}
PolicyName: "AthenaRunnerPolicy"
Roles:
- !Ref AthenaRunnerLambdaExecutionRole
AthenaRunnerLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: "athenarunner"
Description: "Starts and monitors AWS Athena queries on behalf of AWS Step Functions for a specific Activity ARN."
Handler: "athenarunner.handler"
Role: !GetAtt AthenaRunnerLambdaExecutionRole.Arn
Code:
S3Bucket: !Ref ArtifactBucketName
S3Key: !Ref LambdaSourceS3Key
Timeout: 180 #seconds
MemorySize: 128 #MB
Runtime: python3.12
DependsOn:
- AthenaRunnerLambdaExecutionRole
ScheduledRule:
Type: "AWS::Events::Rule"
Properties:
Description: "ScheduledRule"
ScheduleExpression: "rate(3 minutes)"
State: "ENABLED"
Targets:
-
Arn: !GetAtt AthenaRunnerLambdaFunction.Arn
Id: "AthenaRunner"
PermissionForEventsToInvokeLambda:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName: !Ref AthenaRunnerLambdaFunction
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt ScheduledRule.Arn
AthenaRunnerActiveQueriesTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: !Ref DDBTableName
KeySchema:
- KeyType: "HASH"
AttributeName: "sfn_activity_arn"
- KeyType: "RANGE"
AttributeName: "athena_query_execution_id"
AttributeDefinitions:
- AttributeName: "sfn_activity_arn"
AttributeType: "S"
- AttributeName: "athena_query_execution_id"
AttributeType: "S"
ProvisionedThroughput:
WriteCapacityUnits: 10
ReadCapacityUnits: 10