|
| 1 | +# Multi-tenant API with Amazon API Gateway and AWS Lambda Tenant Isolation |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This pattern implements a serverless multi-tenant counter API using Amazon API Gateway, AWS Lambda and Amazon DynamoDB. It deploys two parallel endpoints, one without tenant isolation and one with full per-tenant isolation, to demonstrate how shared state leads to cross-tenant data leakage in multi-tenant applications. |
| 6 | + |
| 7 | +When a request hits the standard endpoint, the Lambda function increments a single shared counter in DynamoDB, exposing activity across all tenants. The isolated endpoint requires a tenant ID header, which maps to a dedicated Lambda execution environment and a tenant-specific DynamoDB row. Each tenant gets an independent counter, ensuring complete data separation. |
| 8 | + |
| 9 | +Please note that this pattern deploys API Gateway endpoints with no authorization. Production deployments should secure all endpoints with an appropriate authorization mechanism such as AWS IAM authorization, Amazon Cognito user pools, or API key usage plans. |
| 10 | + |
| 11 | +Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-ddb-tenant-isolation-terraform |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +* [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. |
| 18 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 19 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 20 | +* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed |
| 21 | + |
| 22 | +## Deployment Instructions |
| 23 | + |
| 24 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 25 | + ``` |
| 26 | + git clone https://github.com/aws-samples/serverless-patterns |
| 27 | + ``` |
| 28 | +1. Change directory to the pattern directory: |
| 29 | + ``` |
| 30 | + cd lambda-ddb-tenant-isolation-terraform |
| 31 | + ``` |
| 32 | +1. From the command line, initialize terraform to downloads and installs the providers defined in the configuration: |
| 33 | + ``` |
| 34 | + terraform init |
| 35 | + ``` |
| 36 | +1. From the command line, apply the configuration in the main.tf file: |
| 37 | + ``` |
| 38 | + terraform apply -auto-approve |
| 39 | + ``` |
| 40 | +1. During the prompts |
| 41 | + #var.aws_region |
| 42 | + - Enter a value: {enter the region for deployment} |
| 43 | +
|
| 44 | + #var.prefix |
| 45 | + - Enter a value: {enter any prefix to associate with resources} |
| 46 | +
|
| 47 | +1. Note the outputs from the Terraform deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 48 | +
|
| 49 | +## Testing |
| 50 | +
|
| 51 | +Use [curl](https://curl.se/) to send a HTTP GET request to the API. |
| 52 | +
|
| 53 | +1. Make a GET request to the Standard API endpoint using the following cURL command: |
| 54 | +``` |
| 55 | +curl -H "x-tenant-id: TENANT_ID" "STANDARD_API_ENDPOINT" |
| 56 | +``` |
| 57 | +Note: Replace the `TENANT_ID` with a unique Tenant ID of your choice and `STANDARD_API_ENDPOINT` with the generated `standard_multi_tenant_api_endpoint_url` from Terraform (refer to the Terraform Outputs section) |
| 58 | +
|
| 59 | +For ex, |
| 60 | +``` |
| 61 | +curl -H "x-tenant-id: Tenant-1" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/standard" |
| 62 | +``` |
| 63 | +
|
| 64 | +The response would be, |
| 65 | +``` |
| 66 | +{"counter": 1, "tenant_id": "Tenant-1", "isolation_enabled": false, "message": "This function does NOT provide tenant isolation and every tenant reads and writes the same DynamoDB row. Incremented counter is shared across all the tenants"} |
| 67 | +``` |
| 68 | +
|
| 69 | +Test this for another Tenant ID. For ex, |
| 70 | +``` |
| 71 | +curl -H "x-tenant-id: Tenant-2" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/standard" |
| 72 | +``` |
| 73 | +
|
| 74 | +The response would be, |
| 75 | +``` |
| 76 | +{"counter": 2, "tenant_id": "Tenant-2", "isolation_enabled": false, "message": "This function does NOT provide tenant isolation and every tenant reads and writes the same DynamoDB row. Incremented counter is shared across all the tenants"} |
| 77 | +``` |
| 78 | +
|
| 79 | +1. Now make a GET request to the Isolated API endpoint using the following cURL command: |
| 80 | +``` |
| 81 | +curl -H "x-tenant-id: TENANT_ID" "ISOLATED_API_ENDPOINT" |
| 82 | +``` |
| 83 | +Note: Replace the `TENANT_ID` with a unique Tenant ID of your choice and `ISOLATED_API_ENDPOINT` with the generated `isolated_tenant_api_endpoint_url` from Terraform (refer to the Terraform Outputs section) |
| 84 | +
|
| 85 | +For ex, |
| 86 | +``` |
| 87 | +curl -H "x-tenant-id: Tenant-1" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/isolated" |
| 88 | +``` |
| 89 | +
|
| 90 | +The response would be, |
| 91 | +``` |
| 92 | +{"counter": 1, "tenant_id": "Tenant-1", "isolation_enabled": true, "message": "Counter incremented for tenant Tenant-1"} |
| 93 | +``` |
| 94 | +
|
| 95 | +Test this for another Tenant ID. For ex, |
| 96 | +``` |
| 97 | +curl -H "x-tenant-id: Tenant-2" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/isolated" |
| 98 | +``` |
| 99 | +
|
| 100 | +The response would be, |
| 101 | +``` |
| 102 | +{"counter": 2, "tenant_id": "Tenant-2", "isolation_enabled": true, "message": "Counter incremented for tenant Tenant-2"} |
| 103 | +``` |
| 104 | +
|
| 105 | +## Cleanup |
| 106 | +
|
| 107 | +1. Change directory to the pattern directory: |
| 108 | + ``` |
| 109 | + cd serverless-patterns/lambda-ddb-tenant-isolation-terraform |
| 110 | + ``` |
| 111 | +
|
| 112 | +1. Delete all created resources |
| 113 | + ``` |
| 114 | + terraform destroy -auto-approve |
| 115 | + ``` |
| 116 | + |
| 117 | +1. During the prompts: |
| 118 | + ``` |
| 119 | + Enter all details as entered during creation. |
| 120 | + ``` |
| 121 | +
|
| 122 | +1. Confirm all created resources has been deleted |
| 123 | + ``` |
| 124 | + terraform show |
| 125 | + ``` |
| 126 | +---- |
| 127 | +Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 128 | +
|
| 129 | +SPDX-License-Identifier: MIT-0 |
0 commit comments