-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
189 lines (182 loc) · 5.75 KB
/
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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
# Setting global properties
Globals:
# This applies to all AWS::Serverless::Function
Function:
Timeout: 30
Runtime: python3.8
Handler: app.lambda_handler
Resources:
InputQueue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 30
QueueName: event-processor-queue-main
RedrivePolicy:
deadLetterTargetArn: !GetAtt DeadLetterQueue.Arn
maxReceiveCount: 2
DeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 30
QueueName: event-processor-queue-dlq
Processor:
Type: AWS::Serverless::Function
Properties:
FunctionName: event-processor
CodeUri: lambdas/processor.py
Handler: processor.lambda_handler
Policies:
- AmazonSQSFullAccess
- DynamoDBCrudPolicy:
TableName: !Ref Table
Environment:
Variables:
TABLE_NAME: !Ref Table
REGION_NAME: !Ref AWS::Region
Events:
SqsEvent:
Type: SQS
Properties:
Queue: !GetAtt InputQueue.Arn
BatchSize: 5
Enabled: true
Layers:
- !Ref ProcessorLayer
Generator:
Type: AWS::Serverless::Function
Properties:
FunctionName: event-generator
CodeUri: lambdas/generator.py
Handler: generator.lambda_handler
Environment:
Variables:
QUEUE_PROXY_ENDPOINT: !Sub "https://${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com/prod/"
NUM_CALLS: 7
NUM_SECONDS: 4
Events:
ApiEvent:
Type: Api
Properties:
Path: /
Method: get
RestApiId: !Ref ApiGatewayApi
ScheduleEvent:
Type: Schedule
Properties:
Schedule: cron(0/1 * * * ? *)
Name: TestSchedule
Description: test schedule
Enabled: True
Layers:
- !Ref GeneratorLayer
GeneratorLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: event-generator
Description: Dependencies for event-generator.
ContentUri: lambdas/layers/generator-deps/
CompatibleRuntimes:
- python3.8
ProcessorLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: event-processor
Description: Dependencies for event processor.
ContentUri: lambdas/layers/generator-deps/
CompatibleRuntimes:
- python3.8
Table:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 2
WriteCapacityUnits: 2
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Name: event-processor-sqs-api
DefinitionBody:
openapi: "3.0.0"
paths:
"/":
post:
summary: Post a message to the Queue
responses:
"200":
description: 200 status response
"400":
description: 400 status response
"500":
description: 500 status response
x-amazon-apigateway-integration:
httpMethod: POST
type: AWS
passthroughBehavior: NEVER
uri: !Sub "arn:aws:apigateway:${AWS::Region}:sqs:path//"
credentials: !GetAtt ApiRole.Arn
requestParameters:
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"
requestTemplates:
application/json:
!Sub "Action=SendMessage##\n&QueueUrl=$util.urlEncode('${InputQueue}')##\n\
&MessageBody=$util.urlEncode($input.body)##\n"
responses:
"4\\d{2}":
statusCode: 400
responseTemplates:
application/json: >
{
"message": $input.json('$.message')
}
"5\\d{2}":
statusCode: 500
responseTemplates:
application/json: >
{
"message": $input.json('$.message')
}
default: # On success - maybe return an ID here for the client to track?
statusCode: 200
responseTemplates:
application/json: >
{
"message": "Success"
}
# ---- IAM ----
# Role for API Gateway to assume to post to the SQS queue
ApiRole:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyName: AnythingGoes
PolicyDocument:
Statement:
- Effect: Allow
Action:
- "*" # Specify only to post messages to the que as action
# - dynamodb:PutItem
Resource:
- "*" #Specify the Queue ARN here to minimize attack surface
# - !Sub "arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${Table}"
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- sts:AssumeRole
Outputs:
PostEndpointApi:
Description: "API Gateway endpoint URL for prod stage for Post Endpoint"
Value: !Sub "https://${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com/prod/"
Processor:
Description: "1st Lambda Function ARN"
Value: !GetAtt Processor.Arn