Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Importing Azure resources to terraform
1. Install Terraform on mac
brew install hashicorp/tap/terraform
brew install terraform
2. Install the azure-cli
brew install azure-cli
3. Install terraformer tool
To import the existing resources from (AWS, Google cloud,AZURE)
brew install terraformer
4. Authenticate with azure
az login
"cloudName": "AzureCloud",
"homeTenantId": "58fb75e6-9db3-4492-b319-a095a08786ce",
"id": "bc8ba97c-c476-4996-9d49-c92ccfe258f2",
"isDefault": true,
"managedByTenants": [],
"name": "Free Trial",
"state": "Enabled",
"tenantId": "58fb75e6-9db3-4492-b319-a095a08786ce",
"user": {
"name": "",
"type": "user"
5. Create a file with the following content
mkdir -p test
cd test
provider "azurerm" {
features {}
terraform init
export ARM_SUBSCRIPTION_ID=bc8ba97c-c476-4996-9d49-c92ccfe258f2
6. To import all the existing resources from your Azure account
Here "test_gourp" is the resource group in ur Azure account
terraformer import azure -R test_group -r "*"
specific resource
terraformer import azure -R test_group -r virtual_machine
7. This will generate Terraform files for the existing resources (instance)
├── generated
│   └── azurerm
│   └── virtual_machine
│   ├──
│   ├──
│   ├──
│   └── terraform.tfstate
8. Now we can control the exisitng resources
cd generated/azurerm/virtual_machine/
terraform state replace-provider -auto-approve "" "hashicorp/azurerm"
terraform init
terraform refresh
9. 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