Skip to content

Instantly share code, notes, and snippets.

@demiters
Created July 24, 2020 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demiters/acf08ae68ebb4bf235a69cc83faa51b5 to your computer and use it in GitHub Desktop.
Save demiters/acf08ae68ebb4bf235a69cc83faa51b5 to your computer and use it in GitHub Desktop.
Terraform setup for automated install of microk8s on a Ubuntu 20.04 DigitalOcean VPS. Edits required, see comments for more info.
# Edit name (example.com), region and size as you wish
resource "digitalocean_droplet" "example.com" {
image = "ubuntu-20-04-x64"
name = "example.com"
region = "fra1"
size = "s-2vcpu-4gb"
private_networking = true
ssh_keys = [
var.ssh_fingerprint
]
connection {
host = self.ipv4_address
user = "root"
type = "ssh"
private_key = file(var.pvt_key)
timeout = "2m"
}
provisioner "remote-exec" {
# Local scripts that are going to be executed on the new server, edit as necessary
scripts = [
./swap.sh, # https://gist.github.com/demiters/ba1c66c382cd2870764bf58066116a7a
./user.sh, # https://gist.github.com/demiters/f51a0094eee3e56ea55b0e31095d9fbb
./firewall.sh, # https://gist.github.com/demiters/00794411f9144b9f684c8ea7acd74743
./aliases.sh, # https://gist.github.com/demiters/c322d99db658e37ba30c8f13ba8b434b
./microk8s.sh # https://gist.github.com/demiters/e8a7a7dda40037f52115849a7e7680b5
]
inline = [
"export PATH=$PATH:/usr/bin"
]
}
}
#!/bin/bash
# Assumes your ssh public and private keys are located at .ssh/id_rsa[.pub]
terraform init
terraform plan \
-var "do_token=${DO_TOKEN}" \
-var "pub_key=$HOME/.ssh/id_rsa.pub" \
-var "pvt_key=$HOME/.ssh/id_rsa" \
-var "ssh_fingerprint=${DO_SSH_FINGERPRINT}"
terraform apply \
-var "do_token=${DO_TOKEN}" \
-var "pub_key=$HOME/.ssh/id_rsa.pub" \
-var "pvt_key=$HOME/.ssh/id_rsa" \
-var "ssh_fingerprint=${DO_SSH_FINGERPRINT}"
terraform show terraform.tfstate
#!/bin/bash
# Installs Terraform (Linux 64bit) for local use
mkdir -p $TF_PATH
filename="terraform.zip"
path_append='[[ ":$PATH:" != *":$TF_PATH/bin:"* ]] && PATH=$PATH:$TF_PATH/bin'
if curl --silent -o "${PWD}/${filename}" -L "https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip"; then
unzip "${filename}" -d "${TF_PATH}/bin"
if [[ -f "${PWD}/${filename}" ]]; then
echo "Removing the file.."
rm -f "${PWD}/${filename}"
echo "$path_append" >> $SH_RC"
. $SH_RC
fi
else
echo "Something went wrong"
fi
variable "do_token" {}
variable "pub_key" {}
variable "pvt_key" {}
variable "ssh_fingerprint" {}
provider "digitalocean" {
token = var.do_token
}
#!/bin/bash
# Edit these and run this script first
# DigitalOcean personal access token
export DO_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# MD5 fingerprint of your public key:
# ssh-keygen -E md5 -lf ~/.ssh/id_rsa.pub | awk '{print $2}'
export DO_SSH_FINGERFRINT="xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
# Terraform version to install
export TF_VERSION="0.12.24"
# Path to install Terraform in
export TF_PATH="/home/arturs/opt/terraform"
# Enable Terraform logging
export TF_LOG=1
# Shell config to write into
export SH_RC="/home/arturs/.zshrc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment