Skip to content

Instantly share code, notes, and snippets.

@brysontyrrell
Last active January 7, 2022 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brysontyrrell/f0ba0b6d030f4a5642dc5b976d14c2a5 to your computer and use it in GitHub Desktop.
Save brysontyrrell/f0ba0b6d030f4a5642dc5b976d14c2a5 to your computer and use it in GitHub Desktop.
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