top of page

Create GCP Resources with Terraform GCP Provider

Updated: Aug 26, 2023

Step 1: Set up your GCP account


First, you'll need to create a GCP account and project for GCP resource provisioning with Terraform. Once you have your account and project set up, you'll need to create a service account and download the service account key file. This key file will allow Terraform to authenticate and manage your GCP resources.


Step 2: Install Terraform


Next, you'll need to install Terraform on your local machine. You can download Terraform from the HashiCorp website and later use Terraform GCP provider for creating GCP compute resources, GCP IAM roles and permissions with Terraform, GCP Kubernetes Engine with Terraform etc



Step 3: Create a Terraform configuration file


Once you have Terraform installed, you can create a new Terraform configuration file (also known as a "manifest") that defines the infrastructure resources you want to create. In this example, we'll create a new Compute Engine instance.


Create a new file named main.tf and paste in the following code:


provider "google" {
  credentials = file("<PATH_TO_YOUR_SERVICE_ACCOUNT_KEY>")
  project     = "<YOUR_GCP_PROJECT_ID>"
  region      = "us-central1"
}

resource "google_compute_instance" "example_instance" {
  name         = "example-instance"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }

  }

  network_interface {
    network = "default"
  }

  tags = ["web", "dev"]
}

This code defines a google_compute_instance resource that specifies the in


stance's name, machine type, boot disk image, and network interface. Note that you'll need to replace <PATH_TO_YOUR_SERVICE_ACCOUNT_KEY> and <YOUR_GCP_PROJECT_ID> with the appropriate values for your GCP project.



Step 4: Initialize your Terraform project


Next, you'll need to initialize your Terraform project. Open a terminal window and navigate to the directory where you saved your main.tf file. Then, run the following command:

terraform init

This will initialize your Terraform project and download any necessary plugins and dependencies.

Step 5: Preview your changes


Once your project is initialized, you can preview the changes that Terraform will make to your infrastructure. Run the following command:

terraform plan

This will show you a summary of the changes that Terraform will make to your infrastructure resources. If everything looks good, you can proceed to the next step.


Step 6: Apply your changes


Finally, you can apply your changes to your infrastructure resources by running the following command:

terraform apply

This will create your new Compute Engine instance. Once the command finishes running, you should be able to see your new instance in the GCP Console.


Conclusion


Using Terraform with GCP can help you to automate and streamline your infrastructure management processes. By defining your infrastructure resources as code, you can easily version-control and collaborate on your infrastructure management, ensuring consistency across environments. With Terraform's support for GCP's many services and resources, you can use a single tool to manage your entire infrastructure, from Compute Engine instances to Cloud Storage buckets to Cloud Functions and more.



Comentários


bottom of page