# Buggy code
def build_run_instance_spec(params, ec2=None):
    if ec2 is None:
        ec2 = module.client("ec2")

    spec = dict(
        ClientToken=uuid.uuid4().hex,
        MaxCount=1,
        MinCount=1,
    )
    # network parameters
    spec["NetworkInterfaces"] = build_network_spec(params, ec2)
    spec["BlockDeviceMappings"] = build_volume_spec(params)
    spec.update(**build_top_level_options(params))
    spec["TagSpecifications"] = build_instance_tags(params)

    # IAM profile
    if params.get("instance_role"):
        spec["IamInstanceProfile"] = dict(
            Arn=determine_iam_role(params.get("iam_profile"))
        )

    spec["InstanceType"] = params["instance_type"]
    return spec


# 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 build_run_instance_spec(params, ec2=None):
    if ec2 is None:
        ec2 = module.client("ec2")

    spec = dict(
        ClientToken=uuid.uuid4().hex,
        MaxCount=1,
        MinCount=1,
    )
    # network parameters
    spec["NetworkInterfaces"] = build_network_spec(params, ec2)
    spec["BlockDeviceMappings"] = build_volume_spec(params)
    spec.update(**build_top_level_options(params))
    spec["TagSpecifications"] = build_instance_tags(params)

    # IAM profile
    if params.get("instance_role"):
        spec["IamInstanceProfile"] = dict(
            Arn=determine_iam_role(params.get("instance_role"))
        )

    spec["InstanceType"] = params["instance_type"]
    return spec