Skip to content

Commit abc0413

Browse files
authored
Merge pull request #2964 from archiev4/archiev4-feature-lambda-ddb-tenant-isolation-terraform
New serverless pattern - lambda-ddb-tenant-isolation-terraform
2 parents 9745536 + 9dfd8a7 commit abc0413

5 files changed

Lines changed: 1257 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Multi-tenant API with Amazon API Gateway and AWS Lambda Tenant Isolation
2+
3+
![architecture](architecture/architecture.png)
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
19.7 KB
Loading
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"title": "Multi-tenant API with Amazon API Gateway and AWS Lambda Tenant Isolation",
3+
"description": "This pattern implements a serverless multi-tenant API using Amazon API Gateway, AWS Lambda and Amazon DynamoDB to demonstrate tenant isolation.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This solution works by exposing two API Gateway endpoints, /standard and /isolated, each backed by a separate Lambda function. When a request hits the /standard endpoint, the Lambda function increments a single shared counter row in DynamoDB, meaning all tenants read and write the same value. When a request hits the /isolated endpoint with an x-tenant-id header, API Gateway maps the header to the Lambda execution context, ensuring a dedicated execution environment per tenant, and the Lambda function increments a tenant-specific counter row in DynamoDB, keeping each tenant's data completely separate."
11+
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-ddb-tenant-isolation-terraform",
17+
"templateURL": "serverless-patterns/lambda-ddb-tenant-isolation-terraform",
18+
"projectFolder": "lambda-ddb-tenant-isolation-terraform",
19+
"templateFile": "main.tf"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Lambda Tenant Isolation",
26+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/tenant-isolation.html"
27+
}
28+
]
29+
},
30+
"deploy": {
31+
"text": [
32+
"terraform init",
33+
"terraform apply"
34+
]
35+
},
36+
"testing": {
37+
"text": [
38+
"See the GitHub repo for detailed testing instructions."
39+
]
40+
},
41+
"cleanup": {
42+
"text": [
43+
"terraform destroy",
44+
"terraform show"
45+
]
46+
},
47+
"authors": [
48+
{
49+
"name": "Archana V",
50+
"image": "https://media.licdn.com/dms/image/v2/D5603AQGhkVtEhllFEw/profile-displayphoto-shrink_200_200/B56ZZH3LL6H0AY-/0/1744962369852?e=1772668800&v=beta&t=y0t7bsQKJwy5McO395hDmW7QPu_K-a1WKXeTA0-ecno",
51+
"bio": "Solutions Architect at AWS",
52+
"linkedin": "archanavenkat"
53+
}
54+
]
55+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"title": "Multi-tenant API with Amazon API Gateway and AWS Lambda Tenant Isolation",
3+
"description": "This pattern implements a serverless multi-tenant API using Amazon API Gateway, AWS Lambda and Amazon DynamoDB to demonstrate tenant isolation.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This solution works by exposing two API Gateway endpoints, /standard and /isolated, each backed by a separate Lambda function. When a request hits the /standard endpoint, the Lambda function increments a single shared counter row in DynamoDB, meaning all tenants read and write the same value. When a request hits the /isolated endpoint with an x-tenant-id header, API Gateway maps the header to the Lambda execution context, ensuring a dedicated execution environment per tenant, and the Lambda function increments a tenant-specific counter row in DynamoDB, keeping each tenant's data completely separate."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-ddb-tenant-isolation-terraform",
16+
"templateURL": "serverless-patterns/lambda-ddb-tenant-isolation-terraform",
17+
"projectFolder": "lambda-ddb-tenant-isolation-terraform",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Lambda Tenant Isolation",
25+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/tenant-isolation.html"
26+
}
27+
]
28+
},
29+
"deploy": {
30+
"text": [
31+
"terraform init",
32+
"terraform apply"
33+
]
34+
},
35+
"testing": {
36+
"text": [
37+
"See the GitHub repo for detailed testing instructions."
38+
]
39+
},
40+
"cleanup": {
41+
"text": [
42+
"terraform destroy",
43+
"terraform show"
44+
]
45+
},
46+
"authors": [
47+
{
48+
"name": "Archana V",
49+
"image": "https://media.licdn.com/dms/image/v2/D5603AQGhkVtEhllFEw/profile-displayphoto-shrink_200_200/B56ZZH3LL6H0AY-/0/1744962369852?e=1772668800&v=beta&t=y0t7bsQKJwy5McO395hDmW7QPu_K-a1WKXeTA0-ecno",
50+
"bio": "Solutions Architect at AWS",
51+
"linkedin": "archanavenkat"
52+
}
53+
],
54+
"patternArch": {
55+
"icon1": {
56+
"x": 15,
57+
"y": 50,
58+
"service": "apigw",
59+
"label": "API Gateway"
60+
},
61+
"icon2": {
62+
"x": 50,
63+
"y": 20,
64+
"service": "lambda",
65+
"label": "AWS Lambda"
66+
},
67+
"icon3": {
68+
"x": 50,
69+
"y": 70,
70+
"service": "lambda",
71+
"label": "AWS Lambda"
72+
},
73+
"icon4": {
74+
"x": 80,
75+
"y": 20,
76+
"service": "dynamodb",
77+
"label": "DynamoDB"
78+
},
79+
"icon5": {
80+
"x": 80,
81+
"y": 70,
82+
"service": "dynamodb",
83+
"label": "DynamoDB"
84+
},
85+
"line1": {
86+
"from": "icon1",
87+
"to": "icon2",
88+
"label": "/standard"
89+
},
90+
"line2": {
91+
"from": "icon1",
92+
"to": "icon3",
93+
"label": "/isolated"
94+
},
95+
"line3": {
96+
"from": "icon3",
97+
"to": "icon5",
98+
"label": ""
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)