Terraform migration made easy by terracognita

Pradipta Sanyal
Generac Clean Energy
2 min readJul 20, 2022

We all have been there, created a whole bunch of resources in cloud, to figure out some stuff quickly, without any IAC implementation and now that the POC went well, we would want to control the resources via IAC. Now this poses a problem as we have gone so deep that it seems to be a cumbersome work to translate current resources in proper IAC format without breaking a single thing.

Recently I was looking for easy migration options for existing AWS Infrastructure and stumbled upon terracognita (https://github.com/cycloidio/terracognita).

Terracognita has the capability to analyze your current AWS infrastructure and import terraform (.tf) files , alongwith their .tfstate files. Once you have these files in your grasp , you can easily update / delete infrastructure like any regular terraform-controlled resources.

I will briefly show you how this works in reality, I have used terracognita with a variety of resources e.g. EC2, S3 even ECS clusters and Task Definitions and was successfully able to import current cloud resources in the forms of .tf and .tfstate files.

Example:

I have created an S3 bucket in AWS as terracognita-demo from AWS console. Now when I run the following command, terracognita imports that bucket to Terraform configuration and also to Terraform state file.

terracognita aws --hcl --aws-access-key="<YOUR_AWS_ACCESS_KEY_ID_HERE>" --aws-secret-access-key="<YOUR_AWS_SECRET_ACCESS_KEY_HERE>" --aws-default-region=<AWS_REGION> --tfstate=resources.tfstate -i aws_s3_bucket --target='aws_s3_bucket.terracognita-demo'

Console output will look like this :

If you inspect the folder structure , you will notice terracognitahas created a folder — aws-access-key=<AWS_ACCESS_KEY> and inside that you will find your terraform files :

  • hcl.tf — contains terraform cloud provider information
  • s3.tf — contains AWS resource

There is also a resources.tfstate file that basically contains the current state of AWS resource and as per Terraform definition it will run a comparison with incoming changes in case of a terraform plan / apply .

With these terraform files in your arsenal now, you can do further updates or deletion of AWS resources very easily.

Terracognita supports multi-cloud — AWS, Azure and GCP at the moment. Kudos to the team at Cycloid for this amazing tool!

--

--

Pradipta Sanyal
Generac Clean Energy

Cloud Architecture, Site Reliability, AWS, HashiCorp Products, Observability, Docker and Kubernetes, DevOps Culture