Sunday, December 14, 2014

Configuring a Linux Swap Device with Cloud-Init

Cloud-Init is a set of Python scripts used to configure Linux instances when they boot in AWS. Cloud-Init is included on Ubuntu and Amazon Linux AMIs.

You can think of a Cloud Init script as a bare-bones Configuration Management solution like Chef or Puppet. A Cloud-Init script is passed as user data. If you have ever passed a shell script as user data, it was Cloud-Init that queried the meta-data service and executed the script. But, Cloud-Init offers a higher level syntax known as cloud-config.

One of the common examples is to mount devices when the instance boots. An obvious example is to create a swap volume from the ephemeral disks that you are likely not using. Here is a script that will create a swap volume on an m3.medium with its ephemeral volume attached at /dev/sdb. (Note: even though you select /dev/sdb in the console, Linux will see it /dev/xvdb)

repo_update: true
repo_upgrade: all

  - [ ephemeral0, none, swap, sw, 0, 0 ]

 - mkswap /dev/xvdb
 - swapon /dev/xvdb

There are three parts to the script:

First, repo_update and repo_upgrade will update the repository and upgrade all packages respectively. Note that this only occurs once on the first boot.

Second, mounts will mount the ephemeral volume as a swap device with no mount point. This will both mount the volume and update fstab so it is mounted on future reboots.

Third, bootcmd will run a series of commands on each boot. In this case setup the swap area and then enable it.

Note that I am using bootcmd rather than runcmd. runcmd will only run on the first boot, while bootcmd will run every time Linux boots. If you used runcmd the swap device would be configured on the first boot, but if you start/stop the instance you would be assigned a new host and the swap device would never be configured.