I’m new to Terraform, and I’m trying to handle creating/accessing a resource group. In our staging
environment, the resource group was created manually via the Azure console, and I want to access it in Terraform via a data
block, as so:
data "azurerm_resource_group" "my_rg" {
name = "existing_staging_rg_name"
}
However, in our dev
env, this resource group doesn’t exist yet, so I plan to create it via a module
block, in order to use my company’s repo of pre-approved published TF modules:
module "my_rg" {
source = "github.com/org-name/module-that-creates-an-rg-repo-name"
# Additional input vars...
}
My questions are:
- How can I ensure that only one of the above blocks is executed, depending on the env?
- How can I access this resource group uniformly from other modules? I.e. if the env is
staging
, I want to usedata.azurerm_resource_group.my_rg.name
, indev
I want to usemodule.my_rg.rg_name
.