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.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
- API Request Validation
-
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
-
Change directory to the pattern directory:
cd apigw-lambda-request-validator
-
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
-
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 usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs that were created.
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.
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"
}
- Working with HTTP APIs
- Working with AWS Lambda proxy integrations for HTTP APIs
- AWS Lambda - the Basics
- Lambda Function Handler
- Function Environment Variables
- Delete the stack
aws cloudformation delete-stack --stack-name STACK_NAME
- 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