# Buggy code
def _get_elasticache_connection(self):
    """Get an elasticache connection"""
    try:
        endpoint = "elasticache.%s.amazonaws.com" % self.region
        connect_region = RegionInfo(name=self.region, endpoint=endpoint)
        return ElastiCacheConnection(region=connect_region, **self.aws_connect_kwargs)
    except boto.exception.NoAuthHandlerFound as e:
        self.module.fail_json(msg=e.message)


# Traceback (socket.gaierror)
No config file found; using defaults
Set default localhost to localhost
Loading callback plugin minimal of type stdout, v2.0 from /usr/local/lib/python2.7/dist-packages/ansible/plugins/callback/__init__.pyc
META: ran handlers
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/cloud/amazon/elasticache_subnet_group.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: sszwed
<localhost> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/sszwed/.ansible/tmp/ansible-tmp-1493119113.83-14610261905763 `" &amp;&amp; echo ansible-tmp-1493119113.83-14610261905763="` echo /home/sszwed/.ansible/tmp/ansible-tmp-1493119113.83-14610261905763 `" ) &amp;&amp; sleep 0'
<localhost> PUT /tmp/tmp3G1P22 TO /home/sszwed/.ansible/tmp/ansible-tmp-1493119113.83-14610261905763/elasticache_subnet_group.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/sszwed/.ansible/tmp/ansible-tmp-1493119113.83-14610261905763/ /home/sszwed/.ansible/tmp/ansible-tmp-1493119113.83-14610261905763/elasticache_subnet_group.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/sszwed/.ansible/tmp/ansible-tmp-1493119113.83-14610261905763/elasticache_subnet_group.py; rm -rf "/home/sszwed/.ansible/tmp/ansible-tmp-1493119113.83-14610261905763/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_vLU7aO/ansible_module_elasticache_subnet_group.py", line 160, in <module>
main()
File "/tmp/ansible_vLU7aO/ansible_module_elasticache_subnet_group.py", line 129, in main
matching_groups = conn.describe_cache_subnet_groups(group_name, max_records=100)
File "/usr/local/lib/python2.7/dist-packages/boto/elasticache/layer1.py", line 851, in describe_cache_subnet_groups
path='/', params=params)
File "/usr/local/lib/python2.7/dist-packages/boto/elasticache/layer1.py", line 1658, in _make_request
path='/', params=params)
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1116, in make_request
return self._mexe(http_request)
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1030, in _mexe
raise ex
socket.gaierror: [Errno -2] Name or service not known

localhost | FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_vLU7aO/ansible_module_elasticache_subnet_group.py\", line 160, in <module>\n    main()\n  File \"/tmp/ansible_vLU7aO/ansible_module_elasticache_subnet_group.py\", line 129, in main\n    matching_groups = conn.describe_cache_subnet_groups(group_name, max_records=100)\n  File \"/usr/local/lib/python2.7/dist-packages/boto/elasticache/layer1.py\", line 851, in describe_cache_subnet_groups\n    path='/', params=params)\n  File \"/usr/local/lib/python2.7/dist-packages/boto/elasticache/layer1.py\", line 1658, in _make_request\n    path='/', params=params)\n  File \"/usr/local/lib/python2.7/dist-packages/boto/connection.py\", line 1116, in make_request\n    return self._mexe(http_request)\n  File \"/usr/local/lib/python2.7/dist-packages/boto/connection.py\", line 1030, in _mexe\n    raise ex\nsocket.gaierror: [Errno -2] Name or service not known\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def _get_elasticache_connection(self):
    """Get an elasticache connection"""
    try:
        return connect_to_region(region_name=self.region, **self.aws_connect_kwargs)
    except boto.exception.NoAuthHandlerFound as e:
        self.module.fail_json(msg=e.message)