Terraformer: Converting Infrastructure Into Reusable Terraform Code
A CLI tool that generates tf
/json
and tfstate
files based on existing infrastructure (reverse Terraform). • Disclaimer: This is not an official Google product • Created by: Waze SRE
Checkout there Official Github below for more information:
Capabilities:
- Generate
tf
/json
+tfstate
files from existing infrastructure for all supported objects by resource. - Remote state can be uploaded to a GCS bucket.
- Connect between resources with
terraform_remote_state
(local and bucket). - Save
tf
/json
files using a custom folder tree pattern. - Import by resource name and type.
- Support terraform 0.13 (for terraform 0.11 use v0.7.9).
Terraformer uses Terraform providers and is designed to easily support newly added resources. To upgrade resources with new fields, all you need to do is upgrade the relevant Terraform providers.Import current state to Terraform configuration from a provider
Import current state to Terraform configuration from a providerUsage:
import [provider] [flags]
import [provider] [command]Available Commands:
list List supported resources for a providerFlags:
-b, --bucket string gs://terraform-state
-c, --connect (default true)
-С, --compact (default false)
-x, --excludes strings firewalls,networks
-f, --filter strings compute_firewall=id1:id2:id4
-h, --help help for google
-O, --output string output format hcl or json (default "hcl")
-o, --path-output string (default "generated")
-p, --path-pattern string {output}/{provider}/ (default "{output}/{provider}/{service}/")
--projects strings
-z, --regions strings europe-west1, (default [global])
-r, --resources strings firewall,networks or * for all services
-s, --state string local or bucket (default "local")
-v, --verbose verbose mode
-n, --retry-number number of retries to perform if refresh fails
-m, --retry-sleep-ms time in ms to sleep between retriesUse " import [provider] [command] --help" for more information about a command.
Example: Service Level Import
terraformer import aws --resources=ec2_instance --regions=us-east-1 --profile=rahil_scalereal
Example: Resource Level Import
terraformer import aws -r sg,vpc --filter Type=sg;Name=vpc_id;Value=VPC_ID --filter Type=vpc;Name=id;Value=VPC_ID --profile=rahil_scalereal
NOTE: Profile will be changed based on the login methods. If you are using Access Key and Secret Access Key, make sure you have configured in AWS Config file before running this command.
Planning:
The plan
command generates a planfile that contains all the resources set to be imported. By modifying the planfile before running the import
command, you can rename or filter the resources you'd like to import.
The rest of subcommands and parameters are identical to the import
command.
Resource structure
Terraformer by default separates each resource into a file, which is put into a given service directory.
The default path for resource files is {output}/{provider}/{service}/{resource}.tf
and can vary for each provider.
It’s possible to adjust the generated structure by:
- Using
-compact
parameter to group resource files within a single service into oneresources.tf
file - Adjusting the
-path-pattern
parameter and passing e.g.-path-pattern {output}/{provider}/
to generate resources for all services in one directory
It’s possible to combine --compact
--path-pattern
parameters together.
Here Comes a Fun Part, Installation:
- Pre-requisites before installing Terraformer:
- Install Terraform — https://www.terraform.io/downloads
- Now lets move on to installing Terraformer: below link is where you can download ‘exe’ file of Terraformer as per the OS, System Architecture and Cloud Provider.
Example: In my case:
NOTE: It will change in your case based on the Operating System and the Cloud Provider.
- Add the exe file path to path variable by following commands:
- Linux
export PROVIDER={all,google,aws,kubernetes}
curl -LO <https://github.com/GoogleCloudPlatform/terraformer/releases/download/$>(curl -s <https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest> | grep tag_name | cut -d '"' -f 4)/terraformer-${PROVIDER}-linux-amd64
chmod +x terraformer-${PROVIDER}-linux-amd64
sudo mv terraformer-${PROVIDER}-linux-amd64 /usr/local/bin/terraformer
- • MacOS
export PROVIDER={all,google,aws,kubernetes}
curl -LO <https://github.com/GoogleCloudPlatform/terraformer/releases/download/$>(curl -s <https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest> | grep tag_name | cut -d '"' -f 4)/terraformer-${PROVIDER}-darwin-amd64
chmod +x terraformer-${PROVIDER}-darwin-amd64
sudo mv terraformer-${PROVIDER}-darwin-amd64 /usr/local/bin/terraformer
- Windows:
1. Install Terraform - <https://www.terraform.io/downloads>
2. Download exe file for required provider from here - <https://github.com/GoogleCloudPlatform/terraformer/releases>
3. Add the exe file path to path variable
4. Create a folder and initialize the terraform provider and run terraformer commands from there
For AWS - refer <https://learn.hashicorp.com/tutorials/terraform/aws-build?in=terraform/aws-get-started>
NOTE: export PROVIDER value can be provided which cloud provider you are using for your Cloud Resources.
ex: export PROVIDER={aws}, export PROVIDER={azure}, and so on. Depends on how many cloud providers you work, values will differ.
Lets Move on to the Fun Part, Yes you heard it right, Practical!!!!
- First cd into that directory where you want to generated the terraform scripts of Infrastructure.
2. Create a new file ‘versions.tf’ and paste the provider block into that file and save. You will get the provider block from Terraform Registry, Nah I will make your life easy wait Lazy Person..
3. Run Terraform Init command to initialize the terraform to import the resources.
4. Lets learn the Magic: We will see the console for the already created resources first and then Import it through Magic tool which work on our instructions.
5. Now lets import it with Magic:
See the Magic happened above and Terraformer imported the resource that was created by AWS Console.
What Can you import from Terraformer from AWS:
You can check the following document page for the information on resources to import.
That’s All Folks, Now you are a TerraHuman and good to go with Terraformer…!!!!