Last active
May 30, 2019 10:12
-
-
Save naamancampbell/64b8013d30adfd95a18e3585e25e3044 to your computer and use it in GitHub Desktop.
Copy AWS Lambda environment variables from source Lambda function to destination Lambda function
This file contains hidden or 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 boto3 | |
""" | |
Usage: copy_lambda_env_vars.py [-h] [--src-profile-name SRC_PROFILE] | |
[--dst-profile-name DST_PROFILE] | |
src_function dst_function | |
Utility to copy environment variables from source Lambda function to | |
destination Lambda function. | |
positional arguments: | |
src_function Source Lambda function name | |
dst_function Destination Lambda function name | |
optional arguments: | |
-h, --help show this help message and exit | |
--src-profile-name SRC_PROFILE | |
Source Lambda function AWS Profile name | |
--dst-profile-name DST_PROFILE | |
Destination Lambda function AWS Profile name | |
""" | |
def create_argparser(): | |
command_description = """ | |
Utility to copy environment variables from source Lambda function to | |
destination Lambda function. | |
""" | |
argparser = argparse.ArgumentParser(description=command_description) | |
argparser.add_argument( | |
'src_function', | |
help='Source Lambda function name') | |
argparser.add_argument( | |
'dst_function', | |
help='Destination Lambda function name') | |
argparser.add_argument( | |
'--src-profile-name', | |
dest='src_profile', | |
help='Source Lambda function AWS Profile name') | |
argparser.add_argument( | |
'--dst-profile-name', | |
dest='dst_profile', | |
help='Destination Lambda function AWS Profile name') | |
return argparser | |
def copy_env_vars(src_function, dst_function, src_profile, dst_profile): | |
if src_profile: | |
session = boto3.Session(profile_name=src_profile) | |
src_lambda_client = session.client('lambda') | |
else: | |
src_lambda_client = boto3.client('lambda') | |
if dst_profile: | |
session = boto3.Session(profile_name=dst_profile) | |
dst_lambda_client = session.client('lambda') | |
else: | |
dst_lambda_client = boto3.client('lambda') | |
src_config = src_lambda_client.get_function_configuration( | |
FunctionName=src_function) | |
dst_lambda_client.update_function_configuration( | |
FunctionName=dst_function, | |
Environment=src_config['Environment'] | |
) | |
def main(): | |
argparser = create_argparser() | |
args = argparser.parse_args() | |
copy_env_vars( | |
args.src_function, args.dst_function, | |
args.src_profile, args.dst_profile) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment