Terraform Import: Import Blocks, Commands and ID Formats
Terraform import does 1 thing: it ties a resource that already exists to an address in your Terraform state. Bottom line Use an import block. It has been in Terraform since 1.5 (June 2023), it shows up in terraform plan , and it goes through a pull request like any other change. import { to = aws_s3_bucket . assets id = "my-company-assets" } resource "aws_s3_bucket" "assets" { bucket =…
Terraform import is a tool that connects an existing resource to a resource address within your Terraform state. This feature was introduced in version 1.5 (June 2023) and is part of the standard import process, similar to any other change made through a pull request. To use the import command, you need to specify the resource type, its ID, and provide an import block.
After running terraform plan, you should see a line indicating "Plan: 1 to import, 0 to add, 0 to change, 0 to destroy." Following this, you can apply the configuration using terraform apply. Importing a resource does not affect your cloud account, as Terraform records that it now manages the imported bucket. Depending on your situation, there are several methods to use Terraform import.
If you know the resource and can write its block, you can simply use an import block. If you have the ID but no configuration, you can generate the resource block using import -generate-config-out. For resources with the same type, you can use a single import block with for_each. If you are unsure about the existing resources in your account, you can use terraform query (Terraform 1.14 and later) to gather that information.
The import process involves writing the state entry for an existing object while still requiring a resource block to be defined. Importing a resource does not change anything in your cloud, but the first apply after the import may modify the cloud resources if your configuration differs from the real object. It is essential to have a resource block defined for any resource in state to prevent Terraform from destroying it. Importing a resource only changes the state entry and does not update the actual cloud resource.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.