Home > OS >  Post Build Steps In Terraform
Post Build Steps In Terraform

Time:01-07

I have recently came across a problem in Terraform when making Azure Event Grid Topic Subscriptions for our web app. Terraform builds the web app, and everything else apart from Event Topic Subscriptions because on the pipeline I am passing in a Csproj file before hand, and for the Event Grid topic webhook handshake to validate the web app needs the files running from the csproj file

I then thought of making a post build step for the Topic, on my pipeline Terraform makes all the resources then the artifact deploys to the web app, then a post Terraform task runs and would build the Event Grid topic subscriptions, so I thought.

I put this post build tf file in its own folder on the repo but under the main project where all the other tf files are, so essentially everything else sits in the main project file then the post build folder sits under the main project file but I get an error on my pipeline that sates:

╷
│ Error: Reference to undeclared resource
│ 
│   on eventgrid_subscriptions.tf line 3, in data "azurerm_eventgrid_topic" "read_system_notification_topic":
│    3:     azurerm_eventgrid_topic.Event_Grid_System_Notification_Topic
│ 
│ A managed resource "azurerm_eventgrid_topic"
│ "Event_Grid_System_Notification_Topic" has not been declared in the root
│ module.
╵
╷
│ Error: Reference to undeclared resource
│ 
│   on eventgrid_subscriptions.tf line 6, in data "azurerm_eventgrid_topic" "read_system_notification_topic":
│    6:   resource_group_name = azurerm_resource_group.example.name
│ 
│ A managed resource "azurerm_resource_group"
│ "example" has not been declared in the root
│ module.
╵
##[error]Error: The process '/opt/hostedtoolcache/terraform/1.0.4/x64/terraform' failed with exit code 1

Yet Terraform just went and successfully made those resources in the other part of the pipeline task. Its like it cant see anything outside of the folder. I did initialise inside the post build folder should I have done that? I am also using a remote state that I have also pointed the Terraform code to for the post build folder, but the state file is the same for the main build and the post build. I assumed Terraform would know and just kick off a plan on top of what it just ran and successfully made.

Here is my Event Grid Topic Subscription Code:

data "azurerm_eventgrid_topic" "read_system_notification_topic" {
  depends_on = [
    azurerm_eventgrid_topic.Event_Grid_System_Notification_Topic
  ]
  name                = var.Event_Grid_System_Notification_Topic_name
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_eventgrid_event_subscription" "sub" {
  name  = "SystemNotificationToWebsite"
  scope = data.azurerm_eventgrid_topic.read_system_notification_topic.id

  webhook_endpoint {
    url = format("https://${azurerm_app_service.website_app.name}.azurewebsites.net/messaging?authcode=${local.resource_group_id}")
  }
  included_event_types = ["SystemNotification.MaintenanceStarted", "SystemNotification.MaintenanceFinished", "SystemNotification.MaintenanceWarning"]
}

Essentially I'm asking whats the best way to make post build steps in Terraform?

Is it to use work spaces? enter image description here

Code for Topic:

resource "azurerm_eventgrid_topic" "Event_Grid_System_Notification_Topic" {
  name                = var.Event_Grid_System_Notification_Topic_name
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location

  tags = {
    environment = "Development"
  }
}

CodePudding user response:

I tried the same code and structure in my local environment and received the same error as below:

enter image description here

enter image description here

So as a solution , We have to add the data blocks for all the resource that we have created in the root folder and also add a variable.tf file with the required variables that we are using inside post_build :

eventgrid_subscriptions.tf :

provider "azurerm" {
  features{}
}

data "azurerm_resource_group" "example" {
  name = var.rg_name
}
data "azurerm_app_service" "website_app" {
  name = var.app_name
  resource_group_name = data.azurerm_resource_group.example.name
}
data "azurerm_eventgrid_topic" "read_system_notification_topic" {
  name                = var.Event_Grid_System_Notification_Topic_name
  resource_group_name = data.azurerm_resource_group.example.name
}

resource "azurerm_eventgrid_event_subscription" "sub" {
  name  = "SystemNotificationToWebsite"
  scope = data.azurerm_eventgrid_topic.read_system_notification_topic.id

  webhook_endpoint {
    url = format("https://${data.azurerm_app_service.website_app.name}.azurewebsites.net/messaging?authcode=${data.azurerm_resource_group.example.id}")
  }
  included_event_types = ["SystemNotification.MaintenanceStarted", "SystemNotification.MaintenanceFinished", "SystemNotification.MaintenanceWarning"]
}

variable.tf

variable "rg_name" {
  default="testbuild"
}
variable "Event_Grid_System_Notification_Topic_name" {
  default="testansumantopic"
}
variable "app_name" {
  default="ansumantestapp"
}

Output:

enter image description here

  •  Tags:  
  • Related