Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions aws_lambda/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ def deploy(
local_package=local_package,
)

if function_exists(cfg):
update_function(cfg, path_to_zip_file)
existing_config = get_function_config(cfg)
if existing_config:
update_function(cfg, path_to_zip_file, existing_config)
else:
create_function(cfg, path_to_zip_file)

Expand Down Expand Up @@ -143,8 +144,10 @@ def deploy_s3(

use_s3 = True
s3_file = upload_s3(cfg, path_to_zip_file, use_s3)
if function_exists(cfg):
update_function(cfg, path_to_zip_file, use_s3=use_s3, s3_file=s3_file)
existing_config = get_function_config(cfg)
if existing_config:
update_function(cfg, path_to_zip_file, existing_config, use_s3=use_s3,
s3_file=s3_file)
else:
create_function(cfg, path_to_zip_file, use_s3=use_s3, s3_file=s3_file)

Expand Down Expand Up @@ -538,6 +541,14 @@ def create_function(cfg, path_to_zip_file, use_s3=False, s3_file=None):
'Publish': True,
}

if 'tags' in cfg:
kwargs.update(
Tags={
key: str(value)
for key, value in cfg.get('tags').items()
}
)

if 'environment_variables' in cfg:
kwargs.update(
Environment={
Expand All @@ -552,7 +563,9 @@ def create_function(cfg, path_to_zip_file, use_s3=False, s3_file=None):
client.create_function(**kwargs)


def update_function(cfg, path_to_zip_file, use_s3=False, s3_file=None):
def update_function(
cfg, path_to_zip_file, existing_cfg, use_s3=False, s3_file=None
):
"""Updates the code of an existing Lambda function"""

print('Updating your Lambda function')
Expand Down Expand Up @@ -620,7 +633,18 @@ def update_function(cfg, path_to_zip_file, use_s3=False, s3_file=None):
},
)

client.update_function_configuration(**kwargs)
ret = client.update_function_configuration(**kwargs)

if 'tags' in cfg:
tags = {
key: str(value)
for key, value in cfg.get('tags').items()
}
if tags != existing_cfg.get('Tags'):
if existing_cfg.get('Tags'):
client.untag_resource(Resource=ret['FunctionArn'],
TagKeys=list(existing_cfg['Tags'].keys()))
client.tag_resource(Resource=ret['FunctionArn'], Tags=tags)


def upload_s3(cfg, path_to_zip_file, *use_s3):
Expand Down Expand Up @@ -663,8 +687,8 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
return filename


def function_exists(cfg):
"""Check whether a function exists or not"""
def get_function_config(cfg):
"""Check whether a function exists or not and return its config"""

function_name = cfg.get('function_name')
profile_name = cfg.get('profile')
Expand All @@ -681,6 +705,7 @@ def function_exists(cfg):
if 'Function not found' in str(e):
return False


def read_cfg(path_to_config_file, profile_name):
cfg = read(path_to_config_file, loader=yaml.load)
if profile_name is not None:
Expand Down
7 changes: 7 additions & 0 deletions aws_lambda/project_templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ environment_variables:
env_1: foo
env_2: baz

# If `tags` is uncommented then tags will be set at creation or update
# time. During an update all other tags will be removed except the tags
# listed here.
#tags:
# tag_1: foo
# tag_2: bar

# Build options
build:
source_directories: lib # a comma delimited list of directories in your project root that contains source to package.