terraform import block - ghdrako/doc_snipets GitHub Wiki

The Terraform import block introduced in Terraform v1.5.0 provides a declarative approach for importing existing infrastructure resources into a Terraform state file.

The syntax for an import block in Terraform is as follows:

import {
  to = <resource_address>
  id = <resource_identifier>
}
resource "aws_s3_bucket" "example" {
  bucket = "my-existing-bucket"
}

import {
  to = aws_s3_bucket.example
  id = "my-existing-bucket"
}

After running terraform apply and successfully importing the resource, it is a best practice to remove the import block.

resource "aws_instance" "example" {
  ami           = "ami-0abcdef1234567890"  # Replace with the actual AMI ID
  instance_type = "t2.micro"
}

import {
  to = aws_instance.example
  id = "i-1234567890abcdef0"
}