Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prestomation/02f1ce4fde028eb5eb4c71bf49ec5efb to your computer and use it in GitHub Desktop.
Save prestomation/02f1ce4fde028eb5eb4c71bf49ec5efb 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:
/{proxy+}:
post:
schema:
$ref: "#/definitions/Empty"
consumes:
- application/json
produces:
- application/json
parameters:
- name: proxy
in: path
required: true
type: string
- in: body
name: RequestBodyModel
required: true
schema:
"$ref": "#/definitions/RequestBodyModel"
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:
Empty:
type: object
title: Empty Schema
RequestBodyModel:
type: object
properties:
id:
type: integer
required:
- id
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:
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