Skip to content

feat: Add Lambda Managed Instances pattern (Python, SAM/CloudFormation) - #3253

Open
awsTest1992 wants to merge 5 commits into
aws-samples:mainfrom
awsTest1992:lambda-managed-instances-python-sam
Open

awsTest1992 wants to merge 5 commits into
aws-samples:mainfrom
awsTest1992:lambda-managed-instances-python-sam

Conversation

@awsTest1992

Copy link
Copy Markdown
Contributor

Issue #, if available:

Description of changes:

Adds a SAM/CloudFormation-based pattern for AWS Lambda Managed Instances with Python. This fills the gap where CDK and Terraform versions exist but no SAM equivalent was available.

Pattern includes:

  • VPC with private subnets
  • Lambda Capacity Provider (ARM64/Graviton4)
  • Capacity Provider Operator IAM role (AWSLambdaManagedEC2ResourceOperator)
  • Python 3.13 function with multi-concurrency (thread-safe)
  • Published version + alias for invocation
  • CloudWatch Logs with 14-day retention

Tested: Deployed and invoked successfully in us-east-1.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

- Narrow IAM wildcard: orchestrator* -> orchestrator:*
- Fix casing: 'Durable Lambda Function' -> 'Lambda durable function'
- Use single quotes for function-name in test commands
- Fix docstring casing: 'Durable Functions' -> 'durable functions'
- Add CloudWatch Logs group with 14-day retention

@ellisms ellisms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @awsTest1992 I started reviewing this and have 2 initial findings that need to be reviewed before I test:

  1. It looks like the route tables are missing from the VPC/subnet definitions. Can you confirm this will still be a valid deploy? See the networking documentation: https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-networking.html
  2. A second directory, lambda-durable-saga-python-sam, is included in this PR. Was that intended?

Comment thread lambda-managed-instances-python-sam/template.yaml
Comment thread lambda-managed-instances-python-sam/template.yaml
@@ -0,0 +1,115 @@
# Lambda Managed Instances with SAM (Python)

This pattern deploys a Python Lambda function running on AWS Lambda Managed Instances using CloudFormation. Lambda Managed Instances enables you to run functions on EC2 instances while AWS handles lifecycle management, patching, routing, and scaling. You benefit from EC2 pricing (Savings Plans, Reserved Instances) and multi-concurrency support.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This pattern deploys a Python Lambda function running on AWS Lambda Managed Instances using CloudFormation. Lambda Managed Instances enables you to run functions on EC2 instances while AWS handles lifecycle management, patching, routing, and scaling. You benefit from EC2 pricing (Savings Plans, Reserved Instances) and multi-concurrency support.
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 EC2 instances while AWS handles lifecycle management, patching, routing, and scaling. You benefit from EC2 pricing (Savings Plans, Reserved Instances) and multi-concurrency support.


2. **Capacity Provider Operator IAM Role**: An 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/Graviton4), and the operator role for instance management.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
3. **Lambda Capacity Provider**: Defines where functions run — VPC config, instance architecture (ARM64/Graviton4), and the operator role for instance management.
3. **Lambda Capacity Provider**: Defines where functions run — VPC config, instance architecture (arm64 / AWS Graviton), and the operator role for instance management.

Address @ellisms review on PR aws-samples#3253:
- Add private route table + subnet associations so the private subnets
  have an explicit table (was missing, using only the VPC main table)
- Add PrivateLink egress with no internet route: interface endpoints for
  CloudWatch Logs, ECR (api + dkr), and EC2, plus an S3 gateway endpoint,
  so Managed Instances can send logs and pull the runtime image privately
- Remove the accidental lambda-durable-saga-python-sam directory
- Remove src/ and keep the inline ZipFile as the single source of truth
- README: 'using CloudFormation' -> 'using AWS SAM'; 'ARM64/Graviton4' ->
  'arm64 / AWS Graviton'; document the private networking

Verified: sam validate --lint and cfn-lint pass clean; deployed to a test
account, confirmed private route table + endpoints, successful invoke on
managed instances, and log delivery via the private logs endpoint; stack
deleted after testing.
@awsTest1992

Copy link
Copy Markdown
Contributor Author

Thanks for the review @ellisms! I've pushed a commit addressing all the feedback.

