azureterraformazure-rmterraform-cloudterraform-remote-state

Terraform cloud run triggers with Azure


I'm having issue with accessing my data "terrafrom_remote_state" objects.. So I'm following the hashicorp site to deploy azure resource with terraform cloud with run triggers. The trigger is working , running the plan for the second workspace, but it can't access the data i'm passing through the outputs.

I have set the "state" for the first workspace to be shared, and set the run trigger on the second workspace to be triggered by the 1st. No issues here.

I have tried to follow what is on the hasicorp site, but it is for aws so, maybe for azure I have missed something. I will post my outputs , then some code for the second workspace.

Ouputs : which i have looked at in the statefile and look good.

output "rgName" {
  description = "The resource group for resources"
  value = var.rgName
}

output "location" {
  description = "The location for resources"
  value = var.location
}

output "subnet1_id" {
  description = "subnet 1"
  value = azurerm_subnet.subnet1.id
}

2nd workspace

data "terraform_remote_state" "network" {
  backend = "remote"

  config = {

    organization = "Awesome-Company"
    workspaces = {
          name = "TFCloud-Trigger-Network"
    }
  }
}

provider "azurerm" {
  version =  "2.66.0"

  subscription_id = var.subscription_id
  client_id = var.client_id
  client_secret = var.clientSecret
  tenant_id = var.tenant_id

  features{}
}

#Deploy Public IP
resource "azurerm_public_ip" "pip1" {
  name                = "TFC-pip1"
  location            = data.terraform_remote_state.network.outputs.location
  resource_group_name = data.terraform_remote_state.network.outputs.rgName  
  allocation_method   = "Dynamic"
  sku                 = "Basic"
}

#Create NIC
resource "azurerm_network_interface" "nic1" {
  name                = "TFC-TestVM-Nic"  
  location            = data.terraform_remote_state.network.outputs.location  
  resource_group_name = data.terraform_remote_state.network.outputs.rgName 

    ip_configuration {
    name                          = "ipconfig1"
    subnet_id                     = date.terraform_remote_state.network.outputs.subnet1_id 
    private_ip_address_allocation  = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.pip1.id
  }
}

The error is

Error: Unsupported attribute │ │ on main.tf line 26, in resource "azurerm_public_ip" "pip1": │ 26: location = data.terraform_remote_state.network.outputs.location │
├──────────────── │ │ data.terraform_remote_state.network.outputs is object with no attributes │ │ This object does not have an attribute named "location".

I can't access the data.terraform_remote_state.network.outputs


Solution

  • So, I figured this out and it is not in the documentation. A workspace that is a triggered by another workspace will not automatically update it's terrafrom plan.

    Normally when I edit the code in github (or another repo) terraform cloud will automatically run a plan once you have saved that new code. A workspace that is triggered by another will not do that. So, even though I changed the code, I had to manually go to TF Cloud discard the current run on that triggered workspace, and re-run the plan. After this, the run trigger would successfully run.

    It was a weird thing...