Skip to content

Instantly share code, notes, and snippets.

@kiranchavala
Created September 27, 2021 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiranchavala/c2a01a7a43ea15cb8921a710d6cf22ea to your computer and use it in GitHub Desktop.
Save kiranchavala/c2a01a7a43ea15cb8921a710d6cf22ea to your computer and use it in GitHub Desktop.
Importing AWS resources to terraform.txt
1. Install Terraform on mac
brew install hashicorp/tap/terraform
brew install terraform
2. Install the awscli
brew install awscli
3. Configure the credentials
aws configure
It will create 2 files
/Users/kiran_chavala/.aws/config
/Users/kiran_chavala/.aws/credentials
4. Install terraformer tool
To import the existing resources from (AWS, Google cloud,AZURE)
https://github.com/GoogleCloudPlatform/terraformer
brew install terraformer
5. Create a provider.tf file with the following content
mkdir -p test
cd test
vi provider.tf
provider "aws" {
region = "ap-south-1"
}
terraform init
6. To import all the existing resources from your AWS account
terraformer import aws --resources="*" --connect=true --regions=ap-south-1
terraformer import aws --resources=aws_instance --connect=true --regions=ap-south-1
7. This will generate Terraform files for the existing resources (instance)
.
├── generated
│   └── aws
│   └── ec2_instance
│   ├── instance.tf
│   ├── outputs.tf
│   ├── provider.tf
│   └── terraform.tfstate
└── main.tf
8. Now we can control the exisitng resources
cd generated/aws/ec2_instance/
terraform state replace-provider -auto-approve "registry.terraform.io/-/aws" "hashicorp/aws"
terraform init
terraform refresh
To change the setting of a resource after editing the respective resource tf file
terraform plan
terraform apply
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment