Deploy Lambda durable functions with Infrastructure as Code - AWS Lambda

Deploy Lambda durable functions with Infrastructure as Code

You can deploy Lambda durable functions using Infrastructure as Code (IaC) tools like AWS CloudFormation, AWS CDK, AWS Serverless Application Model, or Terraform. These tools let you define your function, execution role, and permissions in code, making deployments repeatable and version-controlled.

All three tools require you to:

  • Enable durable execution on the function

  • Grant checkpoint permissions to the execution role

  • Publish a version or create an alias (durable functions require qualified ARNs)

AWS CloudFormation

Use CloudFormation to define your durable function in a template. The following example creates a durable function with the required permissions.

AWSTemplateFormatVersion: '2010-09-09' Description: Lambda durable function example Resources: DurableFunctionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicDurableExecutionRolePolicy DurableFunction: Type: AWS::Lambda::Function Properties: FunctionName: myDurableFunction Runtime: nodejs22.x Handler: index.handler Role: !GetAtt DurableFunctionRole.Arn Code: ZipFile: | // Your durable function code here export const handler = async (event, context) => { return { statusCode: 200 }; }; DurableConfig: ExecutionTimeout: 3600 RetentionPeriodInDays: 7 DurableFunctionVersion: Type: AWS::Lambda::Version Properties: FunctionName: !Ref DurableFunction Description: Initial version DurableFunctionAlias: Type: AWS::Lambda::Alias Properties: FunctionName: !Ref DurableFunction FunctionVersion: !GetAtt DurableFunctionVersion.Version Name: prod Outputs: FunctionArn: Description: Durable function ARN Value: !GetAtt DurableFunction.Arn AliasArn: Description: Function alias ARN (use this for invocations) Value: !Ref DurableFunctionAlias

To deploy the template

aws cloudformation deploy \ --template-file template.yaml \ --stack-name my-durable-function-stack \ --capabilities CAPABILITY_IAM

AWS CDK

AWS CDK lets you define infrastructure using programming languages. The following examples show how to create a durable function using TypeScript and Python.

TypeScript
import * as cdk from 'aws-cdk-lib'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as iam from 'aws-cdk-lib/aws-iam'; import { Construct } from 'constructs'; export class DurableFunctionStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Create the durable function const durableFunction = new lambda.Function(this, 'DurableFunction', { runtime: lambda.Runtime.NODEJS_22_X, handler: 'index.handler', code: lambda.Code.fromAsset('lambda'), functionName: 'myDurableFunction', durableConfig: { executionTimeout: Duration.hours(1), retentionPeriod: Duration.days(30) }, }); // Create version and alias const version = durableFunction.currentVersion; const alias = new lambda.Alias(this, 'ProdAlias', { aliasName: 'prod', version: version, }); // Output the alias ARN new cdk.CfnOutput(this, 'FunctionAliasArn', { value: alias.functionArn, description: 'Use this ARN to invoke the durable function', }); } }
Python
from aws_cdk import ( Stack, aws_lambda as lambda_, aws_iam as iam, CfnOutput, ) from constructs import Construct class DurableFunctionStack(Stack): def __init__(self, scope: Construct, id: str, **kwargs): super().__init__(scope, id, **kwargs) # Create the durable function durable_function = lambda_.Function( self, 'DurableFunction', runtime=lambda_.Runtime.NODEJS_22_X, handler='index.handler', code=lambda_.Code.from_asset('lambda'), function_name='myDurableFunction', durable_execution={execution_timeout: Duration.hours(1), retention_period: Duration.days(30)} ) # Add durable execution managed policy for checkpoint permissions durable_function.role.add_managed_policy( iam.ManagedPolicy.from_aws_managed_policy_name('service-role/AWSLambdaBasicDurableExecutionRolePolicy') ) # Create version and alias version = durable_function.current_version alias = lambda_.Alias( self, 'ProdAlias', alias_name='prod', version=version ) # Output the alias ARN CfnOutput( self, 'FunctionAliasArn', value=alias.function_arn, description='Use this ARN to invoke the durable function' )

