Skip to content

Single Required Block

GlennChia edited this page Jul 7, 2021 · 1 revision

This patten covers the following phrase in the Terraform Registry:

(Required) A <name of block> block as defined below.

Example:

(Required) A time_period block as defined below.

Simple use case

In the configuration.tfvars file:

time_period = {
  start_date = "2022-06-01T00:00:00Z"
}

In the resource file:

time_period {
  start_date = try(var.settings.time_period.start_date, join("", [formatdate("YYYY-MM", timestamp()), "-01T00:00:00Z"]))
  end_date   = try(var.settings.time_period.end_date, null)
}
  • No for_each required since there is only one block
  • In this example, a try statement is added to the start_date to ensure that the CI passes with a valid date. If the start_date is not defined in the configuration.tfvars, it is generated dynamically using join("", [formatdate("YYYY-MM", timestamp()), "-01T00:00:00Z"] which gets the date of the first day of the month.
Clone this wiki locally