# Buggy code
def create_dirkey(module, s3, bucket, obj):
    if module.check_mode:
        module.exit_json(
            msg="PUT operation skipped - running in check mode", changed=True
        )
    try:
        bucket = s3.Bucket(bucket)
        key = bucket.new_key(obj)
        key.set_contents_from_string("")
        for acl in module.params.get("permission"):
            s3.put_object_acl(ACL=acl, Bucket=bucket, Key=obj)
        module.exit_json(
            msg="Virtual directory %s created in bucket %s" % (obj, bucket.name),
            changed=True,
        )
    except botocore.exceptions.ClientError as e:
        module.fail_json(
            msg="Failed while creating object %s." % obj,
            exception=traceback.format_exc(),
            **camel_dict_to_snake_dict(e.response),
        )


# Traceback (AttributeError)
Using module file /home/jwitkowski/throtle-ansible-venv/lib/python2.7/site-packages/ansible/modules/cloud/amazon/_s3.py
<localhost> EXEC /bin/sh -c 'AWS_DEFAULT_REGION=us-east-1 AWS_ACCESS_KEY_ID=AWS_SECRET_ACCESS_KEY= python &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_aqikeG/ansible_module_s3.py", line 830, in <module>
main()
File "/tmp/ansible_aqikeG/ansible_module_s3.py", line 796, in main
create_dirkey(module, s3, bucket, dirobj)
File "/tmp/ansible_aqikeG/ansible_module_s3.py", line 378, in create_dirkey
bucket = s3.Bucket(bucket)
File "/home/jwitkowski/throtle-ansible-venv/lib/python2.7/site-packages/botocore/client.py", line 553, in __getattr__
self.__class__.__name__, item)
AttributeError: 'S3' object has no attribute 'Bucket'

failed: [localhost] (item=[u'throtle-data', {u'bucket': u'throtle-data', u'name': u'testing'}, u'Download']) => {
"changed": false,
"failed": true,
"item": [
"throtle-data",
{
"bucket": "throtle-data",
"name": "testing"
},
"Download"
],
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_aqikeG/ansible_module_s3.py\", line 830, in <module>\n    main()\n  File \"/tmp/ansible_aqikeG/ansible_module_s3.py\", line 796, in main\n    create_dirkey(module, s3, bucket, dirobj)\n  File \"/tmp/ansible_aqikeG/ansible_module_s3.py\", line 378, in create_dirkey\n    bucket = s3.Bucket(bucket)\n  File \"/home/jwitkowski/throtle-ansible-venv/lib/python2.7/site-packages/botocore/client.py\", line 553, in __getattr__\n    self.__class__.__name__, item)\nAttributeError: 'S3' object has no attribute 'Bucket'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
Using module file /home/jwitkowski/throtle-ansible-venv/lib/python2.7/site-packages/ansible/modules/cloud/amazon/_s3.py
<localhost> EXEC /bin/sh -c 'AWS_DEFAULT_REGION=us-east-1 AWS_ACCESS_KEY_ID=AWS_SECRET_ACCESS_KEY= python &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_AGetjO/ansible_module_s3.py", line 830, in <module>
main()
File "/tmp/ansible_AGetjO/ansible_module_s3.py", line 796, in main
create_dirkey(module, s3, bucket, dirobj)
File "/tmp/ansible_AGetjO/ansible_module_s3.py", line 378, in create_dirkey
bucket = s3.Bucket(bucket)
File "/home/jwitkowski/throtle-ansible-venv/lib/python2.7/site-packages/botocore/client.py", line 553, in __getattr__
self.__class__.__name__, item)
AttributeError: 'S3' object has no attribute 'Bucket'

failed: [localhost] (item=[u'throtle-data', {u'bucket': u'throtle-data', u'name': u'testing'}, u'Upload']) => {
"changed": false,
"failed": true,
"item": [
"throtle-data",
{
"bucket": "throtle-data",
"name": "testing"
},
"Upload"
],
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_AGetjO/ansible_module_s3.py\", line 830, in <module>\n    main()\n  File \"/tmp/ansible_AGetjO/ansible_module_s3.py\", line 796, in main\n    create_dirkey(module, s3, bucket, dirobj)\n  File \"/tmp/ansible_AGetjO/ansible_module_s3.py\", line 378, in create_dirkey\n    bucket = s3.Bucket(bucket)\n  File \"/home/jwitkowski/throtle-ansible-venv/lib/python2.7/site-packages/botocore/client.py\", line 553, in __getattr__\n    self.__class__.__name__, item)\nAttributeError: 'S3' object has no attribute 'Bucket'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def create_dirkey(module, s3, bucket, obj):
    if module.check_mode:
        module.exit_json(
            msg="PUT operation skipped - running in check mode", changed=True
        )
    try:
        s3.put_object(Bucket=bucket, Key=obj, Body=b"")
        for acl in module.params.get("permission"):
            s3.put_object_acl(ACL=acl, Bucket=bucket, Key=obj)
        module.exit_json(
            msg="Virtual directory %s created in bucket %s" % (obj, bucket),
            changed=True,
        )
    except botocore.exceptions.ClientError as e:
        module.fail_json(
            msg="Failed while creating object %s." % obj,
            exception=traceback.format_exc(),
            **camel_dict_to_snake_dict(e.response),
        )