|
| 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 |
0 commit comments