HOWTO
- GCP https://registry.terraform.io/providers/hashicorp/google/latest/docs
- Network https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network
- Firewall https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall
- GCE https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance
- Terraform input variables https://developer.hashicorp.com/terraform/tutorials/aws-get-started/aws-variables
See also
GitHub
Initialize Terraform
dave@dave:/git/devops-terraform/gcp$ terraform init
Initializing the backend...
Initializing modules...
- mynet-eu-vm in instance
- mynet-us-vm in instance
Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v6.6.0...
- Installed hashicorp/google v6.6.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
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.
Provider
provider "google" {}
Variables
variable "instance_name" {}
variable "instance_zone" {}
variable "instance_type" {
default = "e2-micro"
}
variable "instance_network" {}
Network
$ cat 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"
}
source_ranges = ["0.0.0.0/0"]
}
# 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
resource "google_compute_instance" "vm_instance" {
name = "${var.instance_name}"
# RESOURCE properties go here
zone = "${var.instance_zone}"
machine_type = "${var.instance_type}"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "${var.instance_network}"
access_config {
# Allocate a one-to-one NAT IP to the instance
}
}
}