Skip to content

Commit 4999de4

Browse files
authored
Merge pull request nficano#99 from scotwk/feature-add-tagging
Add the ability to set tags on a function
2 parents 365f1e9 + b1b820a commit 4999de4

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

aws_lambda/aws_lambda.py

Lines changed: 33 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,18 @@ 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.get('Tags'):
644+
if existing_cfg.get('Tags'):
645+
client.untag_resource(Resource=ret['FunctionArn'],
646+
TagKeys=list(existing_cfg['Tags'].keys()))
647+
client.tag_resource(Resource=ret['FunctionArn'], Tags=tags)
624648

625649

626650
def upload_s3(cfg, path_to_zip_file, *use_s3):
@@ -663,8 +687,8 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
663687
return filename
664688

665689

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

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

708+
684709
def read_cfg(path_to_config_file, profile_name):
685710
cfg = read(path_to_config_file, loader=yaml.load)
686711
if profile_name is not None:

aws_lambda/project_templates/config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ environment_variables:
2626
env_1: foo
2727
env_2: baz
2828

29+
# If `tags` is uncommented then tags will be set at creation or update
30+
# time. During an update all other tags will be removed except the tags
31+
# listed here.
32+
#tags:
33+
# tag_1: foo
34+
# tag_2: bar
35+
2936
# Build options
3037
build:
3138
source_directories: lib # a comma delimited list of directories in your project root that contains source to package.

0 commit comments

Comments
 (0)