Skip to content

Instantly share code, notes, and snippets.

Last active Jun 26, 2021
What would you like to do?
Userdata to install Unifi Network (unifi controller) on Ubuntu 20.04 when launching an x86 instance on AWS
# 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 --allow-remove-essential --allow-change-held-packages upgrade
# Update packages.
sudo apt-get upgrade -y
echo "Adding Unifi..."
# See
apt-get update && apt-get install ca-certificates apt-transport-https -y
echo 'deb stable ubiquiti' | sudo tee /etc/apt/sources.list.d/100-ubnt-unifi.list
wget -qO - | sudo apt-key add -
echo "deb xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
sudo apt-get update
sudo apt-key adv --keyserver --recv 06E85760C0A52C50
# Unifi needs Java 8
sudo apt-mark hold openjdk-16-*
sudo apt-mark hold openjdk-11-*
sudo apt-get install openjdk-8-jre-headless -y
# Disable interactive prompts for the unifi installer
echo "unifi unifi/has_backup boolean true" | debconf-set-selections
DEBIAN_FRONTEND=noninteractive apt-get install unifi -y
# Set timezone to NZ
ln -fs /usr/share/zoneinfo/Pacific/Auckland /etc/localtime
# Make sure ssm is running
sudo systemctl start
sudo systemctl enable
sudo systemctl start unifi
sudo systemctl enable unifi
# Associate a given elastic IP with an instance on boot.
sudo apt-get install awscli jq -y
# Get instance creds.
# @see
# @see
# @see
ROLE=$(curl -s
CR=$(curl -s$ROLE/)
INSTANCE_ID=$(curl -s
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"
echo "Start setting up your controller at https://$IPv4:8080"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment