-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Add Lambda Managed Instances pattern (Python, SAM/CloudFormation) #3253
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
awsTest1992
wants to merge
5
commits into
aws-samples:main
Choose a base branch
from
awsTest1992:lambda-managed-instances-python-sam
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.
+475
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
57fa931
feat: Add saga pattern with Lambda durable functions (Python)
awsTest1992 946ce86
fix: Address reviewer feedback from ellisms
awsTest1992 6f92327
feat: Add Lambda Managed Instances pattern (Python, SAM/CloudFormation)
awsTest1992 f2be818
fix: Address reviewer feedback on lambda-managed-instances-python-sam
awsTest1992 7d18bfd
docs: Address round-2 review feedback on README
awsTest1992 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,116 @@ | ||
| # AWS Lambda Managed Instances with AWS SAM (Python) | ||
|
|
||
| This pattern deploys a Python Lambda function running on AWS Lambda Managed Instances using AWS SAM. Lambda Managed Instances enables you to run functions on Amazon EC2 instances while AWS handles lifecycle management, patching, routing, and scaling. You benefit from EC2 pricing (Savings Plans, Reserved Instances) and multi-concurrency support. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/lambda-managed-instances-python-sam](https://serverlessland.com/patterns/lambda-managed-instances-python-sam) | ||
|
|
||
| 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. | ||
|
|
||
| **Note**: Lambda Managed Instances provision EC2 instances that are **NOT eligible for the AWS Free Tier**. Instances incur charges immediately upon deployment. | ||
|
|
||
| ## 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. | ||
| - [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) | ||
| - [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) installed (v1.164.0+) | ||
| - [Python 3.13](https://www.python.org/downloads/) installed and available in your PATH | ||
|
|
||
| ## 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-managed-instances-python-sam | ||
| ``` | ||
| 1. From the command line, use AWS SAM to build and deploy: | ||
| ``` | ||
| sam build | ||
| sam deploy --guided --capabilities CAPABILITY_NAMED_IAM | ||
| ``` | ||
| `CAPABILITY_NAMED_IAM` is required because the template creates a named IAM role for the capacity provider operator. | ||
| 1. During the prompts: | ||
| - Enter a stack name | ||
| - Enter the desired AWS Region | ||
| - Allow SAM CLI to create IAM roles with the required permissions. | ||
|
|
||
| Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. | ||
|
|
||
| 1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. | ||
|
|
||
| ## How it works | ||
|
|
||
| This pattern creates: | ||
|
|
||
| 1. **VPC with private subnets**: Two private subnets across availability zones for the capacity provider. The subnets use a dedicated private route table with no internet route (no NAT, no internet gateway). Egress to AWS services stays inside the VPC via PrivateLink: interface endpoints for Amazon CloudWatch Logs, Amazon ECR (`ecr.api` and `ecr.dkr`), and Amazon EC2, plus an Amazon S3 gateway endpoint for pulling the runtime image layers. | ||
|
|
||
| 2. **Capacity Provider Operator IAM Role**: An Amazon IAM role with the `AWSLambdaManagedEC2ResourceOperator` managed policy that Lambda uses to provision and manage EC2 instances. | ||
|
|
||
| 3. **Lambda Capacity Provider**: Defines where functions run — VPC config, instance architecture (arm64 / AWS Graviton), and the operator role for instance management. | ||
|
|
||
| 4. **Lambda function on Managed Instances**: A Python function attached to the capacity provider via `CapacityProviderConfig`. Once a version is published, Lambda provisions instances and starts execution environments. | ||
|
|
||
| 5. **Multi-concurrency**: Unlike default Lambda (1 invocation per environment), Managed Instances support multiple concurrent invocations per environment. The example uses thread-safe patterns to demonstrate this. | ||
|
|
||
| ### When to use Managed Instances | ||
|
|
||
| - High-volume, predictable workloads (steady-state traffic) | ||
| - Cost optimization via EC2 Savings Plans or Reserved Instances | ||
| - Performance-critical apps needing specific CPU/network characteristics | ||
| - Regulatory requirements needing VPC placement control | ||
|
|
||
| ### Key constraints | ||
|
|
||
| - Minimum `MemorySize` is 2048 MB (2 GB) | ||
| - A published version or alias is required for invocation | ||
| - Capacity provider scales within 5 minutes for traffic doubling | ||
|
|
||
| ## Testing | ||
|
|
||
| 1. Invoke the function via the `live` alias: | ||
|
|
||
| ```bash | ||
| aws lambda invoke \ | ||
| --function-name '<STACK_NAME>-api-handler' \ | ||
| --qualifier live \ | ||
| --cli-binary-format raw-in-base64-out \ | ||
| --payload '{"name": "Serverless Land"}' \ | ||
| /tmp/response.json && cat /tmp/response.json | ||
| ``` | ||
|
|
||
| 2. Expected response: | ||
|
|
||
| ```json | ||
| { | ||
| "statusCode": 200, | ||
| "body": "{\"message\": \"Hello, Serverless Land!\", \"invocation\": 1, \"timestamp\": \"2026-09-16T14:30:00.123456+00:00\", \"version\": \"1\"}" | ||
| } | ||
| ``` | ||
|
|
||
| 3. Test multi-concurrency by invoking in parallel: | ||
| ```bash | ||
| for i in $(seq 1 10); do | ||
| aws lambda invoke \ | ||
| --function-name '<STACK_NAME>-api-handler' \ | ||
| --qualifier live \ | ||
| --cli-binary-format raw-in-base64-out \ | ||
| --payload "{\"name\": \"Request-$i\"}" \ | ||
| /tmp/response-$i.json & | ||
| done | ||
| wait | ||
| ``` | ||
|
|
||
| ## Cleanup | ||
|
|
||
| ```bash | ||
| sam delete | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: MIT-0 | ||
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,54 @@ | ||
| { | ||
| "title": "Lambda Managed Instances with SAM (Python)", | ||
| "description": "Deploy a Python Lambda function on AWS-managed EC2 instances with multi-concurrency, Graviton4 support, and EC2 pricing advantages.", | ||
| "language": "Python", | ||
| "level": "300", | ||
| "framework": "SAM", | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "This pattern creates a Lambda Capacity Provider with VPC configuration and deploys a Python function that runs on managed EC2 instances.", | ||
| "Lambda handles instance lifecycle, OS patching, routing, and auto-scaling while you benefit from EC2 pricing models.", | ||
| "Multi-concurrency allows one execution environment to handle multiple invocations simultaneously." | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-managed-instances-python-sam", | ||
| "templateURL": "serverless-patterns/lambda-managed-instances-python-sam", | ||
| "projectFolder": "lambda-managed-instances-python-sam", | ||
| "templateFile": "template.yaml" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "Lambda Managed Instances", | ||
| "link": "https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances.html" | ||
| }, | ||
| { | ||
| "text": "Build high-performance apps with Lambda Managed Instances", | ||
| "link": "https://aws.amazon.com/blogs/compute/build-high-performance-apps-with-aws-lambda-managed-instances/" | ||
| } | ||
| ] | ||
| }, | ||
| "deploy": { | ||
| "text": ["sam deploy --guided"], | ||
| "file": "template.yaml" | ||
| }, | ||
| "testing": { | ||
| "text": ["See the GitHub repo for detailed testing instructions."], | ||
| "file": "README.md" | ||
| }, | ||
| "cleanup": { | ||
| "text": ["Delete the stack: <code>sam delete</code>"], | ||
| "file": "template.yaml" | ||
| }, | ||
| "authors": [ | ||
| { | ||
| "name": "Ajaya Shrestha", | ||
| "bio": "Cloud Support Engineer at AWS", | ||
| "linkedin": "ajaya-shrestha" | ||
| } | ||
| ] | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provide a sample expected response