- Drop both these files in the same folder.
- Deploy using AWS SAM
sam deploy --guided
openapi: "3.0.1" | |
info: | |
title: "API Gateway REST to SQS" | |
version: "2022-09-27T06:05:21Z" | |
paths: | |
/message: | |
delete: | |
responses: | |
"500": | |
description: "500 response" | |
headers: | |
Content-Type: | |
schema: | |
type: "string" | |
content: {} | |
"200": | |
description: "200 response" | |
headers: | |
Content-Type: | |
schema: | |
type: "string" | |
content: {} | |
security: | |
- sigv4: [] | |
x-amazon-apigateway-integration: | |
type: "aws" | |
credentials: | |
Fn::GetAtt: [ MyApiRole, Arn ] | |
httpMethod: "POST" | |
uri: {"Fn::Sub":["arn:aws:apigateway:${Region}:sqs:path/${Account}/${QueueName}",{"Region":{"Ref": "AWS::Region"},"Account":{"Ref": "AWS::AccountId"},"QueueName" : {"Fn::GetAtt": ["MyQueue", "QueueName"]}}]} | |
responses: | |
default: | |
statusCode: "200" | |
"500": | |
statusCode: "500" | |
responseTemplates: | |
text/html: "Error" | |
requestParameters: | |
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'" | |
requestTemplates: | |
application/json: "Action=DeleteMessage&ReceiptHandle=$util.urlEncode($input.params('receiptHandle'))" | |
passthroughBehavior: "never" | |
/: | |
get: | |
responses: | |
"500": | |
description: "500 response" | |
headers: | |
Content-Type: | |
schema: | |
type: "string" | |
content: {} | |
"200": | |
description: "200 response" | |
headers: | |
Content-Type: | |
schema: | |
type: "string" | |
content: {} | |
security: | |
- sigv4: [] | |
x-amazon-apigateway-integration: | |
type: "aws" | |
credentials: | |
Fn::GetAtt: [ MyApiRole, Arn ] | |
httpMethod: "POST" | |
uri: {"Fn::Sub":["arn:aws:apigateway:${Region}:sqs:path/${Account}/${QueueName}",{"Region":{"Ref": "AWS::Region"},"Account":{"Ref": "AWS::AccountId"},"QueueName" : {"Fn::GetAtt": ["MyQueue", "QueueName"]}}]} | |
responses: | |
default: | |
statusCode: "200" | |
"500": | |
statusCode: "500" | |
responseTemplates: | |
text/html: "Error" | |
requestParameters: | |
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'" | |
requestTemplates: | |
application/json: "Action=ReceiveMessage" | |
passthroughBehavior: "never" | |
post: | |
responses: | |
"500": | |
description: "500 response" | |
headers: | |
Content-Type: | |
schema: | |
type: "string" | |
content: {} | |
"200": | |
description: "200 response" | |
headers: | |
Content-Type: | |
schema: | |
type: "string" | |
content: {} | |
security: | |
- sigv4: [] | |
x-amazon-apigateway-integration: | |
type: "aws" | |
credentials: | |
Fn::GetAtt: [ MyApiRole, Arn ] | |
httpMethod: "POST" | |
uri: {"Fn::Sub":["arn:aws:apigateway:${Region}:sqs:path/${Account}/${QueueName}",{"Region":{"Ref": "AWS::Region"},"Account":{"Ref": "AWS::AccountId"},"QueueName" : {"Fn::GetAtt": ["MyQueue", "QueueName"]}}]} | |
responses: | |
default: | |
statusCode: "200" | |
"500": | |
statusCode: "500" | |
responseTemplates: | |
text/html: "Error" | |
requestParameters: | |
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'" | |
requestTemplates: | |
application/json: "Action=SendMessage&MessageBody=$util.urlEncode(\"$input.body\")" | |
passthroughBehavior: "never" | |
components: | |
securitySchemes: | |
sigv4: | |
type: "apiKey" | |
name: "Authorization" | |
in: "header" | |
x-amazon-apigateway-authtype: "awsSigv4" |
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: AWS::Serverless-2016-10-31 | |
Description: Serverless patterns - API Gateway REST -> SQS | |
# Comment each resource section to explain usage | |
Resources: | |
MyQueue: | |
Type: AWS::SQS::Queue | |
SiteAPI: | |
Type: AWS::Serverless::Api | |
DependsOn: | |
- MyQueue | |
- MyApiRole | |
Properties: | |
StageName: Prod | |
EndpointConfiguration: REGIONAL | |
DefinitionBody: | |
'Fn::Transform': | |
Name: 'AWS::Include' | |
Parameters: | |
Location: './api.yaml' | |
MyApiRole: | |
Type: "AWS::IAM::Role" | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: "2012-10-17" | |
Statement: | |
- Effect: "Allow" | |
Principal: | |
Service: "apigateway.amazonaws.com" | |
Action: | |
- "sts:AssumeRole" | |
Policies: | |
- PolicyName: ApiDirectToSQS | |
PolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
Action: | |
- sqs:SendMessage | |
- sqs:ReceiveMessage | |
- sqs:DeleteMessage | |
Effect: Allow | |
Resource: | |
- !GetAtt MyQueue.Arn | |
# List all common outputs for usage | |
Outputs: | |
ApiEndpoint: | |
Description: "HTTP API endpoint URL" | |
Value: !Sub "https://${SiteAPI}.execute-api.${AWS::Region}.amazonaws.com/Prod" | |