Repository: ansible/ansible

Issue:
Lambda Event: Function ARN is limited to 64 characters in length.
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
lambda_event module

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->

N/A

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

N/A

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

N/A

##### SUMMARY
<!--- Explain the problem briefly -->
The module has `lambda_function_arn` parameter representing a name or ARN of the lambda function.

If this parameter is the name of function, it must be 64 characters or less.
On the other hand, if this parameter is the ARN of the lambda function, it must be 140 characters or less.
[cf. AWS API Reference](http://docs.aws.amazon.com/ja_jp/lambda/latest/dg/API_CreateFunction.html#SSS-CreateFunction-request-FunctionName)

However, in either case the validation check fails if it is 64 characters or less.
The corresponding source code is as follows.
https://github.com/ansible/ansible/blob/eb1214baad0cbe2c4b7304caebb9ae1c7dc0d8db/lib/ansible/modules/cloud/amazon/lambda_event.py#L233

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

```
lambda_event:
  state: "{{ state | default('present') }}"
  event_source: stream
  function_name: arn:aws:lambda:ap-northeast-1:123456789012:function:LAMBDA-FUNCTION-NAME
  alias: Dev
  source_params:
    source_arn: arn:aws:kinesis:ap-northeast-1:123456789012:stream/STREAM-NAME
    enabled: yes
    batch_size: 100
    starting_position: TRIM_HORIZON
```

<!--- Paste example playbooks or commands between quotes below -->
```yaml

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Play should run without errors.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
fatal: [127.0.0.1]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "alias": "Dev", 
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "ec2_url": null, 
            "event_source": "stream", 
            "lambda_function_arn": "arn:aws:lambda:ap-northeast-1:123456789012:function:LAMBDA-FUNCTION-NAME", 
            "profile": null, 
            "region": null, 
            "security_token": null, 
            "source_params": {
                "batch_size": 100, 
                "enabled": true, 
                "source_arn": "arn:aws:kinesis:ap-northeast-1:123456789012:stream/STREAM-NAME", 
                "starting_position": "TRIM_HORIZON"
            }, 
            "state": "present", 
            "validate_certs": true, 
            "version": 0
        }
    }, 
    "msg": "Function name \"arn:aws:lambda:ap-northeast-1:123456789012:function:LAMBDA-FUNCTION-NAME\" exceeds 64 character limit"
}
```

Lambda Event: Function ARN is limited to 64 characters in length.
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
lambda_event module

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->

N/A

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

N/A

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

N/A

##### SUMMARY
<!--- Explain the problem briefly -->
The module has `lambda_function_arn` parameter representing a name or ARN of the lambda function.

If this parameter is the name of function, it must be 64 characters or less.
On the other hand, if this parameter is the ARN of the lambda function, it must be 140 characters or less.
[cf. AWS API Reference](http://docs.aws.amazon.com/ja_jp/lambda/latest/dg/API_CreateFunction.html#SSS-CreateFunction-request-FunctionName)

However, in either case the validation check fails if it is 64 characters or less.
The corresponding source code is as follows.
https://github.com/ansible/ansible/blob/eb1214baad0cbe2c4b7304caebb9ae1c7dc0d8db/lib/ansible/modules/cloud/amazon/lambda_event.py#L233

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

```
lambda_event:
  state: "{{ state | default('present') }}"
  event_source: stream
  function_name: arn:aws:lambda:ap-northeast-1:123456789012:function:LAMBDA-FUNCTION-NAME
  alias: Dev
  source_params:
    source_arn: arn:aws:kinesis:ap-northeast-1:123456789012:stream/STREAM-NAME
    enabled: yes
    batch_size: 100
    starting_position: TRIM_HORIZON
```

<!--- Paste example playbooks or commands between quotes below -->
```yaml

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Play should run without errors.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
fatal: [127.0.0.1]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "alias": "Dev", 
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "ec2_url": null, 
            "event_source": "stream", 
            "lambda_function_arn": "arn:aws:lambda:ap-northeast-1:123456789012:function:LAMBDA-FUNCTION-NAME", 
            "profile": null, 
            "region": null, 
            "security_token": null, 
            "source_params": {
                "batch_size": 100, 
                "enabled": true, 
                "source_arn": "arn:aws:kinesis:ap-northeast-1:123456789012:stream/STREAM-NAME", 
                "starting_position": "TRIM_HORIZON"
            }, 
            "state": "present", 
            "validate_certs": true, 
            "version": 0
        }
    }, 
    "msg": "Function name \"arn:aws:lambda:ap-northeast-1:123456789012:function:LAMBDA-FUNCTION-NAME\" exceeds 64 character limit"
}
```



Discussion:
cc @ryansb @pjodouin
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @ryansb @pjodouin
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/lambda_event.py b/lib/ansible/modules/cloud/amazon/lambda_event.py
--- a/lib/ansible/modules/cloud/amazon/lambda_event.py
+++ b/lib/ansible/modules/cloud/amazon/lambda_event.py
@@ -229,9 +229,12 @@ def validate_params(module, aws):
         module.fail_json(
             msg='Function name {0} is invalid. Names must contain only alphanumeric characters and hyphens.'.format(function_name)
         )
-    if len(function_name) > 64:
+    if len(function_name) > 64 and not function_name.startswith('arn:aws:lambda:'):
         module.fail_json(msg='Function name "{0}" exceeds 64 character limit'.format(function_name))
 
+    elif len(function_name) > 140 and function_name.startswith('arn:aws:lambda:'):
+        module.fail_json(msg='ARN "{0}" exceeds 140 character limit'.format(function_name))
+
     # check if 'function_name' needs to be expanded in full ARN format
     if not module.params['lambda_function_arn'].startswith('arn:aws:lambda:'):
         function_name = module.params['lambda_function_arn']


Test patch: