Pages

Friday, June 4, 2021

Google Cloud : TerraForm

Google Terraform Provider initialization

file provider.tf

    provider "google" {}

In shell, run:

terraform init

 

NOW YOU CAN WORK ON TERRAFORM LOGICAL SPECIFICATIONS. 

SEE EXAMPLE BELOW IN THE BOTTOM OF PAGE.

Other commands ...

To rewrite the Terraform configuration files to a canonical format and style, run the following command:

terraform fmt

To initialize Terraform, run the following command

terraform init

 To create an execution plan, run the following command:

terraform plan

To apply the desired changes, run the following command:

terraform apply 

RESOURCE TEMPLATE

 TerraForm Google RESOURCE TEMPLATE

# Create the mynetwork network
resource [RESOURCE_TYPE] "mynetwork" {
name = [RESOURCE_NAME]
#RESOURCE properties go here
}

DOCUMENTATION

Google Cloud TerraForm provider documentation

https://registry.terraform.io/providers/hashicorp/google/latest/docs

Terraform Module variables

https://learn.hashicorp.com/tutorials/terraform/aws-variables

 

EXAMPLE OF GOOGLE INFRASTRUCTURE AS A CODE

mynetwork.tf

# Create the mynetwork network
resource "google_compute_network" "mynetwork" {
name = "mynetwork"
#RESOURCE properties go here
auto_create_subnetworks = "true"
}

# Add a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on mynetwork
resource "google_compute_firewall" "mynetwork-allow-http-ssh-rdp-icmp" {
name = "mynetwork-allow-http-ssh-rdp-icmp"
#RESOURCE properties go here
network = google_compute_network.mynetwork.self_link
allow {
    protocol = "tcp"
    ports    = ["22", "80", "3389"]
    }
allow {
    protocol = "icmp"
    }
}

# Create the mynet-us-vm instance
module "mynet-us-vm" {
  source           = "./instance"
  instance_name    = "mynet-us-vm"
  instance_zone    = "us-central1-a"
  instance_network = google_compute_network.mynetwork.self_link
}

# Create the mynet-eu-vm" instance
module "mynet-eu-vm" {
  source           = "./instance"
  instance_name    = "mynet-eu-vm"
  instance_zone    = "europe-west1-d"
  instance_network = google_compute_network.mynetwork.self_link
}

instance/main.tf

variable "instance_name" {}
variable "instance_zone" {}
variable "instance_type" {
  default = "n1-standard-1"
  }
variable "instance_network" {}

resource "google_compute_instance" "vm_instance" {
  name         = "${var.instance_name}"
  zone         = "${var.instance_zone}"
  machine_type = "${var.instance_type}"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
      }
  }
  network_interface {
    network = "${var.instance_network}"
    access_config {
      # Allocate a one-to-one NAT IP to the instance
    }
  }
}

 

No comments:

Post a Comment