To deploy the CDK stack

cdk deploy

AWS Serverless Application Model

AWS SAM simplifies CloudFormation templates for serverless applications. The following template creates a durable function with AWS SAM.

AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Lambda durable function with SAM Resources: DurableFunction: Type: AWS::Serverless::Function Properties: FunctionName: myDurableFunction Runtime: nodejs22.x Handler: index.handler CodeUri: ./src DurableConfig: ExecutionTimeout: 3600 RetentionPeriodInDays: 7 Policies: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicDurableExecutionRolePolicy AutoPublishAlias: prod Outputs: FunctionArn: Description: Durable function ARN Value: !GetAtt DurableFunction.Arn AliasArn: Description: Function alias ARN (use this for invocations) Value: !Ref DurableFunction.Alias

To deploy the SAM template

sam build sam deploy --guided

Terraform

Terraform is a popular open-source IaC tool that supports AWS resources. The following example creates a durable function with Terraform using the AWS provider version 6.25.0 or later.

terraform { required_version = ">= 1.0" required_providers { aws = { source = "hashicorp/aws" version = ">= 6.25.0" } } } provider "aws" { region = "us-east-2" } # IAM Role for Lambda Function resource "aws_iam_role" "lambda_role" { name = "durable-function-role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [{ Action = "sts:AssumeRole" Effect = "Allow" Principal = { Service = "lambda.amazonaws.com" } }] }) } # Attach durable execution policy for checkpoint operations resource "aws_iam_role_policy_attachment" "lambda_durable" { policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicDurableExecutionRolePolicy" role = aws_iam_role.lambda_role.name } # Lambda Function with Durable Execution enabled resource "aws_lambda_function" "durable_function" { filename = "function.zip" function_name = "myDurableFunction" role = aws_iam_role.lambda_role.arn handler = "index.handler" runtime = "nodejs22.x" timeout = 30 memory_size = 512 durable_config { execution_timeout = 900 retention_period = 7 } } # Publish a version resource "aws_lambda_alias" "prod" { name = "prod" function_name = aws_lambda_function.durable_function.function_name function_version = aws_lambda_function.durable_function.version } output "function_arn" { description = "ARN of the Lambda function" value = aws_lambda_function.durable_function.arn } output "alias_arn" { description = "ARN of the function alias (use this for invocations)" value = aws_lambda_alias.prod.arn }

To deploy with Terraform

terraform init terraform plan terraform apply
Note

Terraform support for Lambda durable functions requires AWS provider version 6.25.0 or later. Update your provider version if you're using an older version.

Common configuration patterns

Regardless of which IaC tool you use, follow these patterns for durable functions:

Enable durable execution

Set the DurableConfig property on your function to enable durable execution. This property is only available when creating the function. You cannot enable durable execution on existing functions.

Grant checkpoint permissions

Attach the AWSLambdaBasicDurableExecutionRolePolicy managed policy to the execution role. This policy includes the required lambda:CheckpointDurableExecutions and lambda:GetDurableExecutionState permissions.

Use qualified ARNs

Create a version or alias for your function. Durable functions require qualified ARNs (with version or alias) for invocation. Use AutoPublishAlias in AWS SAM or create explicit versions in CloudFormation, AWS CDK, and Terraform.

Package dependencies

Include the durable execution SDK in your deployment package. For Node.js, install @aws/durable-execution-sdk-js. For Python, install aws-durable-execution-sdk-python.

Next steps

After deploying your durable function:

  • Test your function using the qualified ARN (version or alias)

  • Monitor execution progress in the Lambda console under the Durable executions tab

  • View checkpoint operations in AWS CloudTrail data events

  • Review CloudWatch Logs for function output and replay behavior

For more information about deploying Lambda functions with IaC tools, see: