forked from aws-samples/serverless-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yml
More file actions
118 lines (108 loc) · 4.24 KB
/
Copy pathtemplate.yml
File metadata and controls
118 lines (108 loc) · 4.24 KB
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A Lambda function triggered by an AWS API Gateway HTTP APIs call through an Amazon SQS Queue for buffering
# Comment each resource section to explain usage
Resources:
##########################################################################
# SQS Queue #
##########################################################################
# Define the SQS queue
MySqsQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: MySqsQueue
##########################################################################
# HTTP API #
##########################################################################
MyHttpApi:
Type: AWS::Serverless::HttpApi
Properties:
AccessLogSettings:
DestinationArn: !GetAtt MyHttpApiAccessLogs.Arn
Format: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","routeKey":"$context.routeKey", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength" }'
DefinitionBody:
'Fn::Transform':
Name: 'AWS::Include'
Parameters:
Location: 'api.yaml'
##########################################################################
# Lambda Function #
##########################################################################
SQSLambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: SQSLambdaFunction
Description: Lambda to be invoked by the SQS Queue
CodeUri: src/index.zip
Handler: index.handler
Runtime: python3.11
Architectures:
- x86_64
Timeout: 30
MemorySize: 512
Events:
MySQSEvent:
Type: SQS
Properties:
Queue: !GetAtt MySqsQueue.Arn
BatchSize: 10
##########################################################################
# Roles #
##########################################################################
MyHttpApiRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: AllowSqsIntegration
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'sqs:SendMessage'
- 'sqs:GetQueueUrl'
- 'sqs:SendMessageBatch'
Resource: !GetAtt MySqsQueue.Arn
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:DescribeLogGroups'
- 'logs:DescribeLogStreams'
- 'logs:PutLogEvents'
- 'logs:GetLogEvents'
- 'logs:FilterLogEvents'
Resource: !GetAtt MyHttpApiAccessLogs.Arn
##########################################################################
# Cloudwatch Logs #
##########################################################################
MyHttpApiAccessLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: MyHttpApi-Access-Logs
RetentionInDays: 1
##########################################################################
# Outputs #
##########################################################################
Outputs:
MyHttpApiEndpoint:
Description: "HTTP API endpoint"
Value: !Sub "https://${MyHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
SQSLambdaFunction:
Description: SQSLambdaFunction function name
Value: !Ref SQSLambdaFunction
MySqsQueueARN:
Description: SQS queue ARN
Value: !GetAtt MySqsQueue.Arn
MySqsQueueURL:
Description: SQS queue URL
Value: !Ref MySqsQueue