|
| 1 | +# AWS Lambda Tenant Isolation with Amazon SQS |
| 2 | + |
| 3 | +This pattern demonstrates AWS Lambda's tenant isolation feature in Multi-tenant application. It uses single Amazon SQS for multi-tenant application and isolating messages using MessageGroupId and invoking isolated AWS Lambda environments. |
| 4 | + |
| 5 | +## Key Features |
| 6 | + |
| 7 | +- Tenant isolation at infrastructure level (no custom routing logic) |
| 8 | +- Execution environments never shared between tenants |
| 9 | +- Asynchronous invocation pattern |
| 10 | +- Automatic tenant context propagation |
| 11 | + |
| 12 | +Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/sqs-lambda-tenant-isolation-sam-py) |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +## Requirements |
| 17 | + |
| 18 | +* [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. |
| 19 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 20 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 21 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 22 | + |
| 23 | +## How it works |
| 24 | + |
| 25 | +<img width="535" height="183" alt="image" src="https://github.com/user-attachments/assets/b6af3efa-e81b-4a08-80ca-b7536934d490" /> |
| 26 | + |
| 27 | +### 1. SQS Processor (`sqs-processor/`) |
| 28 | +- Triggered by SQS queue messages |
| 29 | +- Invokes tenant-isolated Lambda asynchronously |
| 30 | + |
| 31 | +### 2. Tenant-Isolated Processor (`tenant-isolated-processor/`) |
| 32 | +- Configured with tenant isolation mode enabled |
| 33 | +- Processes requests in isolated execution environments per tenant using message-group-id |
| 34 | + |
| 35 | +## Message Format |
| 36 | + |
| 37 | +```json |
| 38 | +{ |
| 39 | + "data": "your payload here" |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Deployment Instructions |
| 44 | + |
| 45 | +```bash |
| 46 | +sam build |
| 47 | +sam deploy --guided |
| 48 | +``` |
| 49 | + |
| 50 | +## Testing |
| 51 | + |
| 52 | +Step 1: |
| 53 | +After deploying infrastructure using SAM, run below command to get SQS Queue URL. Replace <your-stack-name> with your cloudformation stack name. |
| 54 | + |
| 55 | +```bash |
| 56 | +aws cloudformation describe-stacks \ |
| 57 | + --stack-name <your-stack-name> \ |
| 58 | + --query "Stacks[0].Outputs[?OutputKey=='MyQueueUrl'].OutputValue" \ |
| 59 | + --output text |
| 60 | +``` |
| 61 | + |
| 62 | +Step 2: |
| 63 | +You send messages to the SQS queue with --message-group-id set to a tenant identifier. Use below CLI command to send-message. Make sure to set --message-group-id as tenant name. Send multiple messages with different tenant name |
| 64 | + |
| 65 | +```bash |
| 66 | +aws sqs send-message \ |
| 67 | + --queue-url <QUEUE_URL> \ |
| 68 | + --message-body '{"data": "test payload"}' \ |
| 69 | + --message-group-id "tenant-blue" |
| 70 | +``` |
| 71 | + |
| 72 | +```bash |
| 73 | +aws sqs send-message \ |
| 74 | + --queue-url <QUEUE_URL> \ |
| 75 | + --message-body '{"data": "test payload"}' \ |
| 76 | + --message-group-id "tenant-green" |
| 77 | +``` |
| 78 | + |
| 79 | +Step 3: |
| 80 | +The SQS processor Lambda picks up the message, reads the MessageGroupId from the SQS record attributes, and passes it as the TenantId when invoking the tenant-isolated LambdaAfter dropping the message, review cloudwatch log for Tenant-Isolated Lambda. |
| 81 | + |
| 82 | +```bash |
| 83 | +aws logs describe-log-streams \ |
| 84 | + --log-group-name /aws/lambda/tenant-isolated-processor \ |
| 85 | + --order-by LastEventTime \ |
| 86 | + --descending |
| 87 | +``` |
| 88 | + |
| 89 | +Different log streams should be created for each tenant. |
| 90 | + |
| 91 | +## Cleanup |
| 92 | + |
| 93 | +Delete the stack |
| 94 | + |
| 95 | +```bash |
| 96 | +sam delete |
| 97 | +``` |
| 98 | +---- |
| 99 | +Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 100 | + |
| 101 | +SPDX-License-Identifier: MIT-0 |
0 commit comments