Skip to content

Instantly share code, notes, and snippets.

@soeirosantos
Last active May 18, 2020 12:59
Show Gist options
  • Save soeirosantos/6c9cf7efafafc710af0e063e1a90848a to your computer and use it in GitHub Desktop.
Save soeirosantos/6c9cf7efafafc710af0e063e1a90848a to your computer and use it in GitHub Desktop.

GCE Instance with persistent disk provisioned

In this lab we create a GCE instance, and attach and mount an SSD persistent disk to it.

To run this code you can clone this gist

$ git clone https://gist.github.com/6c9cf7efafafc710af0e063e1a90848a.git
$ cd 6c9cf7efafafc710af0e063e1a90848a

And excute (Terraform v0.12)

$ export TF_VAR_project=your_project_id
$ terraform init
$ terraform apply #verify the plan and confirm if everything looks fine

Check the logs and you'll see your disk properly mounted

[...]
null_resource.streams_app (remote-exec): Filesystem     Type      Size  Used Avail Use% Mounted on
null_resource.streams_app (remote-exec): udev           devtmpfs  267M     0  267M   0% /dev
[...]
null_resource.streams_app (remote-exec): /dev/sdb       ext4      4.9G   20M  4.9G   1% /mnt/disks/data

Clean up

$ terraform destroy
provider "google" {
version = "~> 2.0"
project = var.project
region = var.region
}
resource "random_pet" "streaming_app" {
keepers = {
machine_type = "${var.machine_type}"
machine_image = "${var.machine_image}"
}
}
resource "google_compute_network" "streaming_app" {
name = "${random_pet.streaming_app.id}-vpc"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "streaming_app" {
name = "${random_pet.streaming_app.id}-subnet"
region = var.region
network = google_compute_network.streaming_app.self_link
ip_cidr_range = var.subnet_prefix
}
resource "google_compute_firewall" "streaming_app" {
name = "${random_pet.streaming_app.id}-allow-ssh"
network = google_compute_network.streaming_app.self_link
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = [random_pet.streaming_app.id]
}
resource "google_compute_disk" "streaming_app" {
name = "${random_pet.streaming_app.id}-disk"
type = "pd-ssd"
zone = "${var.region}-b"
size = 5
physical_block_size_bytes = 4096
}
resource "tls_private_key" "ssh-key" {
algorithm = "RSA"
rsa_bits = "4096"
}
resource "google_compute_instance" "streaming_app" {
name = "${random_pet.streaming_app.id}-instance"
zone = "${var.region}-b"
machine_type = random_pet.streaming_app.keepers.machine_type
boot_disk {
initialize_params {
image = random_pet.streaming_app.keepers.machine_image
}
}
network_interface {
subnetwork = google_compute_subnetwork.streaming_app.self_link
access_config {
}
}
attached_disk {
source = google_compute_disk.streaming_app.self_link
device_name = google_compute_disk.streaming_app.name
mode = "READ_WRITE"
}
metadata = {
ssh-keys = "ubuntu:${chomp(tls_private_key.ssh-key.public_key_openssh)} terraform"
}
tags = [random_pet.streaming_app.id]
}
resource "null_resource" "streaming_app" {
depends_on = [
google_compute_instance.streaming_app,
]
triggers = {
build_number = timestamp()
}
provisioner "file" {
source = "mount_disk.sh"
destination = "/home/ubuntu/mount_disk.sh"
connection {
type = "ssh"
user = "ubuntu"
timeout = "300s"
private_key = tls_private_key.ssh-key.private_key_pem
host = google_compute_instance.streaming_app.network_interface.0.access_config.0.nat_ip
}
}
provisioner "remote-exec" {
inline = [
"chmod +x mount_disk.sh",
"./mount_disk.sh google-${google_compute_instance.streaming_app.attached_disk.0.device_name}",
]
connection {
type = "ssh"
user = "ubuntu"
timeout = "300s"
private_key = tls_private_key.ssh-key.private_key_pem
host = google_compute_instance.streaming_app.network_interface.0.access_config.0.nat_ip
}
}
}
#!/bin/bash
# https://cloud.google.com/compute/docs/disks/add-persistent-disk
# https://wiki.archlinux.org/index.php/Fstab
set -e
DEVICE_PATH=$(realpath /dev/disk/by-id/$1)
MNT_DIR=${2:-data}
echo "DEVICE_PATH: $DEVICE_PATH"
echo "MNT_DIR: $MNT_DIR"
if sudo blkid $DEVICE_PATH; then
echo "device $DEVICE_PATH already mount"
sudo df -Th
exit
else
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard $DEVICE_PATH
sudo mkdir -p /mnt/disks/$MNT_DIR
sudo mount -o discard,defaults /dev/sdb /mnt/disks/$MNT_DIR
sudo chmod a+w /mnt/disks/$MNT_DIR
echo UUID=$(sudo blkid -s UUID -o value $DEVICE_PATH) /mnt/disks/$MNT_DIR ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab
cat /etc/fstab
sudo df -Th
fi
variable "project" {
description = "GCP project."
}
variable "region" {
description = "GCP region."
default = "us-central1"
}
variable "subnet_prefix" {
description = "Address prefix for the subnet."
default = "10.0.10.0/24"
}
variable "machine_type" {
description = "GCP instance type."
default = "f1-micro"
}
variable "machine_image" {
description = "GCP machine image."
default = "ubuntu-os-cloud/ubuntu-1804-lts"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment