Skip to content

Instantly share code, notes, and snippets.

@nckg
Last active March 29, 2024 10:14
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 nckg/1da0f34d1ead72c02323585509b5c2bb to your computer and use it in GitHub Desktop.
Save nckg/1da0f34d1ead72c02323585509b5c2bb to your computer and use it in GitHub Desktop.
Setup raspberry Pi 3 (ubuntu server, php7.2, nginx) with Terraform
# https://jtway.co/ubuntu-server-16-06-on-raspberry-pi-3-via-terraform-93dccaef5ddb?gi=e5fcb8404a4d
variable "server_ip" {
default = "SERVER IP ADDRESS"
}
# Terraform documentation
# * Provisioner null_resource: https://www.terraform.io/docs/provisioners/null_resource.html
# * Provisioner Connections: https://www.terraform.io/docs/provisioners/null_resource.html
# Ubuntu issues:
# https://bugs.launchpad.net/ubuntu/+source/linux-raspi2/+bug/1652270
# https://bugs.launchpad.net/ubuntu/+source/linux-raspi2/+bug/1652270/comments/44
# https://bugs.launchpad.net/ubuntu/+source/linux-firmware-raspi2/+bug/1691729
resource "null_resource" "upgrade-packages" {
connection {
type = "ssh"
user = "ubuntu"
host = "${var.server_ip}"
}
provisioner "remote-exec" {
inline = [
"sudo apt-mark hold linux-raspi2 linux-image-raspi2 linux-headers-raspi2",
"sudo dpkg-divert --divert /lib/firmware/brcm/brcmfmac43430-sdio-2.bin --package linux-firmware-raspi2 --rename --add /lib/firmware/brcm/brcmfmac43430-sdio.bin",
"sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y",
"sudo apt-mark unhold linux-raspi2 linux-image-raspi2 linux-headers-raspi2",
"sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y",
"sudo sed 's/device_tree_address.*/device_tree_address=0x02008000/g; s/^.*device_tree_end.*//g;' -i /boot/firmware/config.txt",
"sudo reboot"
]
}
}
variable "server_hostname" {
default = "SERVER HOSTNAME"
}
# Ubuntu reference for hostnamectl: http://manpages.ubuntu.com/manpages/trusty/man1/hostnamectl.1.html
resource "null_resource" "set-hostname" {
connection {
type = "ssh"
user = "ubuntu"
host = "${var.server_ip}"
}
provisioner "remote-exec" {
inline = [
"sudo hostnamectl set-hostname ${var.server_hostname}",
"echo '127.0.0.1 ${var.server_hostname}' | sudo tee -a /etc/hosts",
"sudo reboot"
]
}
}
variable "wlan_ssid" {
default = "SSID"
}
variable "wlan_psk" {
default = "WIFI PASSWORD"
}
# https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=141834
# https://medium.com/a-swift-misadventure/how-to-setup-your-raspberry-pi-2-3-with-ubuntu-16-04-without-cables-headlessly-9e3eaad32c01
resource "null_resource" "wifi" {
# depends_on = ["null_resource.upgrade-packages"]
connection {
type = "ssh"
user = "ubuntu"
host = "${var.server_ip}"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get install -y wireless-tools wpasupplicant",
"cd /lib/firmware/brcm/",
"sudo wget https://raw.githubusercontent.com/RPi-Distro/firmware-nonfree/master/brcm/brcmfmac43430-sdio.bin",
"sudo wget https://raw.githubusercontent.com/RPi-Distro/firmware-nonfree/master/brcm/brcmfmac43430-sdio.txt",
"echo \"allow-hotplug wlan0\niface wlan0 inet dhcp\nwpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\" | sudo tee /etc/network/interfaces.d/10-wlan.cfg",
"echo \"network={\\nssid=\\\"${var.wlan_ssid}\\\"\\npsk=\\\"${var.wlan_psk}\\\"\\n}\" | sudo tee /etc/wpa_supplicant/wpa_supplicant.conf",
"sudo reboot"
]
}
}
resource "null_resource" "lemp" {
connection {
type = "ssh"
user = "ubuntu"
host = "${var.server_ip}"
}
provisioner "remote-exec" {
inline = [
"sudo add-apt-repository ppa:ondrej/php -y",
"sudo apt update",
"sudo apt install php7.2 php7.2-common php7.2-cli php7.2-fpm php7.2-zip php7.2-mysql php7.2-xml php7.2-mbstring -y",
"sudo apt install nginx -y"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment