Import terraform state

Import AWS resource based on particular tag

We can import our AWS resource base on certain AWS resource attribute or tags using terraformer tools.

To install terraformer you can download binary from https://github.com/GoogleCloudPlatform/terraformer/releases and copy it to your /usr/local/bin or /usr/bin

To import resource EC2, EBS, and security group with tags service=tsidata, you can do the following steps

echo 'provider "aws"{}' > provider.tf

terraform init

terraformer import aws --resources=ec2_instance,sg,ebs --path-pattern=output --compact=true --regions=ap-southeast-1 --filter="Name=tags.Service;Value=tsidata"

terraform state list

Generate terraform state logical name from existing terraform code

terraform plan

grep "+" terraformstate

terraform state mv <terraformer-generated-state> <existing-code-state-logical-name>l;

Import resource with random id

This is step by step to import terraform resource with random-id

echo "<hex_random_id>" | xxd -r -p | base64 | tr '/+' '_-' | tr -d '='

terraform import module.example.random.id <prefix-name>, <base64url>

Please do let me know if something is missing, by replying in the thread of this post.