Skip to content

Latest commit

 

History

History

apigw-lambda-request-validator

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Amazon API Gateway REST API to AWS Lambda with Request Validator

This pattern creates an Amazon API Gateway REST API with Request Validator and an AWS Lambda function.

Learn more about this pattern at: https://serverlessland.com/patterns/apigw-lambda-validator.

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:

    git clone https://github.com/aws-samples/serverless-patterns
    
  2. Change directory to the pattern directory:

    cd apigw-lambda-request-validator
    
  3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:

    sam deploy --guided
    
  4. During the prompts:

    • Enter a stack name
    • Enter the desired AWS Region
    • Enter the REST API Name

    Once you have run sam deploy --guided mode once and saved arguments to a configuration file (samconfig.toml), you can use sam deploy in future to use these defaults.

  5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs that were created.

How it works

This pattern deploys an Amazon API Gateway REST API with a request validator and a default route integrated with an AWS Lambda function written in Node.js. The request validator is used to validate query string, header and request body passed on each REST API call. The lambda function returns a basic response when all validations are passed and when any validation fails API will return a response.

Testing

Once the application is deployed, either use a curl or call the endpoint from Postman.

Example POST Request: curl -X POST "https://{api-gateway-endpoint}/prod?myQueryString=mystring123" --header "myHeader:myhead123" -H 'Content-Type: application/json' -d '{"firstName":"John","lastName":"Bean"}'

Response:

{
  "queryString": "mystring123",
  "header": "myhead123",
  "message": "Hello John Bean, from AWS Lambda!"
}

Example POST Request without queryString: curl -X POST "https://{api-gateway-endpoint}/prod" --header "myHeader:myhead123" -H 'Content-Type: application/json' -d '{"firstName":"John","lastName":"Bean"}'

Response:

{
  "message": "Missing required request parameters: [myQueryString]"
}

Example POST Request without header: curl -X POST "https://{api-gateway-endpoint}/prod?myQueryString=mystring123" -H 'Content-Type: application/json' -d '{"firstName":"John","lastName":"Bean"}'

Response:

{
  "message": "Missing required request parameters: [myHeader]"
}

Example POST Request without queryString and header: curl -X POST "https://{api-gateway-endpoint}/prod" -H 'Content-Type: application/json' -d '{"firstName":"John","lastName":"Bean"}'

Response:

{
  "message": "Missing required request parameters: [myHeader, myQueryString]
}

Example POST Request with invalid request body: curl -X POST "https://{api-gateway-endpoint}/prod?myQueryString=mystring123" --header "myHeader:myhead123" -H 'Content-Type: application/json' -d '{"firstName":"John"}'

Response:

{
  "message": "Invalid request body"
}

Documentation

Cleanup

  1. Delete the stack
    aws cloudformation delete-stack --stack-name STACK_NAME
  2. Confirm the stack has been deleted
    aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

This pattern was contributed by Sudheer Yalamanchili.


Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0