ChatGPT解决这个技术问题 Extra ChatGPT

Is it possible to rename an AWS Lambda function?

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

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


C
Chris Franklin

The closest you can get to renaming the lambda function is using an alias, which is a way to name a specific version of a lambda. 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 lambda function check out the documentation here.


you'd think Amazon would just do what you said under the hood and allow me to have my damn rename function :-)
D
Dharman

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!


L
Leo Hsu

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)

e
elliottregan

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


Do you know how to do this via the interface?
I see that it export/import only code. For import code: Function code > Actions > Upload a .zip file / Upload a file from Amazon S3.
if you only import the code, the remaining configuration will be lost, be careful