Run functions on Lambda Managed Instances with AWS SAM
This topic describes how AWS SAM supports Lambda Managed Instances, how function versions work for these functions, and how to plan for capacity when you deploy them.
What is Lambda Managed Instances?
Lambda Managed Instances is a Lambda compute type. With Lambda Managed Instances, you can run your functions on current-generation Amazon EC2 instances in your account, including instances with graphics processing units (GPUs). You choose instance types based on your performance and cost requirements. You also benefit from Amazon EC2 pricing options such as Amazon EC2 Savings Plans and Reserved Instances.
You define a capacity provider that specifies the instance types and subnets to use. Lambda launches the instances, manages their lifecycle, and scales execution environments on them. Each execution environment can process several invocations at the same time.
To learn more, see Lambda Managed Instances in the AWS Lambda Developer Guide.
Topics
Lambda Managed Instances support in AWS SAM
AWS SAM provides the AWS::Serverless::CapacityProvider resource for Lambda Managed Instances. This resource defines the capacity provider, which includes the instance types, subnets, and scaling settings for the instances that run your functions.
The following AWS::Serverless::Function properties configure a function
that runs on a capacity provider:
Property |
Description |
|---|---|
Attaches the function to a capacity provider. For when you can set it, see CapacityProviderConfig. |
|
Sets the minimum and maximum number of execution environments. |
|
Sets the accelerator memory for a GPU function. |
|
Controls whether Lambda publishes updates to the
|
AWS SAM validates these properties when it transforms the template.
FunctionScalingConfig and AcceleratorConfig require
CapacityProviderConfig. A function that sets
CapacityProviderConfig can't also set ProvisionedConcurrencyConfig or VpcConfig. Set the network configuration in the capacity
provider's VpcConfig instead.
How AWS SAM deployments publish the $LATEST.PUBLISHED version
An unqualified invocation of a Lambda Managed Instances function runs the
$LATEST.PUBLISHED version, and only published versions can serve invocations.
So when CloudFormation creates a function that uses a capacity provider, Lambda creates the
$LATEST.PUBLISHED version regardless of the value of
PublishToLatestPublished, and you can invoke the function by name as soon as
stack creation completes.
PublishToLatestPublished applies to later updates of the function. Setting
it to false doesn't delete a $LATEST.PUBLISHED version that
already exists.
Use numbered versions and aliases
To publish numbered versions and point an alias at the latest one, use AutoPublishAlias.
AWS SAM sets the DeletionPolicy of the generated
AWS::Lambda::Version resource to Delete for a function that uses
a capacity provider, and to Retain for other functions. To use a different
policy, set VersionDeletionPolicy.
When a function that sets FunctionScalingConfig also has
CapacityProviderConfig and AutoPublishAlias, AWS SAM applies
FunctionScalingConfig to the generated AWS::Lambda::Version
and to the AWS::Lambda::Function.
The function-level setting applies to $LATEST.PUBLISHED. As a result, the
version that the alias points to and $LATEST.PUBLISHED each keep at least
MinExecutionEnvironments execution
environments running.
If all invocations go through the alias, set PublishToLatestPublished to
false so that later updates don't publish to
$LATEST.PUBLISHED. This setting doesn't remove the
$LATEST.PUBLISHED version created with the function.
To remove the $LATEST.PUBLISHED version:
-
Deploy the template with
PublishToLatestPublishedset tofalse. Otherwise, the next update publishes to$LATEST.PUBLISHEDagain. -
Run the following AWS CLI command to delete the
$LATEST.PUBLISHEDversion. Use single quotes so that your shell doesn't expand$LATEST. Replacemy-functionwith the name of your function:$aws lambda delete-function --function-namemy-function--qualifier '$LATEST.PUBLISHED'
After you remove it, invoke the function only through the alias or a version number,
because unqualified invocations use $LATEST.PUBLISHED. For more information,
see DeleteFunction in the AWS Lambda API Reference.
The following template defines a capacity provider and a function that publishes updates
only through the live alias. Every function in this template uses the capacity
provider, so the template sets PublishToLatestPublished in the Globals section of the AWS SAM template. If a template also has
functions that don't use a capacity provider, set PublishToLatestPublished on
each function that uses one instead. Each ... line marks where other properties
or sections can go:
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Parameters: SubnetIds: Type: List<AWS::EC2::Subnet::Id> SecurityGroupIds: Type: List<AWS::EC2::SecurityGroup::Id> ... Globals: Function: PublishToLatestPublished: false ... Resources: MyCapacityProvider: Type: AWS::Serverless::CapacityProvider Properties: VpcConfig: SubnetIds: !Ref SubnetIds SecurityGroupIds: !Ref SecurityGroupIds InstanceRequirements: AllowedTypes: -m7i.large-m7a.large... MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/ Handler: app.handler Runtime: python3.13 MemorySize: 2048 CapacityProviderConfig: Arn: !GetAtt MyCapacityProvider.Arn FunctionScalingConfig: MinExecutionEnvironments: 3 MaxExecutionEnvironments: 10 AutoPublishAlias: live ... ...
If you set CapacityProviderConfig in the Globals section, it
applies to every function in the template. No function in the template can then set
ProvisionedConcurrencyConfig or VpcConfig.
Plan for instance capacity during deployment
Stack creation waits until Lambda starts execution environments on the capacity provider's instances. Amazon EC2 capacity might not be available for the allowed instance types in the Availability Zones of the capacity provider's subnets. In that case, the function fails to stabilize and the stack operation fails. To reduce this risk, allow more than one instance type in the capacity provider's InstanceRequirements, and use subnets in more than one Availability Zone.
For GPU functions, Lambda falls back to another allowed instance type when the best-fit type has no capacity. Each allowed instance type must therefore have enough accelerator memory for the function's AcceleratorConfig. For more information, see Understanding accelerator memory in the AWS Lambda Developer Guide.
Learn more
To learn more about Lambda Managed Instances, see the following topics in the AWS Lambda Developer Guide: