Wednesday, September 22, 2021

GCP - create VM using Terraform

HOWTO


Install Terraform 

[dave@dave tmp]$ sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
Adding repo from: https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
[dave@dave tmp]$ sudo dnf -y install terraform
Hashicorp Stable - x86_64                                                                                                                         2.0 MB/s | 602 kB     00:00    
Dependencies resolved.
==================================================================================================================================================================================
 Package                                   Architecture                           Version                                         Repository                                 Size
==================================================================================================================================================================================
Installing:
 terraform                                 x86_64                                 1.0.7-1                                         hashicorp                                  25 M
Installing dependencies:
 openssl                                   x86_64                                 1:1.1.1l-2.fc34                                 updates                                   658 k

Transaction Summary
==================================================================================================================================================================================
Install  2 Packages

Total download size: 26 M
Installed size: 77 M
Downloading Packages:
(1/2): openssl-1.1.1l-2.fc34.x86_64.rpm                                                                                                           1.9 MB/s | 658 kB     00:00    
(2/2): terraform-1.0.7-1.x86_64.rpm                                                                                                               4.3 MB/s |  25 MB     00:05    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                             4.2 MB/s |  26 MB     00:06     
Hashicorp Stable - x86_64                                                                                                                          10 kB/s | 3.1 kB     00:00    
Importing GPG key 0xA3219F7B:
 Userid     : "HashiCorp Security (HashiCorp Package Signing) <security+packaging@hashicorp.com>"
 Fingerprint: E8A0 32E0 94D8 EB4E A189 D270 DA41 8C88 A321 9F7B
 From       : https://rpm.releases.hashicorp.com/gpg
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                          1/1 
  Installing       : openssl-1:1.1.1l-2.fc34.x86_64                                                                                                                           1/2 
  Installing       : terraform-1.0.7-1.x86_64                                                                                                                                 2/2 
  Running scriptlet: terraform-1.0.7-1.x86_64                                                                                                                                 2/2 
  Verifying        : openssl-1:1.1.1l-2.fc34.x86_64                                                                                                                           1/2 
  Verifying        : terraform-1.0.7-1.x86_64                                                                                                                                 2/2 

Installed:
  openssl-1:1.1.1l-2.fc34.x86_64                                                             terraform-1.0.7-1.x86_64                                                            

Complete!

Create GCP project and Account Key

 https://cloud.google.com/iam/docs/creating-managing-service-account-keys

 

Install gcloud

[dave@dave ~]$ sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el8-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM

[dave@dave ~]$ sudo dnf install google-cloud-sdk
Fedora 34 - x86_64 - Updates                                                                                            72 kB/s | 9.7 kB     00:00    
Fedora Modular 34 - x86_64 - Updates                                                                                    46 kB/s | 8.0 kB     00:00    
Google Cloud SDK                                                                                                        11 MB/s |  38 MB     00:03    
Dependencies resolved.
=======================================================================================================================================================
 Package                                 Architecture                  Version                           Repository                               Size
=======================================================================================================================================================
Installing:
 google-cloud-sdk                        x86_64                        358.0.0-1                         google-cloud-sdk                         82 M

Transaction Summary
=======================================================================================================================================================
Install  1 Package

gcloud init
You are logged in as: [ab123@gmail.com].

Pick cloud project to use: 
 [1] dave-terraform
 [2] genial-acronym-295114
 [3] Create a new project
Please enter numeric choice or text value (must exactly match list 
item):  1

Your current project has been set to: [dave-terraform].

Not setting default zone/region (this feature makes it easier to use
[gcloud compute] by setting an appropriate default value for the
--zone and --region flag).
See https://cloud.google.com/compute/docs/gcloud-compute section on how to set
default compute region and zone manually. If you would like [gcloud init] to be
able to do this for you the next time you run it, make sure the
Compute Engine API is enabled for your project on the
https://console.developers.google.com/apis page.

Created a default .boto configuration file at [/home/dave/.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use ab123@gmail.com by default
* Commands will reference project `dave-terraform` by default
Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
[dave@dave ~]$ gcloud version
Google Cloud SDK 358.0.0
alpha 2021.09.17
beta 2021.09.17
bq 2.0.71
core 2021.09.17
gsutil 4.68

show service account keys
gcloud iam service-accounts keys list \
    --iam-account=sa-name@project-id.iam.gserviceaccount.com