From 538338870a4300a07a5939778837a7b83f66f0c8 Mon Sep 17 00:00:00 2001 From: Thamu Mnyulwa <43708808+ThamuMnyulwa@users.noreply.github.com> Date: Mon, 2 Oct 2023 09:50:19 +0200 Subject: [PATCH 1/7] Added terraform-service-key.json to gitignore. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 09fd6d4..bc87cc7 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ terraform.rc .terraform.lock.hcl +terraform-service-key.json From f7b673649f9c2af672d5bb03a7ab02a8bf5152cd Mon Sep 17 00:00:00 2001 From: Thamu Mnyulwa <43708808+ThamuMnyulwa@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:34:28 +0200 Subject: [PATCH 2/7] Added a run.sh script to follow when running with a service account rather than as root user --- chap01/cloudshell/run.sh | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 chap01/cloudshell/run.sh diff --git a/chap01/cloudshell/run.sh b/chap01/cloudshell/run.sh new file mode 100644 index 0000000..6695557 --- /dev/null +++ b/chap01/cloudshell/run.sh @@ -0,0 +1,45 @@ + +#!/bin/bash + +# Update the package list +sudo apt-get update +# sudo yum check-update # red-hat version + +# Upgrade all installed packages including kernel and kernel headers +sudo apt-get upgrade -y +# sudo yum upgrade -y # red-hat version + +# Install wget and unzip +sudo apt-get install wget unzip -y +# sudo yum install wget unzip -y # red-hat version + +# Download and install Terraform (used 1.5.7 because latest version) +wget https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linux_amd64.zip + +# Unzip the downloaded file +unzip terraform_1.5.7_linux_amd64.zip + +# Move the executable to a directory in your path +sudo mv terraform /usr/local/bin/ + +# log out Terraform version (assuming /usr/local/bin/ is in your PATH) +terraform version + +# Assuming you have already downloaded and installed Google Cloud SDK authenticate with a service account +gcloud auth activate-service-account --key-file=~/terrform-service-account.json + +# ensure gcloud.sh is executable +chmod +x gcloud.sh + +# run gcloud.sh +./gcloud.sh + +# run terraform +terraform init + +terraform plan --out=plan.txt + +terraform apply -auto-approve + + + From 3bffe76714494376a6628be1bf779e9cd05ee0d5 Mon Sep 17 00:00:00 2001 From: Thamu Mnyulwa <43708808+ThamuMnyulwa@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:35:04 +0200 Subject: [PATCH 3/7] Added a provider block for running using a service account rather than running as the root user. --- chap01/cloudshell/main.tf | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/chap01/cloudshell/main.tf b/chap01/cloudshell/main.tf index 07f0a03..61b06c1 100644 --- a/chap01/cloudshell/main.tf +++ b/chap01/cloudshell/main.tf @@ -1,3 +1,11 @@ +# main.tf + +provider "google" { + credentials = file("terraform-service-key.json") + project = "using-terraf-157-8db7dc35" + region = "us-central1" +} + resource "google_compute_instance" "this" { name = "cloudshell" machine_type = "e2-small" @@ -12,4 +20,4 @@ resource "google_compute_instance" "this" { network_interface { network = "default" } -} +} \ No newline at end of file From c2e6566a82fdba41cd7b6e8255211f665fe5c38f Mon Sep 17 00:00:00 2001 From: Thamu Mnyulwa <43708808+ThamuMnyulwa@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:41:42 +0200 Subject: [PATCH 4/7] Corrected aunthetication with service account (not using enviroment variable yet) --- chap01/cloudshell/run.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/chap01/cloudshell/run.sh b/chap01/cloudshell/run.sh index 6695557..e9d0a64 100644 --- a/chap01/cloudshell/run.sh +++ b/chap01/cloudshell/run.sh @@ -25,8 +25,8 @@ sudo mv terraform /usr/local/bin/ # log out Terraform version (assuming /usr/local/bin/ is in your PATH) terraform version -# Assuming you have already downloaded and installed Google Cloud SDK authenticate with a service account -gcloud auth activate-service-account --key-file=~/terrform-service-account.json +# Assuming you have already downloaded and installed Google Cloud SDK authenticate with a service account (later we use an environment variable) +gcloud auth activate-service-account --key-file=~/terraform-service-account.json # ensure gcloud.sh is executable chmod +x gcloud.sh @@ -35,11 +35,14 @@ chmod +x gcloud.sh ./gcloud.sh # run terraform + terraform init terraform plan --out=plan.txt terraform apply -auto-approve +terrform destroy + From 44dacaa21c68b0191b3768bde629ccf58326e102 Mon Sep 17 00:00:00 2001 From: Thamu Mnyulwa <43708808+ThamuMnyulwa@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:49:02 +0200 Subject: [PATCH 5/7] Added logging of version of Google Cloud CLI --- chap01/cloudshell/run.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chap01/cloudshell/run.sh b/chap01/cloudshell/run.sh index e9d0a64..5684fc5 100644 --- a/chap01/cloudshell/run.sh +++ b/chap01/cloudshell/run.sh @@ -22,8 +22,11 @@ unzip terraform_1.5.7_linux_amd64.zip # Move the executable to a directory in your path sudo mv terraform /usr/local/bin/ +# log version of Google Cloud SDK (assuming /usr/local/bin/ is in your PATH) +gcloud --version + # log out Terraform version (assuming /usr/local/bin/ is in your PATH) -terraform version +terraform --version # Assuming you have already downloaded and installed Google Cloud SDK authenticate with a service account (later we use an environment variable) gcloud auth activate-service-account --key-file=~/terraform-service-account.json From 1f784bcb8712f279fd7e8eb55d78f9a5f1862ce4 Mon Sep 17 00:00:00 2001 From: Thamu Mnyulwa <43708808+ThamuMnyulwa@users.noreply.github.com> Date: Wed, 4 Oct 2023 23:33:05 +0200 Subject: [PATCH 6/7] Added a new run.sh with the latest version of terraform, 1.6.0 with testing. --- chap01/environment-variable/run.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 chap01/environment-variable/run.sh diff --git a/chap01/environment-variable/run.sh b/chap01/environment-variable/run.sh new file mode 100644 index 0000000..e69de29 From 5d4c8ce3ea71c7d0bffcca8c833dea21744c7c52 Mon Sep 17 00:00:00 2001 From: Thamu Mnyulwa <43708808+ThamuMnyulwa@users.noreply.github.com> Date: Wed, 4 Oct 2023 23:36:05 +0200 Subject: [PATCH 7/7] Added a new run.sh with the latest version of terraform, 1.6.0 with testing. --- chap01/environment-variable/run.sh | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/chap01/environment-variable/run.sh b/chap01/environment-variable/run.sh index e69de29..efd558a 100644 --- a/chap01/environment-variable/run.sh +++ b/chap01/environment-variable/run.sh @@ -0,0 +1,51 @@ + +#!/bin/bash + +# Update the package list +sudo apt-get update +# sudo yum check-update # red-hat version + +# Upgrade all installed packages including kernel and kernel headers +sudo apt-get upgrade -y +# sudo yum upgrade -y # red-hat version + +# Install wget and unzip +sudo apt-get install wget unzip -y +# sudo yum install wget unzip -y # red-hat version + +# Download and install Terraform (used 1.6.0 because latest version) +wget https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip + +# Unzip the downloaded file +unzip terraform_1.6.0_linux_amd64.zip + +# Move the executable to a directory in your path +sudo mv terraform /usr/local/bin/ + +# log version of Google Cloud SDK (assuming /usr/local/bin/ is in your PATH) +gcloud --version + +# log out Terraform version (assuming /usr/local/bin/ is in your PATH) +terraform --version + +# Assuming you have already downloaded and installed Google Cloud SDK authenticate with a service account (later we use an environment variable) +gcloud auth activate-service-account --key-file=~/terraform-service-account.json + +# ensure gcloud.sh is executable +chmod +x gcloud.sh + +# run gcloud.sh +./gcloud.sh + +# run terraform + +terraform init + +terraform plan --out=plan.txt + +terraform apply -auto-approve + +terrform destroy + + +