Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #1115/f2ad6375 backport][stable-3] execute_lambda - fix check mode and update RETURN docs #1136

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- execute_lambda - fix check mode and update RETURN documentation (https://github.com/ansible-collections/community.aws/pull/1115).
47 changes: 27 additions & 20 deletions plugins/modules/execute_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,25 @@
'''

RETURN = '''
output:
description: Function output if wait=true and the function returns a value
result:
description: Resulting data structure from a successful task execution.
returned: success
type: dict
sample: "{ 'output': 'something' }"
logs:
description: The last 4KB of the function logs. Only provided if I(tail_log) is true
type: str
returned: if I(tail_log) == true
status:
description: C(StatusCode) of API call exit (200 for synchronous invokes, 202 for async)
type: int
sample: 200
returned: always
contains:
output:
description: Function output if wait=true and the function returns a value
returned: success
type: dict
sample: "{ 'output': 'something' }"
logs:
description: The last 4KB of the function logs. Only provided if I(tail_log) is C(true)
type: str
returned: if I(tail_log) == true
status:
description: C(StatusCode) of API call exit (200 for synchronous invokes, 202 for async)
type: int
sample: 200
returned: always
'''

import base64
Expand All @@ -134,6 +139,7 @@

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry


def main():
Expand All @@ -151,7 +157,10 @@ def main():
supports_check_mode=True,
mutually_exclusive=[
['name', 'function_arn'],
]
],
required_one_of=[
('name', 'function_arn')
],
)

name = module.params.get('name')
Expand All @@ -162,11 +171,8 @@ def main():
version_qualifier = module.params.get('version_qualifier')
payload = module.params.get('payload')

if not (name or function_arn):
module.fail_json(msg="Must provide either a function_arn or a name to invoke.")

try:
client = module.client('lambda')
client = module.client('lambda', retry_decorator=AWSRetry.jittered_backoff())
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Failed to connect to AWS')

Expand Down Expand Up @@ -202,11 +208,12 @@ def main():
elif name:
invoke_params['FunctionName'] = name

if not module.check_mode:
wait_for_lambda(client, module, name)
if module.check_mode:
module.exit_json(changed=True)

try:
response = client.invoke(**invoke_params)
wait_for_lambda(client, module, name)
response = client.invoke(**invoke_params, aws_retry=True)
except is_boto3_error_code('ResourceNotFoundException') as nfe:
module.fail_json_aws(nfe, msg="Could not find Lambda to execute. Make sure "
"the ARN is correct and your profile has "
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/targets/lambda/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
- '"handler" in result.msg'
- '"role" in result.msg'

- name: test execute lambda with no function arn or name
execute_lambda:
register: result
ignore_errors: true
- name: assert failure when called with no parameters
assert:
that:
- result.failed
- "result.msg == 'one of the following is required: name, function_arn'"

- name: test state=present with security group but no vpc
lambda:
name: '{{ lambda_function_name }}'
Expand Down Expand Up @@ -122,6 +132,19 @@
- result.configuration.tracing_config.mode == "PassThrough"

# Test basic operation of Uploaded lambda
- name: test lambda works (check mode)
execute_lambda:
name: '{{lambda_function_name}}'
payload:
name: Mr Ansible Tests
register: result
check_mode: yes
- name: assert check mode works correctly
assert:
that:
- result.changed
- "'result' not in result"

- name: test lambda works
execute_lambda:
name: '{{lambda_function_name}}'
Expand Down