generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 1k
New serverless pattern - lambda-ddb-tenant-isolation-terraform #2964
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
Open
archiev4
wants to merge
3
commits into
aws-samples:main
Choose a base branch
from
archiev4:archiev4-feature-lambda-ddb-tenant-isolation-terraform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,129 @@ | ||
| # Multi-tenant API with Amazon API Gateway and AWS Lambda Tenant Isolation | ||
|
|
||
|  | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-ddb-tenant-isolation-terraform | ||
|
|
||
| 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 GitHub repository: | ||
| ``` | ||
| git clone https://github.com/aws-samples/serverless-patterns | ||
| ``` | ||
| 1. Change directory to the pattern directory: | ||
| ``` | ||
| cd lambda-ddb-tenant-isolation-terraform | ||
| ``` | ||
| 1. From the command line, initialize terraform to downloads and installs the providers defined in the configuration: | ||
| ``` | ||
| terraform init | ||
| ``` | ||
| 1. From the command line, apply the configuration in the main.tf file: | ||
| ``` | ||
| terraform apply -auto-approve | ||
| ``` | ||
| 1. During the prompts | ||
| #var.aws_region | ||
| - Enter a value: {enter the region for deployment} | ||
|
|
||
| #var.prefix | ||
| - Enter a value: {enter any prefix to associate with resources} | ||
|
|
||
| 1. Note the outputs from the Terraform deployment process. These contain the resource names and/or ARNs which are used for testing. | ||
|
|
||
| ## Testing | ||
|
|
||
| Use [curl](https://curl.se/) to send a HTTP GET request to the API. | ||
|
|
||
| 1. Make a GET request to the Standard API endpoint using the following cURL command: | ||
| ``` | ||
| curl -H "x-tenant-id: TENANT_ID" "STANDARD_API_ENDPOINT" | ||
| ``` | ||
| 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) | ||
|
|
||
| For ex, | ||
| ``` | ||
| curl -H "x-tenant-id: Tenant-1" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/standard" | ||
| ``` | ||
|
|
||
| The response would be, | ||
| ``` | ||
| {"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"} | ||
| ``` | ||
|
|
||
| Test this for another Tenant ID. For ex, | ||
| ``` | ||
| curl -H "x-tenant-id: Tenant-2" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/standard" | ||
| ``` | ||
|
|
||
| The response would be, | ||
| ``` | ||
| {"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"} | ||
| ``` | ||
|
|
||
| 1. Now make a GET request to the Isolated API endpoint using the following cURL command: | ||
| ``` | ||
| curl -H "x-tenant-id: TENANT_ID" "ISOLATED_API_ENDPOINT" | ||
| ``` | ||
| 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) | ||
|
|
||
| For ex, | ||
| ``` | ||
| curl -H "x-tenant-id: Tenant-1" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/isolated" | ||
| ``` | ||
|
|
||
| The response would be, | ||
| ``` | ||
| {"counter": 1, "tenant_id": "Tenant-1", "isolation_enabled": true, "message": "Counter incremented for tenant Tenant-1"} | ||
| ``` | ||
|
|
||
| Test this for another Tenant ID. For ex, | ||
| ``` | ||
| curl -H "x-tenant-id: Tenant-2" "https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/isolated" | ||
| ``` | ||
|
|
||
| The response would be, | ||
| ``` | ||
| {"counter": 2, "tenant_id": "Tenant-2", "isolation_enabled": true, "message": "Counter incremented for tenant Tenant-2"} | ||
| ``` | ||
|
|
||
| ## Cleanup | ||
|
|
||
| 1. Change directory to the pattern directory: | ||
| ``` | ||
| cd serverless-patterns/lambda-ddb-tenant-isolation-terraform | ||
| ``` | ||
|
|
||
| 1. Delete all created resources | ||
| ``` | ||
| terraform destroy -auto-approve | ||
| ``` | ||
|
|
||
| 1. During the prompts: | ||
| ``` | ||
| Enter all details as entered during creation. | ||
| ``` | ||
|
|
||
| 1. Confirm all created resources has been deleted | ||
| ``` | ||
| terraform show | ||
| ``` | ||
| ---- | ||
| Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: MIT-0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions
55
lambda-ddb-tenant-isolation-terraform/example-pattern.json
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,55 @@ | ||
| { | ||
| "title": "Multi-tenant API with Amazon API Gateway and AWS Lambda Tenant Isolation", | ||
| "description": "This pattern implements a serverless multi-tenant API using Amazon API Gateway, AWS Lambda and Amazon DynamoDB to demonstrate tenant isolation.", | ||
| "language": "Python", | ||
| "level": "200", | ||
| "framework": "Terraform", | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "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." | ||
|
|
||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-ddb-tenant-isolation-terraform", | ||
| "templateURL": "serverless-patterns/lambda-ddb-tenant-isolation-terraform", | ||
| "projectFolder": "lambda-ddb-tenant-isolation-terraform", | ||
| "templateFile": "main.tf" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "Lambda Tenant Isolation", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/tenant-isolation.html" | ||
| } | ||
| ] | ||
| }, | ||
| "deploy": { | ||
| "text": [ | ||
| "terraform init", | ||
| "terraform apply" | ||
| ] | ||
| }, | ||
| "testing": { | ||
| "text": [ | ||
| "See the GitHub repo for detailed testing instructions." | ||
| ] | ||
| }, | ||
| "cleanup": { | ||
| "text": [ | ||
| "terraform destroy", | ||
| "terraform show" | ||
| ] | ||
| }, | ||
| "authors": [ | ||
| { | ||
| "name": "Archana V", | ||
| "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", | ||
| "bio": "Solutions Architect at AWS", | ||
| "linkedin": "archanavenkat" | ||
| } | ||
| ] | ||
| } |
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.