Is it possible to rename an AWS Lambda function?
Asked Answered
A

4

116

I have created some AWS Lambda functions for testing purposes (named as test_function something), then after testing I found those functions can be used in prod environment.

Is it possible to rename the AWS Lambda function? and how? Or should I create a new one and copy paste source code?

Abbasid answered 18/4, 2016 at 2:23 Comment(0)
L
128

The closest you can get to renaming the AWS Lambda function is using an alias, which is a way to name a specific version of an AWS Lambda function. The actual name of the function though, is set once you create it. If you want to rename it, just create a new function and copy the exact same code into it. It won't cost you any extra to do this (since you are only charged for execution time) so you lose nothing.

For a reference on how to name versions of the AWS Lambda function, check out the documentation here: Lambda function versions .

Legitimate answered 18/4, 2016 at 2:39 Comment(1)
you'd think Amazon would just do what you said under the hood and allow me to have my damn rename function :-)Queensland
I
7

You cannot rename the function, your only option is to follow the suggestions already provided here or create a new one and copypaste the code.

It's a good thing actually that you cannot rename it: if you were able to, it would cease to work because the policies attached to the function still point to the old name, unless you were to edit every single one of them manually, or made them generic (which is ill-advised).

However, as a best practice in terms of software development, I suggest you to always keep production and testing (staging) separate, effectively duplicating your environment.

This allows you to test stuff on a safe environment, where if you make a mistake you don't lose anything important, and when you confirm that your new features work, replicate them in production.

So in your case, you would have two lambdas, one called 'my-lambda-staging' and the other 'my-lambda-prod'. Use the ENV variables of lambdas to adapt to the current environment, so you don't need to refactor!

Intoxicating answered 31/3, 2020 at 15:11 Comment(2)
"it would cease to work because the policies attached to the function still point to the old name": This issue has a solution, don't use names as unique identifiers. It would be odd if Amazon would use the funtion name as ID, I'd expect functions to have an ID i.e. a GUID, but who knows.Nada
@Nada the identifier it uses is the ARN, but the arn is just a string that among other things includes the name of the function itself.Intoxicating
B
2

My solution is to export the function, create a new Lambda, then upload the .zip file to the new Lambda.

Broadcaster answered 20/2, 2020 at 21:16 Comment(3)
Do you know how to do this via the interface?Passbook
I see that it export/import only code. For import code: Function code > Actions > Upload a .zip file / Upload a file from Amazon S3.Stillhunt
if you only import the code, the remaining configuration will be lost, be carefulCarrero
A
2

My solution for lambda rename, basically use boto3 describe previous lambda info for configuration setting and download the previous lambda function code to create a new lambda, but the trigger won't be set so you need to add trigger back manually

from boto3.session import Session
from botocore.client import Config
from botocore.handlers import set_list_objects_encoding_type_url
import boto3
import pprint
import urllib3

pp = pprint.PrettyPrinter(indent=4)

session = Session(aws_access_key_id= {YOUR_ACCESS_KEY},
                  aws_secret_access_key= {YOUR_SECRET_KEY},
                  region_name= 'your_region')

PREV_FUNC_NAME = 'your_prev_function_name'
NEW_FUNC_NAME = 'your_new_function_name'


def prev_lambda_code(code_temp_path):
    '''
    download prev function code
    '''
    code_url = code_temp_path
    http = urllib3.PoolManager()
    response = http.request("GET", code_url)
    if not 200 <= response.status < 300:
        raise Exception(f'Failed to download function code: {response}')
    return response.data

def rename_lambda_function(PREV_FUNC_NAME , NEW_FUNC_NAME):
    '''
    Copy previous lambda function and rename it
    '''
    lambda_client = session.client('lambda')
    prev_func_info = lambda_client.get_function(FunctionName = PREV_FUNC_NAME)

    if 'VpcConfig' in prev_func_info['Configuration']:
        VpcConfig = {
            'SubnetIds' : prev_func_info['Configuration']['VpcConfig']['SubnetIds'],
            'SecurityGroupIds' : prev_func_info['Configuration']['VpcConfig']['SecurityGroupIds']
        }
    else:
        VpcConfig = {}

    if 'Environment' in prev_func_info['Configuration']:
        Environment = prev_func_info['Configuration']['Environment']
    else:
        Environment = {}

    response = client.create_function(
        FunctionName = NEW_FUNC_NAME,
        Runtime = prev_func_info['Configuration']['Runtime'],
        Role = prev_func_info['Configuration']['Role'],
        Handler = prev_func_info['Configuration']['Handler'],
        Code = {
            'ZipFile' : prev_lambda_code(prev_func_info['Code']['Location'])
        },
        Description = prev_func_info['Configuration']['Description'],
        Timeout = prev_func_info['Configuration']['Timeout'],
        MemorySize = prev_func_info['Configuration']['MemorySize'],
        VpcConfig = VpcConfig,
        Environment = Environment,
        PackageType = prev_func_info['Configuration']['PackageType'],
        TracingConfig = prev_func_info['Configuration']['TracingConfig'],
        Layers = [Layer['Arn'] for Layer in prev_func_info['Configuration']['Layers']],
    )
    pp.pprint(response)

rename_lambda_function(PREV_FUNC_NAME , NEW_FUNC_NAME)
Arelus answered 3/5, 2021 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.