Using cloud-init on AWS, Digital Ocean, or OpenStack

As cloud-init is an initialization system for cloud instances, we need to find a way to send the cloud-config YAML file to the bootstrapping process. On all IaaS providers supporting cloud-init, there's a field where we can paste our file. We'll review how cloud-init works on three important IaaS providers—AWS, Digital Ocean, and OpenStack.

Getting ready

To step through this recipe, you will need an account on Amazon Web Services, Digital Ocean, or some OpenStack deployment, or on all of them if you want to try them all!

How to do it…

To illustrate cloud-init usage, we'll create the simplest cloud-config file on Ubuntu 16.04 and CentOS 7.2, installing packages such as htop, tcpdump, docker, or nmap that aren't usually installed by default on most Linux distributions. This is how a very simple cloud-config file looks:

#cloud-config
# Install packages on first boot
packages:
  - tcpdump
  - docker
  - nmap

Using cloud-init on Amazon Web Services

Using the AWS Console, when launching your instance, click on Advanced Details and we'll be able to paste our sample (and simple) cloud-config YAML file, or even simply upload it:

Using cloud-init on Amazon Web Services

In this case, the Ubuntu 16.04 instance we just launched will already have the htop and tcpdump system tools installed, along with the Linux distribution's supported version of Docker:

ubuntu@ip-172-31-40-77:~$ which htop
/usr/bin/htop
ubuntu@ip-172-31-40-77:~$ which tcpdump
/usr/sbin/tcpdump
ubuntu@ip-172-31-40-77:~$ docker --version
Docker version 1.11.2, build b9f10c9

Note

We can manually update cloud-config.yml of a particular instance by powering off the instance, then under the Actions menu, navigate to Instance Settings | View/Change User Data. Start the EC2 instance again and the updated configuration is applied.

Using cloud-init on Digital Ocean

The situation is similar on Digital Ocean. When creating a new droplet, be sure to tick the User data checkbox under the Select additional options section and paste the cloud-config file content:

Using cloud-init on Digital Ocean

After a few seconds of boot time and package installation, our customized Ubuntu distribution is available:

root@ubuntu-512mb-nyc3-01:~# which htop
/usr/bin/htop
root@ubuntu-512mb-nyc3-01:~# which tcpdump
/usr/sbin/tcpdump
root@ubuntu-512mb-nyc3-01:~# docker --version
Docker version 1.11.2, build b9f10c9

Using cloud-init on OpenStack

When creating an instance on OpenStack, using the Horizon dashboard, click on the Post-Creation tab, and paste the cloud-config YAML content in the text box. Alternatively, it is possible to upload the file:

Using cloud-init on OpenStack

Verify the requested packages were installed, this time on a CentOS 7.2 box:

[centos@cloud-init-demo ~]$ which nmap
/usr/bin/nmap
[centos@cloud-init-demo ~]$ docker --version
Docker version 1.10.3, build cb079f6-unsupported
[centos@cloud-init-demo ~]$ which tcpdump
/usr/sbin/tcpdump

Combining cloud-init and Terraform for any IaaS

In the previous chapters about Terraform, we've in fact already used a cloud-init file a few times.

On Amazon Web Services, using the aws_instance resource to launch an EC2 VM, we use the user_data argument to pass the cloud-config file content, and in this case, using the file() interpolation:

resource "aws_instance" "vm" {
  ami           = "ami-643d4217"
  instance_type = "t2.micro"
  key_name      = "manual cloud init"
  user_data     = "${file("cloud-config.yml")}"
}

The equivalent for a Digital Ocean VM is the user_data argument as well:

resource "digitalocean_droplet" "vm" {
  image              = "ubuntu-14-04-x64"
  name               = "ubuntu"
  region             = "ams3"
  size               = "512mb"
  ssh_keys           = ["keys/admin_key"]
  user_data          = "${file("cloud-config.yml")}"
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.0.240