To my Windows-PowerShell-Terraform-Azure users out there, I just want to share a quick nugget on how to escape the annoying double quotes.
Prerequisites
- PowerShell
- Terraform
Example
Let’s try to import an already existing Azure subnet to the TF state. Given the following terraform import
command:
terraform import azurerm_subnet.devcoops_subnets["private-subnet-1"] /subscriptions/<subId>/resourceGroups/devcoops-rg/providers/Microsoft.Network/virtualNetworks/devcoops-vnet/subnets/private-subnet-1
I’ll get the following error in the console:
Error: Invalid character
on networks.tf line 21:
1: azurerm_subnet.devcoops_subnets[\private-subnet-1\]
This character is not used within the language.
Error: Index value required
on networks.tf line 21:
1: azurerm_subnet.devcoops_subnets[\private-subnet-1\]
Index brackets must contain either a literal number or a literal string.
Error: Invalid character
on networks.tf line 21:
1: azurerm_subnet.devcoops_subnets[\private-subnet-1\]
This character is not used within the language.
Solution
Two things to add. Wrap the whole azurerm_subnet..
part with single quotes and escape the double quotes with \
(backlash). Hence, the right command to do so:
terraform import 'azurerm_subnet.devcoops_subnets[\"private-subnet-1\"]' /subscriptions/<subId>/resourceGroups/devcoops-rg/providers/Microsoft.Network/virtualNetworks/devcoops-vnet/subnets/private-subnet-1
Conclusion
If you have any related unresolved issues, feel free to share them in the comment section below. On a side note, follow our official channel on Telegram.