Last active
June 26, 2021 00:06
-
-
Save cam8001/d3745f0652082a92f7fac8a8cac38fa1 to your computer and use it in GitHub Desktop.
Userdata to install Unifi Network (unifi controller) on Ubuntu 20.04 when launching an x86 instance on AWS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Associate this EIP on launch. | |
ALLOC_ID=eipalloc-0d896138148fed55c | |
AWS_DEFAULT_REGION=ap-southeast-2 | |
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 https://help.ui.com/hc/en-us/articles/220066768-UniFi-How-to-Install-and-Update-via-APT-on-Debian-or-Ubuntu | |
apt-get update && apt-get install ca-certificates apt-transport-https -y | |
echo 'deb https://www.ui.com/downloads/unifi/debian stable ubiquiti' | sudo tee /etc/apt/sources.list.d/100-ubnt-unifi.list | |
wget -qO - https://www.mongodb.org/static/pgp/server-3.4.asc | sudo apt-key add - | |
echo "deb https://repo.mongodb.org/apt/ubuntu 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 keyserver.ubuntu.com --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 snap.amazon-ssm-agent.amazon-ssm-agent.service | |
sudo systemctl enable snap.amazon-ssm-agent.amazon-ssm-agent.service | |
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 https://github.com/pierreg256/eip/blob/master/auto-ip.sh | |
# @see https://gist.github.com/fgassert/8923461 | |
# @see https://gist.github.com/odessky/e7b8b31e3b710cf3e29e85339d4ec5e6 | |
ROLE=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/) | |
CR=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/$ROLE/) | |
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) | |
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 http://169.254.169.254/latest/meta-data/public-ipv4) | |
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