Skip to content

Instantly share code, notes, and snippets.



Created Sep 30, 2020
What would you like to do?
AWS EC2 userdata for Unifi Controller on Ubuntu 16.04. Assumes Correto is installed and Ubiqiuiti repo is added
echo "Updating Unifi..."
systemctl stop unifi
apt update
# Disable interactive prompts for the unifi installer
echo "unifi unifi/has_backup boolean true" | debconf-set-selections
DEBIAN_FRONTEND=noninteractive apt-get install --only-upgrade unifi
# Update the Unifi config for a system using Amazon Corretto instead of OpenJDK
curl > /usr/lib/unifi/bin/unifi.init
# Set timezone to NZ
ln -fs /usr/share/zoneinfo/Pacific/Auckland /etc/localtime
# Make sure ssm is running
systemctl start
systemctl enable
systemctl start unifi
systemctl enable unifi
# Associate a given elastic IP with an instance on boot.
INSTANCE_ID=$(curl -s
# Associate this EIP on launch.
echo "Updating packages..."
# Force grub to update in a silent mode
DEBIAN_FRONTEND=noninteractive apt-get -y -o DPkg::options::="--force-confdef" -o DPkg::options::="--force-confold" -qq --force-yes upgrade
# Update packages.
apt upgrade -y
# Install the awscli
if ! [ -x "$(command -v aws)" ]; then
echo "Installing awscli..."
curl "" -o ""
yes | unzip
yes | ./aws/install
# Install jq
if ! [ -x "$(command -v jq)" ]; then
echo "Installing jq..."
snap install jq
# Get instance creds.
# @see
# @see
# @see
ROLE=$(curl -s
CR=$(curl -s$ROLE/)
export AWS_ACCESS_KEY_ID=$(echo $CR | jq -r '.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(echo $CR | jq -r '.SecretAccessKey')
export AWS_SESSION_TOKEN=$(echo $CR | jq -r '.Token')
# Now we can associate the address.
echo "Running: aws --region=$AWS_DEFAULT_REGION ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOC_ID --allow-reassociation"
aws --region=$AWS_DEFAULT_REGION ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOC_ID --allow-reassociation
IPv4=$(curl -s
echo "This instance now has public IPv4 $IPv4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment