Question: How do I migrate a Lambda function to another AWS account or Region using the AWS CLI?

I need to move an AWS Lambda function from one AWS account (or AWS Region) to another. How can I do that using the AWS Command Line Interface (AWS CLI)?Short descriptionTo migrate a Lambda function to a second AWS account or Region using the AWS CLI, do the following:1.    Run the GetFunction command to download the Lambda function deployment package.2.    Configure the AWS CLI for the second AWS account or Region that you want to move the function to.Note: You can configure a new AWS CLI profile for your second AWS account or Region as well.3.    Run the CreateFunction command to create a new function in the second AWS account or Region.Note: You can also migrate a Lambda function using the Lambda console or an AWS Serverless Application Model (AWS SAM).

ResolutionNote: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.Run the GetFunction command to download the Lambda function deployment package1.    Run the following GetFunction command:aws lambda get-function --function-name my-functionImportant: Replace my-function with the name of the function you want to migrate.2.    In the command response, open the URL link after "Location":. The link will appear in a code block similar to the following one:"Code": {        "RepositoryType": "S3",        "Location": "https://awslambda-us-west-2-tasks.s3.us-west-2.amazonaws.com/snapshots/123456789012/my-function..."    },Note: Opening the link following "Location": will download the deployment package.Configure the AWS CLI for the second AWS account or Region that you want to move the function to1.    Run the following Configure command:aws configure --profile profilenameImportant: Change profilename to a recognizable name for your second AWS account or Region.2.    Enter the following input values to pass the AWS Identity and Access Management (IAM) user credentials of the second AWS account and the Region:For AWS Access Key ID [None]: enter the access key of an IAM user in the second AWS account. Or, if you’re migrating the function to another Region, enter the access key of an IAM user in your first AWS account.For AWS Secret Access Key [None]: enter the secret access key of the same IAM user.For Default region name [None]: enter the AWS Region you’re migrating your function to.For more information, see Configuring the AWS CLI.Run the CreateFunction command to create a new function in the second AWS account or Region.Note: You need the Lambda function deployment package and execution role to run the CreateFunction command.1.    Run the following CreateFunction command using the AWS CLI profile that you just configured:aws lambda create-function \    --function-name my-function \    --runtime nodejs10.x \        --zip-file fileb://my-function.zip \       --handler my-function.handler \    --role arn:aws:iam::123456789012:role/service-role/MyTestFunction-role-tges6bf4 \    --profile profilenameImportant: Before running the command, replace the following values with the information from the function that you want to migrate:For function-name, enter the name of your function.For runtime, enter the runtime of your function.For zip-file, enter the file path of your function’s deployment package.For handler, enter the handler name of your function.For role, enter the Lambda execution role ARN that’s in the AWS account that you want to migrate your function to.For profile, enter the AWS CLI profile name you created when you ran the Configure command.Note: If you’re migrating a function to another AWS Region, but keeping it in the same AWS account, you can keep using the same execution role.2.    Run the following list-functions command to confirm that the migration worked:aws lambda list-functions \    --profile profilenameImportant: Replace profilename with the AWS CLI profile name that you created when you ran the Configure command.