Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Creates an API Gateway to publish data to a SQS queue.
Resources:
ApiQueue:
Type: AWS::SQS::Queue
ApiRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- sts:AssumeRole
Principal:
Service:
- apigateway.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
Policies:
- PolicyName: ApiQueuePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- sqs:SendMessage
Resource: !GetAtt ApiQueue.Arn
Api:
Type: AWS::Serverless::Api
Properties:
StageName: latest
Auth:
ApiKeyRequired: 'true'
DefinitionBody:
swagger: '2.0'
info:
version: '1.0'
title: !Ref AWS::StackName
paths:
"/":
post:
consumes:
- "application/json"
responses:
"204":
description: 204 Response
x-amazon-apigateway-integration:
type: aws
httpMethod: POST
credentials: !GetAtt ApiRole.Arn
uri: !Sub "arn:aws:apigateway:${AWS::Region}:sqs:path//"
responses:
default:
statusCode: "204"
requestParameters:
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"
requestTemplates:
application/json: !Sub "Action=SendMessage##\n&QueueUrl=$util.urlEncode('${ApiQueue}')##\n\
&MessageBody=$util.urlEncode($input.body)##\n"
passthroughBehavior: never
ApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Description: !Ref AWS::StackName
Enabled: true
StageKeys:
- RestApiId: !Ref Api
StageName: !Ref Api.Stage
ApiUsagePlan:
Type: AWS::ApiGateway::UsagePlan
Properties:
ApiStages:
- ApiId: !Ref Api
Stage: !Ref Api.Stage
ApiUsagePlanKey:
Type: AWS::ApiGateway::UsagePlanKey
Properties:
KeyId: !Ref ApiKey
KeyType: API_KEY
UsagePlanId: !Ref ApiUsagePlan
Outputs:
ApiGatewayExecuteApi:
Value: !Sub https://${Api}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${Api.Stage}/
ApiKey:
Value: !Ref ApiKey
SqsQueueUrl:
Value: !Ref ApiQueue
SqsQueueArn:
Value: !GetAtt ApiQueue.Arn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment