Skip to content

Instantly share code, notes, and snippets.

@veggiemonk
Last active June 26, 2019 11:49
Show Gist options
  • Save veggiemonk/097890ff101a995f3c31f4d25b69665e to your computer and use it in GitHub Desktop.
Save veggiemonk/097890ff101a995f3c31f4d25b69665e to your computer and use it in GitHub Desktop.
Terraform gcp workshop

Terraform

Install

mkdir -p $HOME/bin
cd $HOME/bin
wget https://releases.hashicorp.com/terraform/0.11.9/terraform_0.11.9_linux_amd64.zip
unzip terraform_0.11.9_linux_amd64.zip
export PATH="$PATH:$HOME/bin"
echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc
cd $HOME

Authenticate

Note: If using Cloud Shell, there is no need to authenticate

# Run this on your local machine to authenticated your account to GCP.
# Terraform will pick it up automatically

gcloud auth application-default login

Reference: https://www.terraform.io/docs/providers/google/provider_reference.html

Challenge

Create a VM and a storage bucket and access the bucket from the VM. It could be as simple as writing a text file to that bucket.

install gsutil to interact with the bucket (link)

provider "google" {
region = "europe-west1"
# credentials = "${file("~/path/to/creds.json")}"
}
resource "google_storage_bucket" "mybucket" {
name = "${var.bucket_name}"
project = "${var.project_id}"
location = "EU"
}
resource "google_compute_instance" "vm" {
project = "${var.project_id}"
name = "${var.vm_name}"
machine_type = "f1-micro"
zone = "europe-west1-b"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
access_config {} # Ephemeral IP
}
metadata_startup_script = "echo hi > /terraform_workshop.txt"
service_account {
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
}
}
output "vm_id" {
value = "${google_compute_instance.vm.id}"
}
provider "google" {
region = "europe-west1"
# credentials = "${file("~/path/to/creds.json")}"
}
resource "google_storage_bucket" "mybucket" {
name = "${var.bucket_name}"
project = "${var.project_id}"
location = "EU"
}
resource "google_service_account" "bucket-sa" {
account_id = "bucket-vm"
display_name = "VM service account"
project = "${var.project_id}"
}
resource "google_storage_bucket_iam_member" "myvm" {
count = "${length(var.storage_bucket_roles)}"
role = "${element(var.storage_bucket_roles, count.index)}"
bucket = "${google_storage_bucket.mybucket.name}"
member = "serviceAccount:${google_service_account.bucket-sa.email}"
}
resource "google_compute_instance" "vm" {
project = "${var.project_id}"
name = "${var.vm_name}"
machine_type = "f1-micro"
zone = "europe-west1-b"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
access_config {} # Ephemeral IP
}
metadata_startup_script = "echo hi > /terraform_workshop.txt"
service_account {
email = "${google_service_account.bucket-sa.email}"
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
}
}
variable "project_id" {
default = "ID OF PROJECT"
}
variable "bucket_name" {
default = "my_bucket"
}
variable "vm_name" {
default = "my_vm"
}
variable "storage_bucket_roles" {
type = "list"
default = [
"roles/storage.legacyBucketReader",
"roles/storage.objectAdmin",
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment