Friday, July 5, 2019

Create Google Cloud Engine instance using Terraform

Summary

HashiCorp Terraform open-source tool that facilitates you to provision and manage infrastructure as a code. Terraform tool is to make optimal use of Google Cloud Platform (GCP) services and features. This blog demonstrates you how to use Terraform to create a VM Google Cloud Platform with a public IP address.

Take away:
    How to create a VM on GCP using Terraform
 

 

Terraform Flow graph for GCP Instance creation

 

1. Create main.tf on project folder

# Configure the Google Cloud provider

provider "google" {

  credentials = "${file("CREDENTIALS_FILE.json")}"
  project     = "${var.project}"
  region      = "${var.region}"
}

# Terraform plugin for creating random ids

resource "random_id" "instance_id" {
  byte_length = 8
}

# A single Google Cloud Engine instance

resource "google_compute_instance" "default" {
  name         = "terraform-test-instance-${random_id.instance_id.hex}"
  machine_type = "${var.machine_type}"
  zone         = "${var.zone}"

  boot_disk {

    initialize_params {
      image = "${var.image}"
    }
  }

// Update yum on all new instances for later steps

 metadata_startup_script = "sudo yum update -y"

 network_interface {

    network = "default"

    access_config {

      // Include this section to give the VM an external ip address
   }
 }
}

2. Create variables.tf on project folder

variable "project" {

  description = " Google Cloud Platform - Project "
  default = "luminous-lambda-244013"
}

variable "region" {

  description = " Google Cloud Platform - Region "
  default = "australia-southeast1"
}

variable "machine_type" {

  description = " Machine type "
  default = "n1-standard-1"
}

variable "zone" {

  description = " Zone "
  default = "australia-southeast1-a"
}

variable "image" {

  description = " Image "
  default = "centos-7"
}


 




3. Terraform init


# terraform init

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.google: version = "~> 2.9"
* provider.random: version = "~> 2.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.

If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.




4. Terraform plan

# terraform plan

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + google_compute_instance.default
      id:                                                  <computed>
      boot_disk.#:                                         "1"
      boot_disk.0.auto_delete:                             "true"
      boot_disk.0.device_name:                             <computed>
      boot_disk.0.disk_encryption_key_sha256:              <computed>
      boot_disk.0.initialize_params.#:                     "1"
      boot_disk.0.initialize_params.0.image:               "centos-7"
      boot_disk.0.initialize_params.0.size:                <computed>
      boot_disk.0.initialize_params.0.type:                <computed>
      can_ip_forward:                                      "false"
      cpu_platform:                                        <computed>
      deletion_protection:                                 "false"
      guest_accelerator.#:                                 <computed>
      instance_id:                                         <computed>
      label_fingerprint:                                   <computed>
      machine_type:                                        "n1-standard-1"
      metadata_fingerprint:                                <computed>
      metadata_startup_script:                             "sudo yum update -y"
      name:                                                "terraform-test-instance-${random_id.instance_id.hex}"
      network_interface.#:                                 "1"
      network_interface.0.access_config.#:                 "1"
      network_interface.0.access_config.0.assigned_nat_ip: <computed>
      network_interface.0.access_config.0.nat_ip:          <computed>
      network_interface.0.access_config.0.network_tier:    <computed>
      network_interface.0.address:                         <computed>
      network_interface.0.name:                            <computed>
      network_interface.0.network:                         "default"
      network_interface.0.network_ip:                      <computed>
      network_interface.0.subnetwork_project:              <computed>
      project:                                             <computed>
      scheduling.#:                                        <computed>
      self_link:                                           <computed>
      tags_fingerprint:                                    <computed>
      zone:                                                "australia-southeast1-a"

  + random_id.instance_id
      id:                                                  <computed>
      b64:                                                 <computed>
      b64_std:                                             <computed>
      b64_url:                                             <computed>
      byte_length:                                         "8"
      dec:                                                 <computed>
      hex:                                                 <computed>


Plan: 2 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.




5. Terraform apply


# terraform apply

