Skip to content

Instantly share code, notes, and snippets.

@MattHealy
Created May 22, 2019 02:53
Show Gist options
  • Save MattHealy/eccb04d57ea1ac404ba36606af7521a8 to your computer and use it in GitHub Desktop.
Save MattHealy/eccb04d57ea1ac404ba36606af7521a8 to your computer and use it in GitHub Desktop.
Cloudformation Template for API Gateway AWS Service Integration to SQS Queue
Description: API Gateway to JSON payloads and send to SQS
Outputs:
ApiEndpoint:
Description: Endpoint for this stage of the api
Value: !Join
- ''
- - https://
- !Ref 'SQSProxy'
- .execute-api.ap-southeast-2.amazonaws.com/
- prod
QueueArnSQS:
Description: ARN of SQS Queue
Value: !GetAtt 'DestQueue.Arn'
Resources:
APIGatewayRole:
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Version: '2012-10-17'
Path: /
Policies:
- PolicyDocument:
Statement:
- Action: sqs:SendMessage
Effect: Allow
Resource: !GetAtt 'DestQueue.Arn'
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName: apig-sqs-send-msg-policy
RoleName: apig-sqs-send-msg-role
Type: AWS::IAM::Role
DestQueue:
Properties:
ContentBasedDeduplication: true
DelaySeconds: 0
FifoQueue: true
MaximumMessageSize: 262144
MessageRetentionPeriod: 1209600
QueueName: myQueue.fifo
ReceiveMessageWaitTimeSeconds: 0
VisibilityTimeout: 30
Type: AWS::SQS::Queue
PolicySQS:
Properties:
PolicyDocument:
Statement:
- Action: SQS:*
Effect: Allow
Principal: '*'
Resource: !GetAtt 'DestQueue.Arn'
Sid: Sid1517269801413
Version: '2012-10-17'
Queues:
- !Ref 'DestQueue'
Type: AWS::SQS::QueuePolicy
PostMethod:
Properties:
AuthorizationType: NONE
HttpMethod: POST
Integration:
Credentials: !GetAtt 'APIGatewayRole.Arn'
IntegrationHttpMethod: POST
IntegrationResponses:
- StatusCode: '200'
PassthroughBehavior: NEVER
RequestParameters:
integration.request.header.Content-Type: '''application/x-www-form-urlencoded'''
RequestTemplates:
application/json: Action=SendMessage&MessageBody=$input.body&MessageGroupId=1
Type: AWS
Uri: arn:aws:apigateway:ap-southeast-2:sqs:path/<ACCOUNTID>/myQueue.fifo
MethodResponses:
- ResponseModels:
application/json: Empty
StatusCode: '200'
ResourceId: !Ref 'enqueueResource'
RestApiId: !Ref 'SQSProxy'
Type: AWS::ApiGateway::Method
SQSProxy:
Properties:
Description: API Endpoint to receive JSON payloads and queue in SQS
Name: SQSProxy
Type: AWS::ApiGateway::RestApi
enqueueResource:
Properties:
ParentId: !Ref 'v1Resource'
PathPart: enqueue
RestApiId: !Ref 'SQSProxy'
Type: AWS::ApiGateway::Resource
prodDeployment:
DependsOn: PostMethod
Properties:
RestApiId: !Ref 'SQSProxy'
Type: AWS::ApiGateway::Deployment
prodStage:
Properties:
DeploymentId: !Ref 'prodDeployment'
RestApiId: !Ref 'SQSProxy'
StageName: prod
Type: AWS::ApiGateway::Stage
v1Resource:
Properties:
ParentId: !GetAtt 'SQSProxy.RootResourceId'
PathPart: v1
RestApiId: !Ref 'SQSProxy'
Type: AWS::ApiGateway::Resource
@leadelngalame1611
Copy link

Thank you. This is exactly what i have been searching for. How to use api gateway as a proxy for sqs fifo queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment