Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prestomation/95aa6f95cb047c2a9e4482febcf71929 to your computer and use it in GitHub Desktop.
Save prestomation/95aa6f95cb047c2a9e4482febcf71929 to your computer and use it in GitHub Desktop.
---
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: API Gateway Request Model Validation using Lambda Proxy Integration
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
DefinitionBody:
swagger: 2.0
basePath: /prod
info:
title: ModelValidatorExample
schemes:
- https
paths:
/thing_one:
post:
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: ThingOne
required: true
schema:
"$ref": "#/definitions/ThingOne"
responses: {}
x-amazon-apigateway-integration:
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations
httpMethod: POST
type: aws_proxy
/thing_two:
post:
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: ThingTwo
required: true
schema:
"$ref": "#/definitions/ThingTwo"
responses: {}
x-amazon-apigateway-integration:
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations
httpMethod: POST
type: aws_proxy
x-amazon-apigateway-request-validator : "all"
x-amazon-apigateway-request-validators:
all:
validateRequestBody : true
validateRequestParameters : true
definitions:
ThingOne:
type: object
properties:
prop1:
type: string
required:
- prop1
ThingTwo:
type: object
properties:
prop2:
type: string
required:
- prop2
StageName: Prod
#We aren't using AWS::Serverless::Function so we can inline code and make this cfn template self-contained
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
#Just mirror our request to our examples.
ZipFile: >
exports.handler = function(event, context, callback) {
callback(null, {
statusCode: '200',
body: JSON.stringify(event)
}); }
Handler: index.handler
Runtime: nodejs6.10
Role:
Fn::GetAtt:
- "LambdaExecutionRole"
- "Arn"
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref LambdaFunction
Action: 'lambda:InvokeFunction'
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment