generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 1k
New serverless pattern - Amazon API Gateway Validation (Terraform) #2583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
julianwood
merged 9 commits into
aws-samples:main
from
MakendranG:makendran-feature-apigw-data-validation-tf
Mar 24, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6f95c4e
Added new pattern
3805b4d
Added new pattern
2de5d6e
New serverless Pattern
4899e29
Update README.md
MakendranG e384986
Update example-pattern.json
MakendranG 6ca0d16
Merge branch 'aws-samples:main' into makendran-feature-apigw-data-val…
MakendranG 24197e4
Added src/app.py and removed zip folder
MakendranG d5bd2bc
Update main.tf
MakendranG 1f90c18
Create apigw-data-validation-tf.json
parikhudit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # Amazon API Gateway data validation models | ||
|
|
||
| This pattern creates an Amazon API Gateway that handles simple data validation at the endpoint without invoking the Lambda function when the data validation fails. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/apigw-custom-resource-policy](https://serverlessland.com/patterns/apigw-custom-resource-policy) | ||
|
|
||
| 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](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. | ||
|
|
||
| ## Requirements | ||
|
|
||
| * [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) 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](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
| * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||
| * [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed | ||
|
|
||
| ## Deployment Instructions | ||
|
|
||
| 1. Create a new directory, navigate to that directory in a terminal and clone the repository: | ||
| ``` | ||
| git clone https://github.com/aws-samples/serverless-patterns | ||
| ``` | ||
| 1. Change directory to this pattern's directory | ||
| ``` | ||
| cd serverless-patterns/apigw-data-validation-tf | ||
| ``` | ||
| 1. From the command line, initialize Terraform to downloads and install the providers defined in the configuration: | ||
| ``` | ||
| terraform init | ||
| ``` | ||
| 1. From the command line, apply the configuration in the main.tf file: | ||
| ``` | ||
| terraform apply | ||
| ``` | ||
| 1. Note the outputs from the deployment process. These contain the resource names and/or ARNs which are used for testing. | ||
|
|
||
| ## API Endpoint | ||
|
|
||
| After running `terraform apply`, you will see outputs including the API endpoint URL. You'll need this URL for testing. The output will look similar to: | ||
| ``` | ||
| api_endpoint = "https://xxxxx.execute-api.us-east-1.amazonaws.com/Prod" | ||
| ``` | ||
|
|
||
| Note: When testing, append `/123?order=ORD12345` to this base URL. For example, if your API endpoint is `https://xxxxx.execute-api.us-east-1.amazonaws.com/Prod`, your full testing URL would be: | ||
| ``` | ||
| `https://xxxxx.execute-api.us-east-1.amazonaws.com/Prod/123?order=ORD12345` | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| The data model is declared in the API Gateway resource. The Lambda function then requires the request body to be validated against this model. | ||
|
|
||
| ## Testing | ||
|
|
||
| After the application is deployed try the following scenarios. | ||
|
|
||
| ### Create a new vehicle entering valid data: | ||
| ``` | ||
| curl --location --request POST 'https://t9nde3gpp2.execute-api.us-east-1.amazonaws.com/Prod/123?order=ORD12345' \ | ||
| --header 'Content-Type: application/json' \ | ||
| --header 'custom-agent: MyMobileApp/1.0' \ | ||
| --data-raw '{ | ||
| "make":"MINI", | ||
| "model":"Countryman", | ||
| "year": 2010 | ||
| }' | ||
| ``` | ||
| Expected response: `{"message": "Data validation succeded", "data": {"make": "MINI", "model": "Countryman", "year": 2010}}` | ||
| ### Now enter a year less than 2010 | ||
| ``` | ||
| curl --location --request POST 'https://t9nde3gpp2.execute-api.us-east-1.amazonaws.com/Prod/123?order=ORD12345' \ | ||
| --header 'Content-Type: application/json' \ | ||
| --header 'custom-agent: MyMobileApp/1.0' \ | ||
| --data-raw '{ | ||
| "make":"MINI", | ||
| "model":"Countryman", | ||
| "year": 2002 | ||
| }' | ||
| ``` | ||
| Expected response: `{"message": "Invalid request body"}` | ||
|
|
||
| Try some other combinations and see what you get! | ||
|
|
||
| ## Cleanup | ||
|
|
||
| 1. Change directory to the pattern directory: | ||
| ``` | ||
| cd apigw-data-validation-tf | ||
| ``` | ||
| 1. Delete all created resources by Terraform | ||
| ```bash | ||
| terraform destroy | ||
| ``` | ||
| 1. Confirm all created resources has been deleted | ||
| ```bash | ||
| terraform show | ||
| ``` | ||
|
|
||
| ---- | ||
| Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: MIT-0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| { | ||
| "title": "API Gateway data validation", | ||
| "description": "Creates an API Gateway with request validation, rejecting invalid requests before Lambda invocation using model schema validation.", | ||
| "language": "YAML", | ||
| "level": "300", | ||
| "framework": "Terraform", | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "The data model is declared in the API Gateway resource. The Lambda function then requires the request body to be validated against this model." | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-data-validation-tf", | ||
| "templateURL": "serverless-patterns/apigw-data-validation-tf", | ||
| "projectFolder": "apigw-data-validation-tf", | ||
| "templateFile": "main.tf" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "API Gateway model example", | ||
| "link": "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-models" | ||
| }, | ||
| { | ||
| "text": "JSON Schema", | ||
| "link": "https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-04#section-4.1" | ||
| } | ||
| ] | ||
| }, | ||
| "deploy": { | ||
| "text": [ | ||
| "terraform init", | ||
| "terraform apply" | ||
| ] | ||
| }, | ||
| "testing": { | ||
| "text": [ | ||
| "See the Github repo for detailed testing instructions." | ||
| ] | ||
| }, | ||
| "cleanup": { | ||
| "text": [ | ||
| "<code>terraform destroy</code>", | ||
| "terraform show" | ||
| ] | ||
| }, | ||
| "authors": [ | ||
| { | ||
| "name": "Makendran G", | ||
| "image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing", | ||
| "bio": "Cloud Support Engineer @ AWS", | ||
| "linkedin": "makendran", | ||
| "twitter": "@MakendranG" | ||
| } | ||
| ], | ||
| "patternArch": { | ||
| "icon1": { | ||
| "x": 20, | ||
| "y": 50, | ||
| "service": "internet" | ||
| }, | ||
| "icon2": { | ||
| "x": 80, | ||
| "y": 50, | ||
| "service": "apigw", | ||
| "label": "API Gateway REST API" | ||
| }, | ||
| "line1": { | ||
| "from": "icon1", | ||
| "to": "icon2" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| { | ||
| "title": "API Gateway data validation", | ||
| "description": "Creates an API Gateway with request validation, rejecting invalid requests before Lambda invocation using model schema validation.", | ||
| "language": "YAML", | ||
| "level": "300", | ||
| "framework": "Terraform", | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "The data model is declared in the API Gateway resource. The Lambda function then requires the request body to be validated against this model." | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-data-validation-tf", | ||
| "templateURL": "serverless-patterns/apigw-data-validation-tf", | ||
| "projectFolder": "apigw-data-validation-tf", | ||
| "templateFile": "main.tf" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "API Gateway model example", | ||
| "link": "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-models" | ||
| }, | ||
| { | ||
| "text": "JSON Schema", | ||
| "link": "https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-04#section-4.1" | ||
| } | ||
| ] | ||
| }, | ||
| "deploy": { | ||
| "text": [ | ||
| "terraform init", | ||
| "terraform apply" | ||
| ] | ||
| }, | ||
| "testing": { | ||
| "text": [ | ||
| "See the Github repo for detailed testing instructions." | ||
| ] | ||
| }, | ||
| "cleanup": { | ||
| "text": [ | ||
| "<code>terraform destroy</code>", | ||
| "terraform show" | ||
| ] | ||
| }, | ||
| "authors": [ | ||
| { | ||
| "name": "Makendran G", | ||
| "image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing", | ||
| "bio": "Cloud Support Engineer @ AWS", | ||
| "linkedin": "makendran", | ||
| "twitter": "@MakendranG" | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.