Created
January 1, 2018 23:23
-
-
Save votaguz/c55a7ef5f6540e0fc233fe66e877516f to your computer and use it in GitHub Desktop.
Script to clone lambda functions in AWS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import random | |
import string | |
import boto3 | |
LAMBDA_CONFIG_TEMPLATE = ["TracingConfig", "FunctionName", "VpcConfig", "MemorySize", "Environment", "Handler", "Role", "Timeout", "Runtime", "Description"] | |
def clone_lambda(aws_client, name, code_zipfile_path): | |
""" | |
This function clones a lambda in your AWS stack | |
""" | |
absolute_file_path = os.path.abspath(code_zipfile_path) | |
random_suffix = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(12)]) | |
with open(absolute_file_path) as zipfile: | |
new_lambda_config = {} | |
print("Getting configuration of {}".format(name)) | |
current_lambda = aws_client.get_function(FunctionName=name) | |
current_lambda_config = current_lambda.get('Configuration') | |
print("Creating configuration for new lambda") | |
for config in current_lambda_config.keys(): | |
if config in LAMBDA_CONFIG_TEMPLATE: | |
new_lambda_config[config] = current_lambda_config[config] | |
new_lambda_config.get('VpcConfig').pop('VpcId') | |
new_lambda_config['Code'] = {} | |
new_lambda_config['Code']['ZipFile'] = b'{}'.format(zipfile.read()) | |
new_lambda_function_name = "{}-{}".format(current_lambda_config['FunctionName'], random_suffix) | |
new_lambda_config['FunctionName'] = new_lambda_function_name | |
print("Creating new lambda function") | |
aws_client.create_function(**new_lambda_config) | |
print("{} Created".format(new_lambda_function_name)) | |
if __name__ == "__main__": | |
argument_parser = argparse.ArgumentParser(description='Clone a lambda function from AWS') | |
argument_parser.add_argument('name', type=str, help='Name of function to be cloned') | |
argument_parser.add_argument('code_zipfile_path', type=str, help='Code zipfile path') | |
args = argument_parser.parse_args() | |
aws_client = boto3.client('lambda') | |
clone_lambda(aws_client, args.name, args.code_zipfile_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
things can go wrong here too:
new_lambda_config['Code']['ZipFile'] = b'{}'.format(zipfile.read())