Import Terraform Configuration

google storage bucket & Virtual machine


The terraform import command is used to bring existing infrastructure, which was not initially managed by Terraform, under Terraform management. This is particularly useful in scenarios where you have existing resources deployed manually or through other means, and you want to start managing them with Terraform without recreating them.

Terraform import can be useful in brownfield environments, resource discovery and resource created through third-party tools.

In this example we are going to import two GCP resources - virtual instance and cloud storage bucket into terraform configuration.

Here is the basic workflow using terraform import

1. identify the resource you want to manage in terraform.

2. Write a basic terraform configuration describing the desired state of the resource.

3. Use terraform import <resource configuration> <actual resource to be imported>

4. Run terraform plan to match the state file and configuration

resource "google_compute_instance" "imp_instance" {
  name = "existing_vm_name"
  machine_type = "existing_vm_machine_type"
  # configuration for your instance.
}        

above resource along with google provider information to be created in tf file. You need to run terraform init command to download required plugins. Next use below command to import vm instance

terraform import google_compute_instance.imp_instance <existing_vm_name>        

After executing above import command, terraform will import the resource from your GCP account and write the configuration in tfstate file.

Note that after running terraform import, you need to update your Terraform configuration to reflect the desired state accurately. Here is the sample snippet.

resource "google_compute_instance" "imp_instance" {
  name = "existing_vm_name"
  machine_type = "existing_vm_machine_type"
# for all these below attributes, refer tfstate file which got updated after import command and upate below.
# note for a vm instance in GCP, all these tags are mandatory and you need to populate the values from state file.
  tags = ["refer-tfstatefile-to-update-value"]
  network_interface {
    network = "default"
    access_config {} 
  }
  boot_disk {
    initialize_params {
      image = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-2204-jammy-v20231030"
      size = 20
      type = "pd-balanced"
    }
  }
}        

After update above configuration file, you can run terraform plan to validate consistentcy betwen your tfstate file and tf file.

Secondly, below refers to importing a cloud storage bucket into terraform configuration.

resource "google_storage_bucket" "website" {
  name = var.custom_bucket_name
  location = var.bucket_location
}

c:\> terraform import google_storage_bucket.website <existing_bucket_name>        

Keep in mind that while terraform import is a powerful tool, it's essential to understand the intricacies of the resource you're importing and ensure that your Terraform configuration accurately represents the existing state.

Additionally, certain resource attributes may not be manageable through Terraform, so manual adjustments might be necessary in such cases.

要查看或添加评论,请登录

Srinivasan (Srini) Viswanathan的更多文章

社区洞察

其他会员也浏览了