packagemain.tech

packagemain.tech

Share this post

packagemain.tech
packagemain.tech
How to import an existing cluster into Terraform
User's avatar
Discover more from packagemain.tech
Welcome to packagemain.tech, your one-stop shop for mastering Backend, Cloud, Kubernetes, Microservices, APIs, and more. We'll provide you with hands-on, practical and real-world tutorials that you can use to build your software development skills.
Over 11,000 subscribers
Already have an account? Sign in

How to import an existing cluster into Terraform

Streamlining infrastructure management by bringing your existing resources under Terraform control

Julien Singler's avatar
Julien Singler
Dec 23, 2024
9

Share this post

packagemain.tech
packagemain.tech
How to import an existing cluster into Terraform
1
Share

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

  1. Install Terraform CLI

  2. Configure your cloud provider credentials

  3. 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.

Samuel Umoh's avatar
Adarsh's avatar
Nator Verinumbe's avatar
Emily Erans's avatar
Alex Pliutau's avatar
9 Likes∙
1 Restack
9

Share this post

packagemain.tech
packagemain.tech
How to import an existing cluster into Terraform
1
Share

Discussion about this post

User's avatar
Essential CLI/TUI Tools for Developers
An opinionated list of CLI/TUI applications for developer productivity.
Jan 6 • 
Alex Pliutau
64

Share this post

packagemain.tech
packagemain.tech
Essential CLI/TUI Tools for Developers
8
Performance Benchmarking: gRPC+Protobuf vs. HTTP+JSON
A fair benchmark with Go examples to compare Protocol Buffers over gRPC vs. JSON over HTTP/1 and HTTP/2.
Jun 17, 2024 • 
Alex Pliutau
46

Share this post

packagemain.tech
packagemain.tech
Performance Benchmarking: gRPC+Protobuf vs. HTTP+JSON
14
Mastering SOLID Principles with Go Examples
The SOLID principles are a set of design guidelines that help developers write more maintainable, scalable, and testable code.
Jul 9, 2024 • 
Alex Pliutau
23

Share this post

packagemain.tech
packagemain.tech
Mastering SOLID Principles with Go Examples
5

Ready for more?

© 2025 Aliaksandr Pliutau
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Create your profile

User's avatar

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.