Terraform - First VM at Google cloud
Download Terraform:
Terraforms
Download GCP Cli SDK: Google Cloud SDK
Download Code Editor: Visual Studio
Download GCP Cli SDK: Google Cloud SDK
Download Code Editor: Visual Studio
Pre requirements
FIrst you will need to setting Up GCP account, create a API credentials from gcloud console (APIs & Services / Credentials). After that you are ready do configure your terraform script to Plan, Apply and Destroy an GCP Virtual Machine.
Plan, Apply and Destroy GCP Virtual Machine using Terraform
This example ilustrates commands used by Ubuntu. After installing terraform and gcp cloud SDK you will need:
- GCP Service account key (Json file)
- GCP Project ID
- Define GCP Region & Zones and set your default Region/zone for your project.
Save your secret key (secrets.json) locally, this will be set as a variable in our script. In this scenario I used:
/home/andrehps/terraform/credentials
Folder structure is like this:
/home/andrehps/terraformFile location: ~/terraform/first_instance:
~/terraform/credentials
~/terraform/first_instance
- main.tf
- provider.tf
Terraform scripts
provider.tfvariable "path" {default = "/home/andrehps/terraform/credentials"} provider "google" { project = "celfocus-vfpt-poc-dwh" region = "europe-west1-b" credentials = "${file("${var.path}/secrets-VFPTPOC.json")}" }main.tf
resource "google_compute_instance" "default" { name = "firstvm" machine_type = "n1-standard-1" zone = "europe-west1-b" boot_disk { initialize_params { image = "ubuntu-os-cloud/ubuntu-1604-lts" } } network_interface { network = "default" } service_account{ scopes = ["userinfo-email","compute-ro","storage-ro"] } }
To execute terraform commands from anyware, you need to set up your path variable:
export PATH:$PATH:/opt
Terraform commands
terraform init first_instance/ terraform plan first_instance/ terraform apply first_instance/ terraform destroy first_instance/
Comments
Post a Comment