I want to move an AWS Lambda function from one AWS account or AWS Region to another AWS account or Region.
Short description
To migrate a Lambda function to another AWS account or Region, use an AWS Serverless Application Model (AWS SAM) file.
For more information, see Tutorial: Deploy a Hello World application with AWS SAM.
Note: The Lambda console and the AWS Command Line Interface (AWS CLI) also support Lambda function migration.
Resolution
Download the Lambda function AWS SAM file and deployment package
To download the Lambda function AWS SAM file and its deployment package, complete the following steps:
- Open the Lambda console.
- In the navigation pane, choose Functions.
- Choose the name of the Lambda function that you want to migrate.
- Choose the Download dropdown list and then choose Download both to download the AWS SAM file and deployment package.
For more information, see Downloading your function code.
Upload the Lambda function's deployment package to an Amazon S3 bucket
Upload the deployment package to an Amazon Simple Storage Service (Amazon S3) bucket in the Region that you're migrating the function to.
Complete the following steps:
-
Open the Amazon S3 console.
Note: Make sure that you sign in from the AWS account that you want to migrate the Lambda function to.
-
In the Buckets list, choose the name of the S3 bucket that you want to upload your files to.
-
Choose Upload.
-
On the Upload page, choose Add files.
-
Choose the Lambda function deployment package that you downloaded. Then, choose Open.
-
Choose Upload.
-
Choose Files and folders. To open the deployment package, select the deployment package file name.
-
In the Object Overview section of the file, note the S3 URI value. This value is the S3 path location for the deployment package. Save the value for use in the next step.
Use the AWS SAM file and CloudFormation to deploy a function
Use the AWS SAM file and AWS CloudFormation to deploy and manage a new Lambda function in another AWS account or Region.
Complete the following steps:
-
Open the Lambda function AWS SAM file that you downloaded.
Example Lambda function AWS SAM file code
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
MyLambdaFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: lambda_function.lambda_handler
Runtime: python3.13
CodeUri: . #S3 bucket Link
Description: ''
MemorySize: 128
Timeout: 6
Role: 'arn:aws:iam::123456789012:role/service-role/FirstLambdaPolicy' #Replace
Environment:
Variables:
homelocation: .tmp
Tags:
sampleTag: test
Tracing: Active
-
After CodeUri, replace the dot (.) with the S3 path location of the Lambda deployment package that you retrieved in the preceding step.
Important: Make sure that the AWS Identity and Access Management (IAM) user that creates the CloudFormation stack has 's3:GetObject' permission for this S3 object.
-
Migrate a function to another AWS account:
For the Role: value, delete the existing IAM role ARN. Then, replace the value with the IAM role ARN in the second AWS account.
For functions with layers, add a permission in the layer for the second AWS account that allows the layer to use the first account's layer. Or, replace the Lambda layer ARN in the AWS SAM file with the Lambda layer ARN in the second AWS account.
-
Migrate a function to another Region:
For the Role: value, use the existing IAM role ARN.
For functions with layers, replace the Lambda layer ARN with the Lambda layer ARN in the second AWS Region.
Important: For functions in a virtual private cloud (VPC), replace the values for SecurityGroupdIds and SubnetIds with the resources in the second AWS account or Region.
-
For functions with event sources, the event source configuration isn't exported as part of the AWS SAM file that's downloaded from Lambda function. To match the original event source with the resource values from your second account or Region, set the syntax for the Events property. Then, add the events to the AWS SAM file in your second AWS account or Region.
-
Use the edited AWS SAM file to create a AWS CloudFormation stack in the second AWS account or Region to migrate the function to.
Note: After you create the CloudFormation stack, the migrated Lambda function appears in your Lambda console.