How to import an existing cluster into Terraform
Streamlining infrastructure management by bringing your existing resources under Terraform control
Introduction
As organizations transition to Infrastructure as Code (IaC), they often face the challenge of incorporating existing resources into their Terraform workflows.
This article will guide you through the process of importing an existing cluster into Terraform, enabling you to manage your infrastructure more effectively.
Understanding Terraform import
Terraform import allows you to bring existing infrastructure under Terraform management. This is crucial when you have resources that were created manually or through other tools, and you want to start managing them with Terraform.
Prerequisites
Install Terraform CLI
Configure your cloud provider credentials
Create a new directory for your Terraform configuration
Step-by-Step guide to importing an existing cluster
1. Identify your existing resources
Before importing, identify the resources associated with your cluster. This may include the cluster itself, node pools, security groups, and other related components.
2. Create Terraform configuration files
Create a main.tf
file in your project directory. Define the provider and empty resource blocks for the components you'll be importing.
provider "google" {
project = "your-project-id"
region = "us-central1"
}
resource "google_container_cluster" "existing_cluster" {
# Configuration will be filled in after import
}
3. Initialize Terraform
Run terraform init
in your project directory to initialize Terraform and download the necessary provider plugins.
4. Import the cluster
Use the terraform import
command to import your existing cluster. You'll need the cluster's ID or name:
terraform import google_container_cluster.existing_cluster projects/{your-project-id}/locations/{us-central1}/clusters/{your-cluster-name}
5. Generate configuration
After importing, use terraform show —no-color > imported_config.tf
to save the current state. Copy the relevant configuration from the generated file into your main.tf
file.
That usually means replacing the resource block from the generated file into the main.tf file.
Don’t forget to remove the generated file.
6. Verify and adjust configuration
Run Terraform plan
to ensure there are no unexpected changes. (there probably will be). Adjust your configuration as needed to match the desired state.
That’s where it can be a bit daunting, first thing is usually to remove any variable that is set to null.
Second is to remove anything that is usually managed by GCP itself and you wouldn’t want to set yourself if you were doing the configuration yourself.
A good link to open is your provider, let’s say you work with Google Cloud like in our example, then keep this page open somewhere have a quick access to the documentation of each resource block.
Now, begins the terraform plan feedback loop:
Whenever you see this error:
Error: Value for unconfigurable attribute
Which also should contains this last line:
Can't configure a value for {your_var_name}: its value will be decided automatically based on the result of applying this configuration.
It means the variable {your_var_name} shouldn’t be set in your terraform file, simply remove the line or the block and rerun terraform plan
With some patience and understanding of the error presented to you, you should quickly reach a state where terraform plan will show you no errors, but what it plans to do with your resource.
Goals
You should aim to see:
Plan: 0 to add, X to change, 0 to destroy
There also could be a case when Terraform wants to add something, and you don’t understand why. For instance, you imported a cluster and its node pool but still it says it wants to (re)create it.
It could be that inside your node pool resource, you defined a cluster variable referenced by id like this:
cluster = google_container_cluster.mycluster.id
and in fact, you should probably define it like that:
cluster = google_container_cluster.mycluster.name
That should change your “add” action into a “change” one.
7. Apply changes
Once you're satisfied with the configuration, run terraform apply
to bring your Terraform state in line with the actual infrastructure.
Best practices
Use remote backends for state storage to enable team collaboration
Implement state locking to prevent concurrent modifications
Regularly update and maintain your Terraform configurations
Conclusion
Importing existing resources into Terraform allows you to bring your infrastructure under version control and enables more efficient management.
While the process requires careful planning and execution, the benefits of having a unified IaC approach are substantial for long-term infrastructure management.