5

How do I replace an entire block of a terraform script with a variable? For example,

resource "azurerm_data_factory_trigger_schedule" "sfa-data-project-agg-pipeline-trigger" {
  name            = "aggregations_pipeline_trigger"
  data_factory_id = var.data_factory_resource_id
  pipeline_name   = "my-pipeline"

  frequency = var.frequency
  schedule {
    hours   = [var.adf_pipeline_schedule_hour]
    minutes = [var.adf_pipeline_schedule_minute]
  }
}

In the above sample, how to make the entire schedule block configurable using a terraform variable?

I tried this but it isn't working.

schedule = var.schedule

1 Answer 1

7

Nope, that is not possible as schedule is a block with arguments and it is not an argument itself. Maps are aggregate types and and they are made of primitive types (e.g., numbers in this case). A more detailed explanation on primitive and aggregate types with examples can be found in [1] (h/t: Matt Schuchard). In such cases, I prefer to do something like this:

variable "schedule" {
  type = object({
    hours = number
    minutes = number
  })
  description = "Variable to define the values for hours and minutes."
  
  default = {
    hours = 0
    minutes = 0
  }
}

Then, in the resource:

resource "azurerm_data_factory_trigger_schedule" "sfa-data-project-agg-pipeline-trigger" {
  name            = "aggregations_pipeline_trigger"
  data_factory_id = var.data_factory_resource_id
  pipeline_name   = "my-pipeline"

  frequency = var.frequency
  schedule {
    hours   = [var.schedule.hours]
    minutes = [var.schedule.minutes]
  }
}

[1] https://www.terraform.io/plugin/sdkv2/schemas/schema-types#typemap

Sign up to request clarification or add additional context in comments.

2 Comments

Could go even further with object({ hours = number, minutes = number }) type specification. The variable value already matches that, but the type specification is much broader at the moment. Also maybe mention why this is not possible because of the provider attribute schema type differences.
Agreed, will fix it, thanks for the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.