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"
output
and store the terraform code and terraform state in terraform version 0.12.8
terraform_version
in terraform.tfstate
file to your existing terraform version.
terraform state list
terraform plan
terraformstate
grep "+" terraformstate
terraform state mv <terraformer-generated-state> <existing-code-state-logical-name>l;
This is step by step to import terraform resource with random-id
<service-name>-app-<hex_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.