Terraform from Hashicorp allows users to import the resources created directly or through Terraform modules that are already present in the cloud like AWS, GCP, Azure, etc.
In this article, we will learn how to import a resource that is part of Terraform module like Route53 records, role policy attachment, security group rules, etc.
Table of Contents
Importing a resource that is part of a module is the same as importing any other resources using Terraform.
Terraform provides us with an import command that can be used to import existing resources.
Before importing any resources, please make sure you already have their configuration files ready.
Terraform import command does not create configuration files and will give error if there is no configuration file.
Terraform Import Command
Before diving into the examples of importing Terraform module, first, let’s check the syntax or the usage of the Terraform import command.
terraform import [options] ADDRESS ID
terraform: The Terraform command-line interface (CLI).
import: The sub-command used to import an existing resource into Terraform.
[options]: Optional command-line options that can be used to configure the behavior of the import command.
ADDRESS: The address of the Terraform resource in the Terraform configuration. This is typically in the format _., where is the name of the cloud provider, is the type of the resource (e.g., aws_instance for an AWS EC2 instance), and is a user-defined name for the resource.
ID: The unique identifier of the existing resource in the cloud provider that you want to import into Terraform. This is typically a resource-specific identifier, such as an ARN, instance ID, or resource name, depending on the cloud provider and resource type.
2 Reasons for Importing the Resources using Terraform
There are 2 obvious reasons why you will need to import existing resources and bring them under Terraform control.
1. You already have the Terraform configuration file locally and also the resource is already present in the Cloud, but the resource is not in control of Terraform and it is complaining to “create the resource” during the “terraform plan” command.
2. You don’t have Terraform configuration locally, but you have the resources in the cloud and now Terraform complains about “destroying the resource” while running the “terraform plan” command.
Terraform Module Import Examples
Now, let’s see some examples where we will import resources that are part of the module.
Example 1: Importing Autoscaling Policy
terraform import 'module.ec2-ASG.aws_autoscaling_policy.policy_simple' asg-name/policy-name
Here “ec2-ASG is the name of the module” followed by the type of the resource and then the index.
You don’t have to remember this, just run the “terraform plan” and you will get the exact name that you can use in the “terraform import” command.
$ terraform import module.ec2-ASG.aws_autoscaling_policy.policy_simple asg-name/policy-name Acquiring state lock. This may take a few moments... Resource instance <module.ec2-ASG.aws_autoscaling_policy.policy_simple> imported successfully!
Example 2: Importing Route53 Resource
Terraform has good documentation on how to import each resource. If you go to the documentation of the AWS Route53 page, you will see something like the below image, but importing the resource under the module could be challenging sometimes.
To import the Route53 module, you can use the below command.
terraform import 'module.ROUTE53-us-east-1-example.aws_route53_record.my_records[blog.example CNAME]' Z4KAPRWWNC7JR_blog.example_CNAME
This is similar to importing the simple resources, it is just you are adding the module name while importing the resource.
By mistake if you have imported resources with incorrect configuration, please don’t worry, just delete that particular resource from the state file and re-import the resource using Terraform import command.
This is how you import the resources that are part of a module using Terraform. If you are facing any problem related to terraform module import, just let us know through comments.
If you would like to see more such articles, please consider subscribing to our blog.