Unable to create a sample VPC using boto. socket.gaierror: [Errno -2] Name or service not known
Question: I am a beginner and am trying to create a VPC using boto.

    
    
     [root@localhost orchestration]# cat create-vpc.py
    #!/usr/bin/python
    
    import boto.vpc
    import pdb
    
    
    from boto.vpc import VPCConnection
    
    #c = VPCConnection()
    
    c=boto.vpc.connect_to_region('us-east-1')
    
    vpc = c.create_vpc('10.0.12.0/16')
    subnet = c.create_subnet(vpc.id, '10.0.12.1/24')
    

This is my sample script to create a VPC. However i am getting an error
"gaierror: [Errno -2] Name or service not known"

Here is the complete traceback.

    
    
    Traceback (most recent call last):
      File "create-vpc.py", line 13, in <module>
        vpc = c.create_vpc('10.0.12.0/16')
      File "/usr/lib/python2.7/site-packages/boto/vpc/__init__.py", line 135, in create_vpc
        return self.get_object('CreateVpc', params, VPC)
      File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1192, in get_object
        response = self.make_request(action, params, path, verb)
      File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1116, in make_request
        return self._mexe(http_request)
      File "/usr/lib/python2.7/site-packages/boto/connection.py", line 913, in _mexe
        self.is_secure)
      File "/usr/lib/python2.7/site-packages/boto/connection.py", line 705, in get_http_connection
        return self.new_http_connection(host, port, is_secure)
      File "/usr/lib/python2.7/site-packages/boto/connection.py", line 747, in new_http_connection
        connection = self.proxy_ssl(host, is_secure and 443 or 80)
      File "/usr/lib/python2.7/site-packages/boto/connection.py", line 792, in proxy_ssl
        int(self.proxy_port)), timeout)
      File "/usr/lib64/python2.7/socket.py", line 559, in create_connection
        for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    socket.gaierror: [Errno -2] Name or service not known
    

I have also configured the ~/.boto file with the access and the secret keys.
FYI,

    
    
    [root@localhost orchestration]# cat ~/.boto
    [Credentials]
    
    aws_access_key_id=<my-access-key>
    aws_secret_access_key=<my-secret-key>
    

I have also gone through some other similar questions that suggests to add
yun.local entry in the /etc/hosts file.

Here are my /etc/hosts file and /etc/resolv.conf files.

    
    
    [root@localhost orchestration]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.240.1 yun.local
    
    [root@localhost orchestration]# cat /etc/resolv.conf
    # Generated by NetworkManager
    search comviva.com
    nameserver 172.19.1.10
    nameserver 172.19.1.11
    nameserver 172.16.1.20
    # NOTE: the libc resolver may not support more than 3 nameservers.
    # The nameservers listed below may not be recognized.
    nameserver 172.16.1.22
    

Also o/p of telnet

    
    
    [root@localhost orchestration]# telnet yun.local 80
    Trying 192.168.240.1...
    telnet: connect to address 192.168.240.1: Connection refused
    

FYI,

    
    
    [root@localhost orchestration]# nslookup ec2.us-east-1.amazonaws.com
    Server:         172.19.1.11
    Address:        172.19.1.11#53
    
    Name:   ec2.us-east-1.amazonaws.com
    Address: 54.239.26.182
    
    [root@localhost orchestration]#
    [root@localhost orchestration]#
    [root@localhost orchestration]# dig ec2.us-east-1.amazonaws.com
    
    ; <<>> DiG 9.9.4-RedHat-9.9.4-18.el7 <<>> ec2.us-east-1.amazonaws.com
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24619
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
    
    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 4000
    ;; QUESTION SECTION:
    ;ec2.us-east-1.amazonaws.com.   IN      A
    
    ;; ANSWER SECTION:
    ec2.us-east-1.amazonaws.com. 0  IN      CNAME   ec2.amazonaws.com.
    ec2.amazonaws.com.      48      IN      A       54.239.20.1
    
    ;; Query time: 153 msec
    ;; SERVER: 172.19.1.10#53(172.19.1.10)
    ;; WHEN: Tue Nov 17 23:32:12 EST 2015
    ;; MSG SIZE  rcvd: 90
    

I also tried to create this VPC using Ansible and as expected got the same
error there as well. How do i overcome this gaierror ? Any answers shall be
heartily appreciated.


Answer: To create your VPCConnection (or really any service connection object) in
boto, you should use this incantation:

    
    
    import boto.vpc
    c = boto.vpc.connect_to_region('us-east-1')  # or whatever
    

In this way, you are guaranteed to have a properly initialized VPCConnection
object pointing to the right endpoint.

