Terraform - Importing resources
- Brian Greenfield
- Jun 10, 2021
- 1 min read
Brief
I decided I wanted to learn Terraform, which has been great. Now I love Terraform and want to start using it to mange all my AWS infrastructure going forward. Issue is I already have some AWS resource deployed that I don't want to destroy. Luckily with Terraform there is an option to import resources already deployed.
Method
I have my plan file main.tf and I want to add in a DNS record I already have deployed.
First I create the resource in my plan file
resource aws_route53_record.my_webserver {
}
Now I import the resource from AWS into my resource description, from the CLI using the terraform command:
terraform import resource.name resource_id_from_aws
in the case of the route53_record the ID from AWS is zoneID_recordName_recordType:
terraform import aws_route53_record.my_webserver Z4KMPRWWIKGH_web01.example.com_A
The details of how to import resources types are documented at the bottom of resource type on the Terraform registry.
You can see the imported resource in the state file
terraform state show aws_route53_record.my_webserver
From this information you can add the details into the plan file and now you have a managed resource.
Conclusion
I now have a way of importing existing AWS infrastructure into my plan files, giving me Terraform control over all of my infrastructure.
This method is not limited to AWS, so can be used with the other supported providers.
Comments