How to create an Ubuntu 16.04 AWS EC2 AMI with Salt Minion pre-installed
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.
- Go to your AWS EC2 console and create a new instance (t2.micro for example) based on Ubuntu 16.04
- SSH into your new instance
-
Update system
sudo apt update && sudo apt full-upgrade -y
-
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
-
Download Salt Bootstrap script
wget -O bootstrap-salt.sh 'https://bootstrap.saltstack.com'
-
Install and configure Salt Minion.
-A
specifies your Salt Master’s FQDN/IPsudo bash ./bootstrap-salt.sh -A salt-master -j '{"hash_type":"sha256"}'
-
Do other stuff you like to have in the image, like SSH keys or packages.
-
Shutdown the instance
sudo shutdown -h now
- Back in the EC2 console, under “Instances”, select your instance and then “Actions -> Image -> Create Image” to create your AMI
- Enter a name, a description and create the image
- 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.