random_id.instance_id: Refreshing state... (ID: UXkS5ycySJM)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + google_compute_instance.default

      id:                                                  <computed>
      boot_disk.#:                                         "1"
      boot_disk.0.auto_delete:                             "true"
      boot_disk.0.device_name:                             <computed>
      boot_disk.0.disk_encryption_key_sha256:              <computed>
      boot_disk.0.initialize_params.#:                     "1"
      boot_disk.0.initialize_params.0.image:               "centos-7"
      boot_disk.0.initialize_params.0.size:                <computed>
      boot_disk.0.initialize_params.0.type:                <computed>
      can_ip_forward:                                      "false"
      cpu_platform:                                        <computed>
      deletion_protection:                                 "false"
      guest_accelerator.#:                                 <computed>
      instance_id:                                         <computed>
      label_fingerprint:                                   <computed>
      machine_type:                                        "n1-standard-1"
      metadata_fingerprint:                                <computed>
      metadata_startup_script:                             "sudo yum update -y"
      name:                                                "terraform-test-instance-517912e727324893"
      network_interface.#:                                 "1"
      network_interface.0.access_config.#:                 "1"
      network_interface.0.access_config.0.assigned_nat_ip: <computed>
      network_interface.0.access_config.0.nat_ip:          <computed>
      network_interface.0.access_config.0.network_tier:    <computed>
      network_interface.0.address:                         <computed>
      network_interface.0.name:                            <computed>
      network_interface.0.network:                         "default"
      network_interface.0.network_ip:                      <computed>
      network_interface.0.subnetwork_project:              <computed>
      project:                                             <computed>
      scheduling.#:                                        <computed>
      self_link:                                           <computed>
      tags_fingerprint:                                    <computed>
      zone:                                                "australia-southeast1-a"


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

google_compute_instance.default: Creating...
  boot_disk.#:                                         "" => "1"
  boot_disk.0.auto_delete:                             "" => "true"
  boot_disk.0.device_name:                             "" => "<computed>"
  boot_disk.0.disk_encryption_key_sha256:              "" => "<computed>"
  boot_disk.0.initialize_params.#:                     "" => "1"
  boot_disk.0.initialize_params.0.image:               "" => "centos-7"
  boot_disk.0.initialize_params.0.size:                "" => "<computed>"
  boot_disk.0.initialize_params.0.type:                "" => "<computed>"
  can_ip_forward:                                      "" => "false"
  cpu_platform:                                        "" => "<computed>"
  deletion_protection:                                 "" => "false"
  guest_accelerator.#:                                 "" => "<computed>"
  instance_id:                                         "" => "<computed>"
  label_fingerprint:                                   "" => "<computed>"
  machine_type:                                        "" => "n1-standard-1"
  metadata_fingerprint:                                "" => "<computed>"
  metadata_startup_script:                             "" => "sudo yum update -y"
  name:                                                "" => "terraform-test-instance-517912e727324893"
  network_interface.#:                                 "" => "1"
  network_interface.0.access_config.#:                 "" => "1"
  network_interface.0.access_config.0.assigned_nat_ip: "" => "<computed>"
  network_interface.0.access_config.0.nat_ip:          "" => "<computed>"
  network_interface.0.access_config.0.network_tier:    "" => "<computed>"
  network_interface.0.address:                         "" => "<computed>"
  network_interface.0.name:                            "" => "<computed>"
  network_interface.0.network:                         "" => "default"
  network_interface.0.network_ip:                      "" => "<computed>"
  network_interface.0.subnetwork_project:              "" => "<computed>"
  project:                                             "" => "<computed>"
  scheduling.#:                                        "" => "<computed>"
  self_link:                                           "" => "<computed>"
  tags_fingerprint:                                    "" => "<computed>"
  zone:                                                "" => "australia-southeast1-a"
google_compute_instance.default: Still creating... (10s elapsed)
google_compute_instance.default: Creation complete after 14s (ID: terraform-test-instance-517912e727324893)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.




6. Verify instance creation status on Google Cloud Platform console