The manual creation of cloud infrastructures is not always practical and satisfactory. It can cause troubles such as recreation of identical infrastructures on a different environment, a lot of unused resources and a lot of effort to set up new infrastructures.
All these issues can end up costing your organisation a lot of money. Therefore, the most optimal solution for this is the Infrastructure as Code (IaC) approach – which can help you define your infrastructures on time.
Terraform is one of these great IaC tools. With it, you can describe all your infrastructure’s resources in HCL or JSON config files with ease. Later, these configurations can serve as templates to replicate the exact same infrastructure on different stages. The single resource definitions can be reused among different infrastructures. In addition to that, you can manage infrastructures from different cloud providers, such as AWS, Google Cloud, etc.
But what if you have already created your infrastructures manually?
Terraform has a really nice feature for importing existing resources, which makes the migration of existing infrastructure into Terraform a lot easier. In order to do that we can use the import command:
Will that be sufficient?
Unfortunately, no. The current version of Terraform imports resources into their internal state only. It does not create a configuration file. Nevertheless, it helps a lot with the transition of existing components into Terraform.
We can try and see how does the import command look in action. Let’s do the following: create a new folder, run terraform init command and create main.tf file.
Now, we need to add the initial configuration in our main.tf file:
Then, run the import command:
Once the import is done, you could check the new TF state located in terraform.tfstate file. In the JSON, there is a lot of additional information such as resource ARN, hosted zone information, bucket domain name, etc.
From here, you can manually add some of the properties into your main.tf config. Afterwards, the new resource definition can be used to recreate it or to create new infrastructures.
I work on a TyphoonX – a cloud-native platform that automates application lifecycle management in cloud environments, focused on AWS.
If you have any questions, feel free to send us an email at: email@example.com.