Search code examples
amazon-web-servicesamazon-ec2cloudcloud-init

AWS cloud-config not setting dns-nameservers


Here is the cloud.cfg on my instance (I had tampered it manually when provisioning the ami from which I launched the instance)

root@ip-10-17-0-121:~# cat /etc/cloud/cloud.cfg | grep -i resol -C 3

# Network configuration for ami
manage_resolv_conf: true

resolv_conf:
  nameservers: ['10.11.4.1']

However this is never taken into account given that:

# cat /etc/resolv.conf 
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.17.0.2
search eu-west-1.compute.internal

I have tried with and without creating the following file

# cat /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}

Solution

  • I managed to make my instance obtain my custom dns-nameserver as follows

    - name: pre_tasks --> Add my custom dns-nameserver
      lineinfile:
       path: /etc/dhcp/dhclient.conf
       regexp: '^#prepend domain-name-servers'
       line: 'prepend domain-name-servers 10.11.4.1;'
      become: yes
    

    However now I am getting the following warning:

    pkara@ip-10-17-0-35:~$ sudo -i
    sudo: unable to resolve host ip-10-17-0-35
    

    not accepting my answer until / unless I address this;

    edit_1: adding this to my cloud-config.yml which I am setting as user data did not help much:

    hostname: localhost
    manage_etc_hosts: true
    

    edit_2: adding this to my cloud-config.cfg helped me to address the above unable to resolve host problemn

    bootcmd:
      - echo "127.0.0.1 $(hostname)" >> /etc/hosts