DEV Community

Terraform Import & Demo

Use the import block to import existing infrastructure resources into Terraform, bringing them under Terraform's management.

Once imported, Terraform tracks the resource in your state file. You can then manage the imported resource like any other, updated /destroy it as part your requirement.

Terraform older version < 1.5 : It will import state file information but the configuration has to be written.

Example1 :




provider "aws" {
  region     = "eu-west-1"
}

resource "aws_instance" "srinivm" {
 ami           = "unknown"
 instance_type = "unknown"
}



Enter fullscreen mode Exit fullscreen mode

Run the terraform commands :



terraform init



Enter fullscreen mode Exit fullscreen mode

Terraform import :Think of it as if the cloud resource (EC2 instance) and its corresponding configuration were available in our files. All that’s left to do is to map the two into our state file. We do that by running the import command as follows.



terraform import aws_instance.srinivm <Instance ID>



Enter fullscreen mode Exit fullscreen mode

Successfull import looks like



aws_instance.srinivm: Importing from ID "i-0ad9e2e3d0f1a35a9"...
aws_instance.srinivm: Import prepared!
  Prepared aws_instance for import
aws_instance.srinivm: Refreshing state... [id=i-0ad9e2e3d0f1a35a9]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.



Enter fullscreen mode Exit fullscreen mode

While we do not expect to make backwards-incompatible changes to syntax, the -generate-config-out flag and how Terraform processes imports during the plan stage and generates configuration may change in future releases.

Example 2:

Step1: Create AWS Security group through AWS Console with one ingress rule

Image description

Step2: Make sure you are having latest terraform version downloaded
then run the command
terraform version

version should be greater than 1.5

Step3: create a file import.tf then copy the below mentioned code



provider "aws" {
  region     = "eu-west-1"
}


import {
  to = aws_security_group.mysg
  id = "sg-0edc915507cfcd57a" #this is the security group which i have created from step 1
}



Enter fullscreen mode Exit fullscreen mode

Step4 : Run the terraform command

terraform init

terraform plan -generate-config-out sg.tf

sg.tf configuration will be created after the above command ran with the below message

│ Warning: Config generation is experimental

│ Generating configuration during import is currently experimental, and the generated configuration format may

│ change in future versions.

Step 5: sg.tf created with the below mentioned code



# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "sg-0edc915507cfcd57a"
resource "aws_security_group" "mysg" {
  description = "Allow SSH and RDP"
  egress = [{
    cidr_blocks      = ["0.0.0.0/0"]
    description      = ""
    from_port        = 0
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    protocol         = "-1"
    security_groups  = []
    self             = false
    to_port          = 0
  }]
  ingress = [{
    cidr_blocks      = ["0.0.0.0/0"]
    description      = ""
    from_port        = -1
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    protocol         = "icmp"
    security_groups  = []
    self             = false
    to_port          = -1
    }]
  name                   = "sgej"
  name_prefix            = null
  revoke_rules_on_delete = null
  vpc_id = "vpc-***"
}



Enter fullscreen mode Exit fullscreen mode

Step6 : Run the below command for terraform apply

> terraform apply --auto-approve

Below response is generated :

aws_security_group.mysg: Importing... [id=sg-0edc915507cfcd57a]
aws_security_group.mysg: Import complete [id=sg-0edc915507cfcd57a]

Apply complete! Resources: 1 imported, 0 added, 0 changed, 0 destroyed.

References: https://developer.hashicorp.com/terraform/language/import

Conclusion : Discussed about terraform import and created aws security group manually through aws console and imported the configuration using terraform import command.

💬 If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it 😀 and follow me in dev.to , linkedin and buy me a coffee

Top comments (0)