Skip to content

Commit 7836880

Browse files
authored
Merge pull request #2583 from MakendranG/makendran-feature-apigw-data-validation-tf
New serverless pattern - Amazon API Gateway Validation (Terraform)
2 parents 592671d + 1f90c18 commit 7836880

File tree

5 files changed

+451
-0
lines changed

5 files changed

+451
-0
lines changed

apigw-data-validation-tf/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Amazon API Gateway data validation models
2+
3+
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.
4+
5+
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)
6+
7+
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.
8+
9+
## Requirements
10+
11+
* [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.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed
15+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the repository:
19+
```
20+
git clone https://github.com/aws-samples/serverless-patterns
21+
```
22+
1. Change directory to this pattern's directory
23+
```
24+
cd serverless-patterns/apigw-data-validation-tf
25+
```
26+
1. From the command line, initialize Terraform to downloads and install the providers defined in the configuration:
27+
```
28+
terraform init
29+
```
30+
1. From the command line, apply the configuration in the main.tf file:
31+
```
32+
terraform apply
33+
```
34+
1. Note the outputs from the deployment process. These contain the resource names and/or ARNs which are used for testing.
35+
36+
## API Endpoint
37+
38+
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:
39+
```
40+
api_endpoint = "https://xxxxx.execute-api.us-east-1.amazonaws.com/Prod"
41+
```
42+
43+
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:
44+
```
45+
`https://xxxxx.execute-api.us-east-1.amazonaws.com/Prod/123?order=ORD12345`
46+
```
47+
48+
## How it works
49+
50+
The data model is declared in the API Gateway resource. The Lambda function then requires the request body to be validated against this model.
51+
52+
## Testing
53+
54+
After the application is deployed try the following scenarios.
55+
56+
### Create a new vehicle entering valid data:
57+
```
58+
curl --location --request POST 'https://t9nde3gpp2.execute-api.us-east-1.amazonaws.com/Prod/123?order=ORD12345' \
59+
--header 'Content-Type: application/json' \
60+
--header 'custom-agent: MyMobileApp/1.0' \
61+
--data-raw '{
62+
"make":"MINI",
63+
"model":"Countryman",
64+
"year": 2010
65+
}'
66+
```
67+
Expected response: `{"message": "Data validation succeded", "data": {"make": "MINI", "model": "Countryman", "year": 2010}}`
68+
### Now enter a year less than 2010
69+
```
70+
curl --location --request POST 'https://t9nde3gpp2.execute-api.us-east-1.amazonaws.com/Prod/123?order=ORD12345' \
71+
--header 'Content-Type: application/json' \
72+
--header 'custom-agent: MyMobileApp/1.0' \
73+
--data-raw '{
74+
"make":"MINI",
75+
"model":"Countryman",
76+
"year": 2002
77+
}'
78+
```
79+
Expected response: `{"message": "Invalid request body"}`
80+
81+
Try some other combinations and see what you get!
82+
83+
## Cleanup
84+
85+
1. Change directory to the pattern directory:
86+
```
87+
cd apigw-data-validation-tf
88+
```
89+
1. Delete all created resources by Terraform
90+
```bash
91+
terraform destroy
92+
```
93+
1. Confirm all created resources has been deleted
94+
```bash
95+
terraform show
96+
```
97+
98+
----
99+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
100+
101+
SPDX-License-Identifier: MIT-0
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"title": "API Gateway data validation",
3+
"description": "Creates an API Gateway with request validation, rejecting invalid requests before Lambda invocation using model schema validation.",
4+
"language": "YAML",
5+
"level": "300",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"The data model is declared in the API Gateway resource. The Lambda function then requires the request body to be validated against this model."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-data-validation-tf",
16+
"templateURL": "serverless-patterns/apigw-data-validation-tf",
17+
"projectFolder": "apigw-data-validation-tf",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "API Gateway model example",
25+
"link": "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-models"
26+
},
27+
{
28+
"text": "JSON Schema",
29+
"link": "https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-04#section-4.1"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the Github repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"<code>terraform destroy</code>",
47+
"terraform show"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Makendran G",
53+
"image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing",
54+
"bio": "Cloud Support Engineer @ AWS",
55+
"linkedin": "makendran",
56+
"twitter": "@MakendranG"
57+
}
58+
],
59+
"patternArch": {
60+
"icon1": {
61+
"x": 20,
62+
"y": 50,
63+
"service": "internet"
64+
},
65+
"icon2": {
66+
"x": 80,
67+
"y": 50,
68+
"service": "apigw",
69+
"label": "API Gateway REST API"
70+
},
71+
"line1": {
72+
"from": "icon1",
73+
"to": "icon2"
74+
}
75+
}
76+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"title": "API Gateway data validation",
3+
"description": "Creates an API Gateway with request validation, rejecting invalid requests before Lambda invocation using model schema validation.",
4+
"language": "YAML",
5+
"level": "300",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"The data model is declared in the API Gateway resource. The Lambda function then requires the request body to be validated against this model."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-data-validation-tf",
16+
"templateURL": "serverless-patterns/apigw-data-validation-tf",
17+
"projectFolder": "apigw-data-validation-tf",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "API Gateway model example",
25+
"link": "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-models"
26+
},
27+
{
28+
"text": "JSON Schema",
29+
"link": "https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-04#section-4.1"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the Github repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"<code>terraform destroy</code>",
47+
"terraform show"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Makendran G",
53+
"image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing",
54+
"bio": "Cloud Support Engineer @ AWS",
55+
"linkedin": "makendran",
56+
"twitter": "@MakendranG"
57+
}
58+
]
59+
}

0 commit comments

Comments
 (0)