Skip to content

Instantly share code, notes, and snippets.

@nmiguelmoura
Last active March 18, 2021 09:51
Show Gist options
  • Save nmiguelmoura/7b990d091a2c1b6537b7984ee351348a to your computer and use it in GitHub Desktop.
Save nmiguelmoura/7b990d091a2c1b6537b7984ee351348a to your computer and use it in GitHub Desktop.
AWS Api Gateway to SQS integration (path parameters to SQS)
# POST endpoint /update/{productId}.
# Api Gateway will send the productId to an SQS Queue.
AWSTemplateFormatVersion: 2010-09-09
Description: >-
api-to-sqs
Transform:
- AWS::Serverless-2016-10-31
Resources:
SimpleQueue:
Type: AWS::SQS::Queue
ApiRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: SendMessageToSQSPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Action: sqs:SendMessage
Resource: !GetAtt SimpleQueue.Arn
Api:
Type: "AWS::Serverless::Api"
DependsOn:
- ApiRole
Properties:
StageName: Prod
DefinitionBody:
swagger: "2.0"
info:
title: My app
description: My app again
version: 1.0
x-amazon-apigateway-request-validator: "ValidatePath"
paths:
"/update/{productId}":
post:
produces:
- application/json
parameters:
- in: path
required: true
name: productId
description: The product code
schema:
type: integer
x-amazon-apigateway-request-validator: "ValidatePath"
responses:
"202":
description: on success
"400":
description: invalid request body
x-amazon-apigateway-integration:
type: AWS
httpMethod: POST
passthroughBehavior: NEVER
requestParameters:
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"
requestTemplates:
"application/json": "Action=SendMessage&MessageBody=$input.params('productId')"
credentials: !GetAtt ApiRole.Arn
uri:
!Sub "arn:aws:apigateway:${AWS::Region}:sqs:path/${AWS::AccountId}/${SimpleQueue.QueueName}"
responses:
default:
statusCode: "202"
responseTemplates:
application/json: '{"message": "Product sent for processing"}'
x-amazon-apigateway-request-validators:
ValidatePath:
validateRequestParameters: true
validateRequestBody: false
x-amazon-apigateway-gateway-responses:
BAD_REQUEST_PARAMETERS:
responseTemplates:
application/json: '{"error":{"message":"$context.error.messageString","errors":"$context.error.validationErrorString"}}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment