Creating your own AMIs with AWS EC2 is pretty simple. You basically create a new EC2 instance, based on some image, modify the instance to your liking, shutdown the instance and create an AMI based on the current state of the instance.

Here is a quick tutorial how to create an Ubuntu 16.04 based AMI with Salt Minion pre-installed.

  1. Go to your AWS EC2 console and create a new instance (t2.micro for example) based on Ubuntu 16.04
  2. SSH into your new instance
  3. Update system

    sudo apt update && sudo apt full-upgrade -y

  4. Mask the salt-minion systemd service, to avoid salt-minion start after install (Basically disable the service)

    sudo ln -s /dev/null /etc/systemd/system/salt-minion.service

  5. Download Salt Bootstrap script

    wget -O bootstrap-salt.sh 'https://bootstrap.saltstack.com'

  6. Install and configure Salt Minion. -A specifies your Salt Master’s FQDN/IP

    sudo bash ./bootstrap-salt.sh -A salt-master -j '{"hash_type":"sha256"}'

  7. Do other stuff you like to have in the image, like SSH keys or packages.

  8. Shutdown the instance

    sudo shutdown -h now

  9. Back in the EC2 console, under “Instances”, select your instance and then “Actions -> Image -> Create Image” to create your AMI
  10. Enter a name, a description and create the image
  11. After the new AMI is available you can terminate the instance you used for creating the AMI

Now if you create new instances based on your Ubuntu AMI with Salt at “Step 3: Configure Instance Details” you need to specify a little script under “Advanced Details”. Expand the “Advanced Details” and enter the following lines.

#!/bin/bash
MID="ubuntu-ami-test"

apt update
apt full-upgrade -y
echo $MID > /etc/salt/minion_id
systemctl enable salt-minion
systemctl start salt-minion

Change the value of the MID variable to whatever the minion should be called. Launch the instance. On the first start of the instance, the script will set a proper name for the Salt Minon, enable the service and start the Salt Minion, which should now contact your Salt Master to be authenticated.