# Buggy code
def determine_iam_role(name_or_arn, iam):
    if re.match(r"^arn:aws:iam::\d+:instance-profile/[\w+=/,.@-]+$", name_or_arn):
        return name_or_arn
    if iam is None:
        iam = module.client("iam")
    try:
        role = iam.get_instance_profile(InstanceProfileName=name_or_arn)
        return role["InstanceProfile"]["Arn"]
    except botocore.exceptions.ClientError as e:
        if e.response["Error"]["Code"] == "NoSuchEntity":
            module.fail_json_aws(
                e, msg="Could not find instance_role {0}".format(name_or_arn)
            )
        module.fail_json_aws(
            e,
            msg="An error occurred while searching for instance_role {0}. Please try supplying the full ARN.".format(
                name_or_arn
            ),
        )


# Traceback (TypeError)
Using module file /Library/Python/2.7/site-packages/ansible/modules/cloud/amazon/ec2_instance.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: vink
<127.0.0.1> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/vink/.ansible/tmp/ansible-tmp-1521107885.04-209498160937436 `" &amp;&amp; echo ansible-tmp-1521107885.04-209498160937436="` echo /Users/vink/.ansible/tmp/ansible-tmp-1521107885.04-209498160937436 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /Users/vink/.ansible/tmp/ansible-local-20586FoULbu/tmpYiVFpS TO /Users/vink/.ansible/tmp/ansible-tmp-1521107885.04-209498160937436/ec2_instance.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/vink/.ansible/tmp/ansible-tmp-1521107885.04-209498160937436/ /Users/vink/.ansible/tmp/ansible-tmp-1521107885.04-209498160937436/ec2_instance.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /Users/vink/.ansible/tmp/ansible-tmp-1521107885.04-209498160937436/ec2_instance.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /Users/vink/.ansible/tmp/ansible-tmp-1521107885.04-209498160937436/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py", line 1443, in <module>
main()
File "/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py", line 1428, in main
ensure_present(existing_matches=existing_matches, changed=changed, ec2=ec2, state=state)
File "/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py", line 1295, in ensure_present
instance_spec = build_run_instance_spec(module.params)
File "/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py", line 976, in build_run_instance_spec
spec['IamInstanceProfile'] = dict(Arn=determine_iam_role(params.get('iam_profile')))
TypeError: determine_iam_role() takes exactly 2 arguments (1 given)

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py\", line 1443, in <module>\n    main()\n  File \"/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py\", line 1428, in main\n    ensure_present(existing_matches=existing_matches, changed=changed, ec2=ec2, state=state)\n  File \"/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py\", line 1295, in ensure_present\n    instance_spec = build_run_instance_spec(module.params)\n  File \"/var/folders/wk/vr0zt0d53ssdsqqjvh5sv3fr0000gn/T/ansible_ejppjJ/ansible_module_ec2_instance.py\", line 976, in build_run_instance_spec\n    spec['IamInstanceProfile'] = dict(Arn=determine_iam_role(params.get('iam_profile')))\nTypeError: determine_iam_role() takes exactly 2 arguments (1 given)\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def determine_iam_role(name_or_arn):
    if re.match(r"^arn:aws:iam::\d+:instance-profile/[\w+=/,.@-]+$", name_or_arn):
        return name_or_arn
    iam = module.client("iam")
    try:
        role = iam.get_instance_profile(InstanceProfileName=name_or_arn)
        return role["InstanceProfile"]["Arn"]
    except botocore.exceptions.ClientError as e:
        if e.response["Error"]["Code"] == "NoSuchEntity":
            module.fail_json_aws(
                e, msg="Could not find instance_role {0}".format(name_or_arn)
            )
        module.fail_json_aws(
            e,
            msg="An error occurred while searching for instance_role {0}. Please try supplying the full ARN.".format(
                name_or_arn
            ),
        )