# Buggy code
def main():
    argument_spec = ec2_argument_spec()
    argument_spec.update(
        dict(
            az=dict(default=None, required=False),
            cidr=dict(default=None, required=True),
            state=dict(default="present", choices=["present", "absent"]),
            tags=dict(
                default=None, required=False, type="dict", aliases=["resource_tags"]
            ),
            vpc_id=dict(default=None, required=True),
        )
    )

    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

    if not HAS_BOTO:
        module.fail_json(msg="boto is required for this module")

    region, ec2_url, aws_connect_params = get_aws_connection_info(module)

    if region:
        try:
            connection = connect_to_aws(boto.vpc, region, **aws_connect_params)
        except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
            module.fail_json(msg=str(e))
    else:
        module.fail_json(msg="region must be specified")

    vpc_id = module.params.get("vpc_id")
    tags = module.params.get("tags")
    cidr = module.params.get("cidr")
    az = module.params.get("az")
    state = module.params.get("state")

    try:
        if state == "present":
            result = ensure_subnet_present(
                connection, vpc_id, cidr, az, tags, check_mode=module.check_mode
            )
        elif state == "absent":
            result = ensure_subnet_absent(
                connection, vpc_id, cidr, check_mode=module.check_mode
            )
    except AnsibleVPCSubnetException as e:
        module.fail_json(msg=str(e))

    module.exit_json(**result)


# Traceback (TypeError)
TASK [Create default VPC subnets.] *********************************************
task path: /Users/chhaci/Documents/Magento2/provision/provision.yml:31
Using module file /usr/local/Cellar/ansible/2.2.1.0_1/libexec/lib/python2.7/site-packages/ansible/modules/extras/cloud/amazon/ec2_vpc_subnet.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: chhaci
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711 `" &amp;&amp; echo ansible-tmp-1487775336.92-72635135088711="` echo ~/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/tmpAFBO2N TO /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ec2_vpc_subnet.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ec2_vpc_subnet.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/local/Cellar/ansible/2.2.1.0_1/libexec/bin/python2.7 /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ec2_vpc_subnet.py; rm -rf "/Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 272, in <module>
main()
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 261, in main
check_mode=module.check_mode)
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 199, in ensure_subnet_present
ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 169, in ensure_tags
to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])
TypeError: 'NoneType' object is not iterable

failed: [localhost] (item=172.31.0.0/20) => {
"failed": true,
"invocation": {
"module_name": "ec2_vpc_subnet"
},
"item": "172.31.0.0/20",
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 272, in <module>\n    main()\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 261, in main\n    check_mode=module.check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 199, in ensure_subnet_present\n    ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 169, in ensure_tags\n    to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])\nTypeError: 'NoneType' object is not iterable\n",
"module_stdout": "",
"msg": "MODULE FAILURE"
}
Using module file /usr/local/Cellar/ansible/2.2.1.0_1/libexec/lib/python2.7/site-packages/ansible/modules/extras/cloud/amazon/ec2_vpc_subnet.py
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937 `" &amp;&amp; echo ansible-tmp-1487775338.13-18218926660937="` echo ~/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/tmpGh7EMs TO /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ec2_vpc_subnet.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ec2_vpc_subnet.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/local/Cellar/ansible/2.2.1.0_1/libexec/bin/python2.7 /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ec2_vpc_subnet.py; rm -rf "/Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 272, in <module>
main()
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 261, in main
check_mode=module.check_mode)
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 199, in ensure_subnet_present
ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)
File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 169, in ensure_tags
to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])
TypeError: 'NoneType' object is not iterable

failed: [localhost] (item=172.31.16.0/20) => {
"failed": true,
"invocation": {
"module_name": "ec2_vpc_subnet"
},
"item": "172.31.16.0/20",
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 272, in <module>\n    main()\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 261, in main\n    check_mode=module.check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 199, in ensure_subnet_present\n    ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 169, in ensure_tags\n    to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])\nTypeError: 'NoneType' object is not iterable\n",
"module_stdout": "",
"msg": "MODULE FAILURE"
}

# Fixed code
def main():
    argument_spec = ec2_argument_spec()
    argument_spec.update(
        dict(
            az=dict(default=None, required=False),
            cidr=dict(default=None, required=True),
            state=dict(default="present", choices=["present", "absent"]),
            tags=dict(
                default={}, required=False, type="dict", aliases=["resource_tags"]
            ),
            vpc_id=dict(default=None, required=True),
        )
    )

    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

    if not HAS_BOTO:
        module.fail_json(msg="boto is required for this module")

    region, ec2_url, aws_connect_params = get_aws_connection_info(module)

    if region:
        try:
            connection = connect_to_aws(boto.vpc, region, **aws_connect_params)
        except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
            module.fail_json(msg=str(e))
    else:
        module.fail_json(msg="region must be specified")

    vpc_id = module.params.get("vpc_id")
    tags = module.params.get("tags")
    cidr = module.params.get("cidr")
    az = module.params.get("az")
    state = module.params.get("state")

    try:
        if state == "present":
            result = ensure_subnet_present(
                connection, vpc_id, cidr, az, tags, check_mode=module.check_mode
            )
        elif state == "absent":
            result = ensure_subnet_absent(
                connection, vpc_id, cidr, check_mode=module.check_mode
            )
    except AnsibleVPCSubnetException as e:
        module.fail_json(msg=str(e))

    module.exit_json(**result)