1. Missing route tables / networking
Fixed. The private subnets now have a dedicated PrivateRouteTable with explicit associations for both subnets. Rather than adding a NAT/internet path, I kept the pattern fully private and added PrivateLink egress:

  • Interface endpoints for CloudWatch Logs, ECR (ecr.api + ecr.dkr), and EC2 (private DNS enabled, both subnets)
  • An S3 gateway endpoint (bound to the private route table) for pulling the runtime image layers
  • An endpoint security group allowing 443 only from the instance SG

The route table has only the local route + the S3 prefix-list route — no 0.0.0.0/0 — so there's no internet egress, which matches the "private subnets / VPC placement control" framing of the pattern.

2. Stray lambda-durable-saga-python-sam directory
Not intended — it rode in from the base branch. Removed from this PR.

3. Inline code vs src/
Agreed. Removed the src/ directory; the inline ZipFile is now the single source of truth.

4 & 5. README wording

  • "using CloudFormation" → "using AWS SAM"
  • "ARM64/Graviton4" → "arm64 / AWS Graviton"
  • Also updated the "How it works" networking bullet to describe the route table + endpoints.

Verification

  • sam validate --lint and cfn-lint both pass clean.
  • Deployed end-to-end to a test account: confirmed the private route table + both associations, all 5 endpoints available, a successful invoke via the live alias, and — importantly — log delivery to CloudWatch through the private logs endpoint (initializationType: lambda-managed-instances, status success). Stack was deleted after testing.

Ready for another look when you have a chance. Thanks!

@ellisms ellisms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @awsTest1992 . Requesting a few additional minor changes.

1. From the command line, use AWS SAM to build and deploy:
```
sam build
sam deploy --guided

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During testing I had to add --capabilities CAPABILITY_NAMED_IAM to sam deploy.

}
```

3. Test multi-concurrency by invoking in parallel:

Copy link
Copy Markdown
Contributor

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

@@ -0,0 +1,115 @@
# Lambda Managed Instances with SAM (Python)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Lambda Managed Instances with SAM (Python)
# AWS Lambda Managed Instances with AWS SAM (Python)

@@ -0,0 +1,115 @@
# Lambda Managed Instances with 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 EC2 instances while AWS handles lifecycle management, patching, routing, and scaling. You benefit from EC2 pricing (Savings Plans, Reserved Instances) and multi-concurrency support.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 EC2 instances while AWS handles lifecycle management, patching, routing, and scaling. You benefit from EC2 pricing (Savings Plans, Reserved Instances) and multi-concurrency support.
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.


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 CloudWatch Logs, Amazon ECR (`ecr.api` and `ecr.dkr`), and Amazon EC2, plus an S3 gateway endpoint for pulling the runtime image layers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 CloudWatch Logs, Amazon ECR (`ecr.api` and `ecr.dkr`), and Amazon EC2, plus an S3 gateway endpoint for pulling the runtime image layers.
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.


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 CloudWatch Logs, Amazon ECR (`ecr.api` and `ecr.dkr`), and Amazon EC2, plus an S3 gateway endpoint for pulling the runtime image layers.

2. **Capacity Provider Operator IAM Role**: An IAM role with the `AWSLambdaManagedEC2ResourceOperator` managed policy that Lambda uses to provision and manage EC2 instances.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. **Capacity Provider Operator IAM Role**: An IAM role with the `AWSLambdaManagedEC2ResourceOperator` managed policy that Lambda uses to provision and manage EC2 instances.
2. **Capacity Provider Operator IAM Role**: An Amazon IAM role with the `AWSLambdaManagedEC2ResourceOperator` managed policy that Lambda uses to provision and manage EC2 instances.

Apply maintainer-requested wording and instruction fixes for the
lambda-managed-instances-python-sam pattern:

- Retitle to 'AWS Lambda Managed Instances with AWS SAM (Python)'
- Use full service names (Amazon EC2, Amazon CloudWatch Logs,
  Amazon S3, Amazon IAM) in the intro and How it works section
- Add --capabilities CAPABILITY_NAMED_IAM to sam deploy (the template
  creates a named IAM role for the capacity provider operator)
- Replace the placeholder timestamp with a concrete sample response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants