Terraform Import

Reading Time: 4 minutes

In this blog we are going to see about the terraform import . For that purpose needs to have idea about the terraform ,So, as if some people don’t have idea about terraform plz refer to this blog.
Now if got idea about the terraform, Let’s see

So, main point we need to discuss before looking at the terraform import is terraform tfsate. Terraform record information about what infra is cretaed in a terraform state file. So lets condider that whenever we are going to apply the terraform cmd some resources are going to create on to your cloud. and will delete them as wee, so how terraform will know which resources he has to delete and whick kind of resources are exactly managed by terrafoem. So all this kind of information is managed by terraform tf state.

Let’s copy the below code if you wanna try:

resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "main"
  }
}

resource "aws_instance" "app_server" {
  ami           = "ami-000377b9b6bb5f75d"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
} 

So Here you can see i have two resource configation with me in main.tf file.
Lets consider there are only default resources are present on cloud for now.

So after terraform init and then apply the above script will create the vpc and ec2 . And the terraform.tfstate will get created.

Why Import:

So, why we need terraform import here. Let’s consider you need to create some resources through your terraform script, but all those resources are alrady present on the cloud with the same configuration. And after applying you will recieve “resource already exist” at that time what you will.
As you know resource can be created through differant way
1. AWS UI/Console
2. AWS Cli
3. Terraform
So rather than than creating the duplicate resource/to avoid the above error. You will use terraform import. So in simple word we can define it as
“Getting the pre-existing cloud resources under the Terraform management is facilitated by Terraform import. import is a Terraform CLI command which is used to read real-world infrastructure and update the state, so that future updates to the same set of infrastructure can be applied via IaC”.
Now the question arises how exactly we can import resources:

Simple Import:

Now we have resources on our cloud as below:

And I have my terraform script as follows, and need to create the below resources:

provider "aws" {
  region  = "us-east-1"
  profile = "sakshi-aws" //update your user-profile for aws keys
}

resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "main"
  }
}

resource "aws_instance" "app_server" {
  ami           = "ami-000377b9b6bb5f75d"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
} 

So, now if a gonna perform the cmd’s like terraform plan and terraform apply sequentially. It show me that two resource will get created and actually two resource will create. But as we can see in the above screenshot that the one resource is already present i.e aws_vpc. So rather than creating a new one will import the above vpc before applying. And create the tfstate with above resource details.

terraform import aws_vpc.main vpc-062ff8316887c2518

Once the import got successfull. You can see the terraform.tfstate file got generated with the below details

Module Import:

In the same way if you want to import a resource from a module. Lets consider the below module.

module "iam" {
  source = "../modules/iam/medium_demo"
  user_name = "bill"
  group_name = "demo"
  tags = { "foo" = "bar" }
 }

Which contains the below resources

resource "aws_iam_user" "user" {
  name = var.user_name
  tags = var.tags
}
 resource "aws_iam_user_login_profile" "profile" {
  user    = aws_iam_user.user.name
  pgp_key = "keybase:<keybase_name>" # outside our scope
 }

In the above case you need to use the below cmd’s

$ terraform import module.iam.aws_iam_user.user bill
…
$ terraform import module.iam.aws_iam_user_login_profile.profile bil

Note: Whenever you are trying to import a resource the terraform configuration has to present for that . Otherwise you will get an error.

Written by 

Sakshi Gawande is a software consultant at "KNOLDUS" having more than 2 years of experience. She's working as a DevOps engineer. She always wants to explore new things and solve problems herself. on personal, she likes dancing, painting, and traveling.

Leave a Reply