Ask Your Question
3

How can two GCP instances with static IPs be created using Terraform?

asked 2021-08-13 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-12 19:00:00 +0000

woof gravatar image

To create two GCP instances with static IPs using Terraform, follow these steps:

  1. Install and configure Terraform on your local machine.

  2. Create a new Terraform file, name it something like gcp-instances.tf, and include the following code:

provider "google" {
  credentials = file("account.json")
  project     = "<YOUR_GCP_PROJECT>"
  region      = "<YOUR_GCP_REGION>"
}

resource "google_compute_address" "instance1_ip" {
  name = "instance1-static-ip"
}

resource "google_compute_instance" "instance1" {
  name_prefix  = "instance1-"
  machine_type = "n1-standard-1"
  zone         = "<YOUR_GCP_ZONE>"
  boot_disk {
    initialize_params {
      image = "<YOUR_GCP_IMAGE>"
    }
  }

  network_interface {
    network = "default"
    access_config {
      nat_ip = google_compute_address.instance1_ip.address
    }
  }
}

resource "google_compute_address" "instance2_ip" {
  name = "instance2-static-ip"
}

resource "google_compute_instance" "instance2" {
  name_prefix  = "instance2-"
  machine_type = "n1-standard-1"
  zone         = "<YOUR_GCP_ZONE>"
  boot_disk {
    initialize_params {
      image = "<YOUR_GCP_IMAGE>"
    }
  }

  network_interface {
    network = "default"
    access_config {
      nat_ip = google_compute_address.instance2_ip.address
    }
  }
}
  1. Fill in the necessary values in the code, such as your GCP project, region, zone, and image.

  2. Run terraform init and terraform apply to create the instances and static IPs.

  3. Once the Terraform process is complete, you can verify that the instances were created and have static IPs assigned by navigating to the Google Cloud Console and looking for the instances under the Compute Engine section.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-13 11:00:00 +0000

Seen: 19 times

Last updated: May 12 '22