To put it into context, I created a module and placed it in a repo on Azure and this module contains the creation of a UAI, SQL Server, Database and a schedule job. I am creating the Resource Group using the application code that will use the module, so I can use tfvars and work with various environments.
Here’s my problem, when leaving the complete code containing backend, provider, creation of the resource group and the module, it doesn’t create the resource group, it goes straight to the module and shows the error “A managed resource “azurerm_resource_group” “rg” has not been declared in module.job.” When I comment the module line it interprets the creation of the resource group normally. Make sure the module and application code are in the same branch.
terraform {
backend "azurerm" {
storage_account_name = ""
resource_group_name = ""
container_name = ""
key = ""
}
required_providers {
azurerm = {
version = ""
}
azuread = {
version = ""
}
null = {
version = ""
}
}
}
provider "azurerm" {
subscription_id = var.subscription
features {}
}
resource "azurerm_resource_group" "rg" {
name = "Rg-${var.environment}-${var.region_name}-Schedule-Job"
location = var.region_code
}
### JOB ###
module "job" {
source = ""
environment = var.environment
region_code = var.region_code
region_name = var.region_name
subscription = var.subscription
administrator_login = var.administrator_login
administrator_login_password = var.administrator_login_password
}
I tried to use depends_on
module "job" {
source = "git::/home/vsts/work/r1/a/tf-modules/.git//azure/tf-mssql-schedule-job-agent"
environment = var.environment
region_code = var.region_code
region_name = var.region_name
subscription = var.subscription
administrator_login = var.administrator_login
administrator_login_password = var.administrator_login_password
depends_on = [ azurerm_resource_group.rg. ]
}
I tried to use output e input
resource "azurerm_resource_group" "rg" {
name = "Rg-${var.environment}-${var.region_name}-Schedule-Job"
location = var.region_code
}
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "resource_group_location" {
value = azurerm_resource_group.rg.location
}
samucabrave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
creating Resource Group using Terraform while provisioning the sheduled job
From the error description you can observe that the declaration of resoruce group under job module configuration is missing its seems to be the error
Since youre using the module configuration and creating the rg in root configuration so we need to refer the rg info into the job module so we need to declare the rg info under module configuration.
Demo configuration:
resource "azurerm_resource_group" "rg" {
name = "Rg-${var.environment}-${var.region_name}-Schedule-Job"
location = var.region_code
}
module "job" {
source = "./job"
environment = var.environment
region_code = var.region_code
region_name = var.region_name
administrator_login = var.administrator_login
administrator_login_password = var.administrator_login_password
resource_group_name = azurerm_resource_group.rg.name
resource_group_location = azurerm_resource_group.rg.location
depends_on = [ azurerm_resource_group.rg ]
}
Deployment:
Refer:
https://developer.hashicorp.com/terraform/tutorials/modules/module
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/automation_job_schedule