Skip to content

Instantly share code, notes, and snippets.

@sspocketgalaxy
Last active October 10, 2023 20:14
Show Gist options
  • Save sspocketgalaxy/7d623a433b70debd098df889dd9d1b3d to your computer and use it in GitHub Desktop.
Save sspocketgalaxy/7d623a433b70debd098df889dd9d1b3d to your computer and use it in GitHub Desktop.
API Gateway to SQS CloudFormation Template
---
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
QueueName:
Description: The queue name for integration with API Gateway
Type: String
Default: api-gateway-queue
PassThroughProxyEndpoint:
Description: The proxy URL
Type: String
Default: "https://jsonplaceholder.typicode.com/{proxy}"
ApiGatewayDeploymentStageName:
Description: The API Gateway stage for this deployment
Type: String
Default: dev
Resources:
DeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub "${QueueName}-DLQ"
Queue:
Type: AWS::SQS::Queue
Properties:
QueueName: !Ref QueueName
RedrivePolicy:
deadLetterTargetArn:
Fn::GetAtt:
- DeadLetterQueue
- Arn
maxReceiveCount: 1
QueuePolicy:
DependsOn: ["Queue"]
Type: AWS::SQS::QueuePolicy
Properties:
PolicyDocument:
Statement:
- Action: SQS:*
Effect: Allow
Principal: '*'
Resource: !GetAtt Queue.Arn
Version: '2012-10-17'
Queues:
- !Ref Queue
ApiGatewayToSQSRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- sts:AssumeRole
RoleName: ApiGatewayToSQSRole
Policies:
- PolicyName: ApiGatewayLogsPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action: sqs:SendMessage
Effect: Allow
Resource: !GetAtt 'Queue.Arn'
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutLogEvents
- logs:GetLogEvents
- logs:FilterLogEvents
Effect: Allow
Resource: "*"
RestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: "API Gateway to SQS"
Description: "Sample API for sending messages via API Gateway directly to SQS"
FailOnWarnings: true
Body:
openapi: 3.0.0
info:
title: API Gateway -> SQS API
description: This API handles a route with a POST body to be placed on an SQS queue and proxies the remaining routes
contact:
email: me@example.com
version: 1.0.0
paths:
/events:
post:
summary: Handles a new analytics event
description: "Places an event on an SQS queue for processing"
operationId: analyticsEvent
requestBody:
description: The analytics event to push onto queue for processing
content:
application/json:
schema:
$ref: '#/components/schemas/AnalyticsEvent'
responses:
"200":
description: Event post successfully handled and placed on queue
"400":
description: Bad request
"500":
description: Server Error
x-amazon-apigateway-integration:
credentials: !GetAtt ApiGatewayToSQSRole.Arn
httpMethod: "POST"
uri: !Sub "arn:aws:apigateway:${AWS::Region}:sqs:path/${AWS::AccountId}/${QueueName}"
responses:
default:
statusCode: "200"
requestParameters:
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"
requestTemplates:
application/json: "Action=SendMessage&MessageBody=$input.body"
passthroughBehavior: "never"
type: "aws"
/{proxy+}:
x-amazon-apigateway-any-method:
parameters:
- name: proxy
in: path
required: true
type: string
x-amazon-apigateway-integration:
type: "http_proxy"
httpMethod: "ANY"
uri: !Ref PassThroughProxyEndpoint
passthroughBehavior: "when_no_match"
requestParameters:
integration.request.path.proxy: 'method.request.path.proxy'
components:
schemas:
AnalyticsEvent:
required:
- created_at
- product_id
- event_type
type: object
properties:
created_at:
type: string
description: Timestamp in ISO-8601 format UTC
format: date-time
example: 2021-01-01T12:01:00-00:00
event_type:
type: string
example: ADD_TO_CART
enum:
- ADD_TO_CART
- REMOVE_FROM_CART
- PURCHASE
product_id:
type: integer
format: int64
example: 123456789
ApiGatewayAccount:
Type: AWS::ApiGateway::Account
Properties:
CloudWatchRoleArn: !GetAtt ApiGatewayToSQSRole.Arn
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref RestApi
StageName: !Ref ApiGatewayDeploymentStageName
ApiGatewayStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref ApiGatewayDeployment
RestApiId: !Ref RestApi
StageName: "LATEST"
MethodSettings:
- DataTraceEnabled: true
MetricsEnabled: true
HttpMethod: "*"
LoggingLevel: INFO
ResourcePath: "/*"
Outputs:
ApiGatewayDeploymentURL:
Description: The URL for the API to send HTTP requests
Value: !Sub "https://${RestApi}.execute-api.${AWS::Region}.amazonaws.com/${ApiGatewayStage}/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment