User Tools

Site Tools


terraform:import

to import a resource on tfstate in case of tfstate lost follow these steps:

  1. Ordered List Itemfind the resource to import (in this example a cluster GKE)
    in this case we need to find the cluster name, and cluster location [ <cluster-name>,projects/<project-id>/locations/<location>/clusters/<cluster-name> ] the other requested information may change with the case
  2. define the resource in a tf file, for example import.tf with the contents:
    resource "google_container_cluster" "resource-name" {
      name               = "cluster-name"
      location           = "location"
      initial_node_count = 1
      network            = "projects/<project-id within the network>/global/networks/<network name>"
    
      master_authorized_networks_config {
        dynamic "cidr_blocks" {
          for_each = var.master_authorized_networks_cidr_blocks
          content {
            cidr_block   = cidr_blocks.value.cidr_block
            display_name = cidr_blocks.value.display_name
          }
        }
      }
      dns_config { # forces replacement
        cluster_dns        = "CLOUD_DNS" #it depends on original creation
        cluster_dns_domain = "domain.name" #it depends on original creation
        cluster_dns_scope  = "CLUSTER_SCOPE" #it depends on original creation
      }
      timeouts { #it depends on original creation
        create = null
        delete = null
        read   = null
        update = null
      }
    }
  3. importing resource issuing the command:
    terraform import google_container_cluster.<resource-name> projects/<project-id>/locations/<location>/clusters/<cluster-name> 
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
terraform/import.txt · Last modified: 2024/11/10 08:01 by 127.0.0.1