Move AWS Lambda function to another name, another region

shimo
2 min readJul 18, 2021

--

Once a Lambda function is created, you can not rename it or move it to another region. Like other resources in AWS, name and region of a Lambda function are in a unique ARN. You can move the Lambda function manually steps, but it’s rather burdensome.

In this post, I will show how to rename and move regions of a Lambda function using CLI commands and a shell script.

Figure.1 Diagram of Lambda rename, move regions

Steps

Figure.1 shows the idea for 3 steps of moving operations.

1. By get-function, the S3 location of Lambda zip file is obtained.

2. Get zip function using wget S3

3. Create another Lambda function

0. Create IAM Role to execute Lambda function

As shown in this document, the IAM role to execute the Lambda function is required.

You can create the IAM role with this one-liner. Here I use the role name lambda-ex as in the document.

$ aws iam create-role --role-name lambda-ex --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'

1. Get S3 location of Lambda function

Get information of the Lambda function with

$ aws lambda get-function --function-name <name> --region <region>.

Output like below shows Configuration and temporary S3 Location for zip download.

{
"Configuration": {
"FunctionName": "",
"FunctionArn": "",
"Runtime": "",
"Role": "arn:aws:iam::...:role/lambda-ex",
...
},
"Code": {
"RepositoryType": "S3",
"Location": "https://prod-04-2014-tasks.s3.us-east-1.amazonaws.com/snapshots/....................."
}
}

2. Get zip file of Lambda

Download the zip file from S3 using wget. Use--quiet not to show detailed results.

$ wget --quiet --output-document <zip_name> <S3LocationURL>

3. Create another Lambda function

By using $ aws lambda create-function and options, the new lambda function is created with a new name, a new region. (Of course, you can create in the same region.)

4. shell script

These CLI commands and parameters are connected to one .sh file like blow. Save as mv_lambda.sh for example. Set parameters names and regions, and execute this command. (Note that you can change settings for deleting the original Lambda function and the zip file of the function.)

$ bash mv_lambda.sh

Summary

I have shown how to rename and move regions of AWS Lambda functions, using CLI commands and a shell script.

--

--

shimo

Moved to https://dev.to/shimo_s3 | Freelance software engineer | Python, AWS (Community Builder) | Automation, Data analysis, Serverless