Amazon Web Services (AWS) is an on-demand cloud computing platform that offers us a lot of helpful and reliable services. AWS has friendly web interface which user can easily interact with to create virtual machines, networking stuffs, security policies, etc. However, in order to manage all the AWS configuration and keep track of the changes, it is neccessary to work with 3rd party tool like Terraform.
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. You can see here the list of supported providers.
If you are building your IT infrastructure on AWS from scratch with Terraform, it is great, you don’t need to follow this article. But if you already had an existing aws infrastructure and now want to manage with Terraform, you might want to read more :-).
import command to import existing infrastructure into your Terraform state. It will find and import the specified resource into your Terraform state, allowing existing infrastructure to come under Terraform management without having to be initially created by Terraform.
terraform import [options] ADDR ID
- ADDR is the address of your Terraform’s defined resource to import to.
- ID is your AWS object ID.
Currently Terraform does not support to generate the resource’s code automatically. You will have to manually define them before performing the import commands. It is time consuming and might causes problem by human mistakes such as writing invalid syntax, putting wrong object id, etc. So it’s time to play with Terraforming.
Terraforming is a free and open-source tool written in Ruby. It helps you to export existing AWS resources to Terraform style (tf, tfstate).
Currently Terraforming requires Ruby 2.1 and supports Terraform v0.9.3 or higher. You can install Terraforming by
gem install terraforming
Just like Terraform, Terraforming requires access to your AWS infrastructure to be able to export the configuration. You can set the AWS credential by exporting environment variables.
Or you can use
~/.aws/credentials file to manage the profiles then specify
--profile <profile-name> in your
aws_access_key_id = xxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Terraforming supports to export many type of AWS resrouce. For example EC2 instances, Security Group, Route53, VPC, IAM, etc. You can see the full list of supported options by running
Following is an example of exporting existing EC2 instances
terraforming ec2 --profile=khanh
Once you have tf code definitions above, you can just copy and paste them into your Terraform code. But you haven’t finished yet. You will also have to let Terraform know which AWS resource that code block should map to.
terrform import command, we will use it to do the mapping.
terraform import aws_instance.ubuntu-server-01 i-0421ae392a9e26277
ubuntu-server-01is the resource name you defined in Terraform.
i-0421ae392a9e26277is the actual EC2 instance ID which you want to map to.
Now you can confirm the new resource definition by running
terraform plan. If your Terraform does not show any changes to your AWS infrastructure, it means you imported the resource successfully.
That’s all! Now you can do the same steps for other resouces such as Security Group, VPC, Subnet, etc to completely manage your AWS infrastructure under Terraform.