Import Existing Resources¶
https://developer.hashicorp.com/terraform/cli/commands/import
https://dev.to/cloudskills/getting-started-with-terraform-on-azure-importing-existing-infrastructure-43fa
Manage pre-existing infrastructure by terraform
Import manually created resources to be under the control of terraform configurations
simple resource¶
create terraform code
import state:
terraform initandterraform import <terraform-resource-name>.<resource-label> <azure-resource-id>check new resource in state file:
cat terraform.tfstatevalidate terraform configuration
terraform planshould report no changes
multiple resources¶
create terraform code for all resources
import state for resources one by one
check and validate the states
import module¶
in the folder calling the module with
backend.tfrunterraform initthen for each resource run
terraform import module.<module-name>.<terraform-resource-name>.<resource-label> <azure-resource-id
import one of for_each from module¶
in unix shell, use single quotes to make the inner address be taken literally
terraform import 'module.my_storage.azurerm_storage_container.container["<container-name>"]' \
https://<storage-account>.blob.core.windows.net/<container-name>
terraform import 'module.my_storage.azurerm_storage_container.container[\"<container-name>\"]' \
https://<storage-account>.blob.core.windows.net/<container-name>
import with variables¶
https://stackoverflow.com/questions/57187782/how-to-use-terraform-import-with-passed-in-variables
Note that even variables in the variables.tf that are not used are still required.
terraform import -var 'environment=sandbox' azurerm_storage_account.my_storage foo
terraform import -var-file='../tfvars/prod.tfvars' 'module.MySystem.azurerm_windows_virtual_machine.windsvm["dsvm0003"]' \
"/subscriptions/xxx-xxx-xxx-xxx-xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/DSVM0003"
Another way is set default values for the variables in the variables.tf file or set the real value from the state file.
terragrunt: in terragrunt.hcl the inputs section can set the values for the input variables from dependencies. Therefore the best way to import the existing resource is via terragrunt import ADDR ID.