From d895f0f7d02d728099ad03eb4747c0d8b7e0d78c Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Sat, 29 Jan 2022 17:11:14 -0800 Subject: [PATCH] Lambda - Wait before updating (#857) Lambda - Wait before updating SUMMARY Updated lambda module to wait for State = Active & LastUpdateStatus = Successful based on https://aws.amazon.com/blogs/compute/coming-soon-expansion-of-aws-lambda-states-to-all-functions/ Fixes #830 ISSUE TYPE Bugfix Pull Request COMPONENT NAME module: lambda ADDITIONAL INFORMATION Reviewed-by: Markus Bergholz Reviewed-by: Alina Buzachis This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/ab4bda24245cf103212e0a64bff3cb5b985eb9da --- plugins/modules/lambda.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/plugins/modules/lambda.py b/plugins/modules/lambda.py index 1605d6497db..923b1646c3d 100644 --- a/plugins/modules/lambda.py +++ b/plugins/modules/lambda.py @@ -216,7 +216,7 @@ import re try: - from botocore.exceptions import ClientError, BotoCoreError + from botocore.exceptions import ClientError, BotoCoreError, WaiterError except ImportError: pass # protected by AnsibleAWSModule @@ -320,6 +320,18 @@ def set_tag(client, module, tags, function): return changed +def wait_for_lambda(client, module, name): + try: + client_active_waiter = client.get_waiter('function_active') + client_updated_waiter = client.get_waiter('function_updated') + client_active_waiter.wait(FunctionName=name) + client_updated_waiter.wait(FunctionName=name) + except WaiterError as e: + module.fail_json_aws(e, msg='Timeout while waiting on lambda to finish updating') + except (ClientError, BotoCoreError) as e: + module.fail_json_aws(e, msg='Failed while waiting on lambda to finish updating') + + def main(): argument_spec = dict( name=dict(required=True), @@ -453,6 +465,9 @@ def main(): # Upload new configuration if configuration has changed if len(func_kwargs) > 1: + if not check_mode: + wait_for_lambda(client, module, name) + try: if not check_mode: response = client.update_function_configuration(aws_retry=True, **func_kwargs) @@ -494,6 +509,9 @@ def main(): # Upload new code if needed (e.g. code checksum has changed) if len(code_kwargs) > 2: + if not check_mode: + wait_for_lambda(client, module, name) + try: if not check_mode: response = client.update_function_code(aws_retry=True, **code_kwargs)