Skip to content

Commit 2f8aa9a

Browse files
author
Scot Kronenfeld
committed
Add the ability to set tags on a function
1 parent 365f1e9 commit 2f8aa9a

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

aws_lambda/aws_lambda.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ def deploy(
109109
local_package=local_package,
110110
)
111111

112-
if function_exists(cfg):
113-
update_function(cfg, path_to_zip_file)
112+
existing_config = get_function_config(cfg)
113+
if existing_config:
114+
update_function(cfg, path_to_zip_file, existing_config)
114115
else:
115116
create_function(cfg, path_to_zip_file)
116117

@@ -143,8 +144,10 @@ def deploy_s3(
143144

144145
use_s3 = True
145146
s3_file = upload_s3(cfg, path_to_zip_file, use_s3)
146-
if function_exists(cfg):
147-
update_function(cfg, path_to_zip_file, use_s3=use_s3, s3_file=s3_file)
147+
existing_config = get_function_config(cfg)
148+
if existing_config:
149+
update_function(cfg, path_to_zip_file, existing_config, use_s3=use_s3,
150+
s3_file=s3_file)
148151
else:
149152
create_function(cfg, path_to_zip_file, use_s3=use_s3, s3_file=s3_file)
150153

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

544+
if 'tags' in cfg:
545+
kwargs.update(
546+
Tags={
547+
key: str(value)
548+
for key, value in cfg.get('tags').items()
549+
}
550+
)
551+
541552
if 'environment_variables' in cfg:
542553
kwargs.update(
543554
Environment={
@@ -552,7 +563,9 @@ def create_function(cfg, path_to_zip_file, use_s3=False, s3_file=None):
552563
client.create_function(**kwargs)
553564

554565

555-
def update_function(cfg, path_to_zip_file, use_s3=False, s3_file=None):
566+
def update_function(
567+
cfg, path_to_zip_file, existing_cfg, use_s3=False, s3_file=None
568+
):
556569
"""Updates the code of an existing Lambda function"""
557570

558571
print('Updating your Lambda function')
@@ -620,7 +633,17 @@ def update_function(cfg, path_to_zip_file, use_s3=False, s3_file=None):
620633
},
621634
)
622635

623-
client.update_function_configuration(**kwargs)
636+
ret = client.update_function_configuration(**kwargs)
637+
638+
if 'tags' in cfg:
639+
tags = {
640+
key: str(value)
641+
for key, value in cfg.get('tags').items()
642+
}
643+
if tags != existing_cfg['Tags']:
644+
client.untag_resource(Resource=ret['FunctionArn'],
645+
TagKeys=existing_cfg['Tags'].keys())
646+
client.tag_resource(Resource=ret['FunctionArn'], Tags=tags)
624647

625648

626649
def upload_s3(cfg, path_to_zip_file, *use_s3):
@@ -663,8 +686,8 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
663686
return filename
664687

665688

666-
def function_exists(cfg):
667-
"""Check whether a function exists or not"""
689+
def get_function_config(cfg):
690+
"""Check whether a function exists or not and return its config"""
668691

669692
function_name = cfg.get('function_name')
670693
profile_name = cfg.get('profile')
@@ -681,6 +704,7 @@ def function_exists(cfg):
681704
if 'Function not found' in str(e):
682705
return False
683706

707+
684708
def read_cfg(path_to_config_file, profile_name):
685709
cfg = read(path_to_config_file, loader=yaml.load)
686710
if profile_name is not None:

0 commit comments

Comments
 (0)