Skip to content

Commit

Permalink
Lambda - Wait before updating (#857) (#904)
Browse files Browse the repository at this point in the history
[PR #857/ab4bda24 backport][stable-2] Lambda - Wait before updating

This is a backport of PR #857 as merged into main (ab4bda2).
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
  • Loading branch information
patchback[bot] authored Jan 31, 2022
1 parent 776bbb3 commit 46cbd13
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelogs/fragments/857-lambda-wait-before.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- execute_lambda - Wait for Lambda function State = Active before executing (https://github.com/ansible-collections/community.aws/pull/857)
- lambda - Wait for Lambda function State = Active & LastUpdateStatus = Successful before updating (https://github.com/ansible-collections/community.aws/pull/857)
13 changes: 13 additions & 0 deletions plugins/modules/execute_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ def main():
elif name:
invoke_params['FunctionName'] = name

if not module.check_mode:
wait_for_lambda(client, module, name)

try:
response = client.invoke(**invoke_params)
except is_boto3_error_code('ResourceNotFoundException') as nfe:
Expand Down Expand Up @@ -255,5 +258,15 @@ def main():
module.exit_json(changed=True, result=results)


def wait_for_lambda(client, module, name):
try:
waiter = client.get_waiter('function_active')
waiter.wait(FunctionName=name)
except botocore.exceptions.WaiterError as e:
module.fail_json_aws(e, msg='Timeout while waiting on lambda to be Active')
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Failed while waiting on lambda to be Active')


if __name__ == '__main__':
main()
20 changes: 19 additions & 1 deletion plugins/modules/lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 46cbd13

Please sign in to comment.