Reputation: 41
In azure, using terraform and the azurerm provider, I want to check whether a resource group with a given name already exists.
In a perfect world there'd be something like an azurerm_resource_groups block that took a name pattern and returned all matching resource groups in a list, but that doesn't exist. The azurerm_resource_group data block will fail if the provided name doesn't exist. And as far as I can tell from testing it, the azurerm_resources block doesn't seem to find resource groups.
What I want to be able to do is to say "if this resource group exists, put some other resources you're about to create in it, but if it doesn't exist, create it first"
I just can't find a way - shorting of requiring a variable to tell me whether it exists or not - to differentiate between the cases.
I've tried
data "azurerm_resources" "resource_group" {
name = var.resource_group_name
}
output "rg_list" {
value = data.azurerm_resources.resource_group
}
but I got back an empty block:
rg_list = {
"id" = "resource-8c79a1bd-fcb4-423b-9327-d62f3274210d"
"name" = "my-rg-name"
"required_tags" = tomap(null) /* of string */
"resource_group_name" = tostring(null)
"resources" = tolist([])
"timeouts" = null /* object */
"type" = tostring(null)
}
Upvotes: 0
Views: 3164
Reputation: 121
"if this resource group exists, put some other resources you're about to create in it, but if it doesn't exist, create it first"
If I understood it correctly, this is exactly what Terraform does by default, without any special configuration needed to validate the existence of the resource group.
If you have a defined configuration (like the example below) for a resource group and other for any other Azure resource referenciating the resource_group_name parameter to the resource group resource previously defined, Terraform will automatically map the resource group as a dependency for this other resource and create it first if it doesn´t exist or just create the resource into if it already exists. You don´t even have to use the depends_on clause in this scenario, as Terraform handles the dependency by itself.
resource "azurerm_resource_group" "this" {
name = "rg-name"
location = "northeurope"
}
resource "azurerm_route_table" "this" {
name = "rtb-name"
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
disable_bgp_route_propagation = false
}
Let me know if this helps by accepting the answer.
Upvotes: 0
Reputation: 853
You can use aztfexport A tool to bring your existing Azure resources under the management of Terraform.
Azure Export for Terraform exports resources that are supported by the Terraform AzureRM provider into Terraform state and generate the corresponding Terraform configuration. Both the Terraform state and configuration are expected to be consistent with the resources' remote state, i.e., terraform plan shows no diff. The user then is able to use Terraform to manage these resources.
Source: https://github.com/Azure/aztfexport
You can install the last release on any OS
Source: https://github.com/Azure/aztfexport/releases/tag/v0.12.0
At its most abstract, Azure Export is called as follows:
aztfexport [command] [option] <scope>
The scope changes depending on the command being run, as do the available set of option flags. There are three commands that should be used based on what you are trying to export:
aztfexport resource [option] <resource id>
aztfexport resource-group [option] <resource group name>
aztfexport query [option] <ARG where predicate>
Let me know if this work by accepting the answer.
Upvotes: 0
Reputation: 8048
You can use the data
block for azurerm_resource_group
to check if a resource group with a given name is already existed.
After a workaround on this, I used the locals
block to check whether the resource group exists based on the length of data.azurerm_resource_group.existed_rg
as follows.
provider "azurerm"{
features{}
}
data "azurerm_resource_group" "existed_rg" {
name = "Jahnavi"
}
resource "azurerm_resource_group" "main" {
name = "Jahnavirgg"
location = "eastus"
}
locals {
if_resourcegroup_exists = length(data.azurerm_resource_group.existed_rg) > 0
}
Deployed the above code by giving the existing resource name and the output is shown as below.
After that, I tested the same code by passing the new resource group name and was able to create it successfully.
Upvotes: 0