Create Resource before locals variable eval

is there any way to force a creation of a resource before a locals is evaluated.

I say this as my locals var isnt working correctly because the resource has not been created yet.

is there a depends_on for locals?

I have used a workaround to force Terraform to change the order of creation / evaluation of resources.

Don’t know if there is a better way or if you can adjust this hack to your current need with locals.

[resource A] - [resource B]

In order to force Terraform to create [Resource A] before [Resource B] I create a fake dependency (B needs something, like an ID, from A).

Example:

When creating [B = aws_rds_mariadb_pro_pub_01] pass a variable referencing [A = aws_rds_sn_pub_pro_01].

module "aws_rds_mariadb_pro_pub_01" {
  source = "./modules/aws/rds/instance"
  identifier              = var.aws_rds_mariadb_pro_pub_01["identifier"]
  ...
  # Workaround for dependency.
  # We need Terraform to wait for aws_rds_sn_pro_01 creation before the RDS DB can use it.
  db_subnet_group_name    = module.aws_rds_sn_pub_pro_01.id 
  ...
  tag_cost_center         = var.aws_rds_mariadb_pro_pub_01["tag_cost_center"]
}

Full example (search for “workaround” )

Hi @freibuis,

Terraform will, as with all objects, automatically infer dependencies for local values based on the references they contain. If you are seeing ordering problems then I suspect something other than the dependencies of a local variable would be the cause.

Can you say a little more about the problem you’ve encountered, including some real code examples? I’d like to understand better what’s going on so I can suggest ways to address it.

I had a closer look to see what was going on. the issue is that the data.aws_route_tables.resource.ids returns a set and not a list. therefore on each run, the ordering is different… this is throwing the ordering on anything using it in a tailspin. tolist function is no help either (or tolist(sort()))

this makes the local fire off before the resource creation… there is no workaround for this.

if the data.aws_route_tables.resource.ids returned a list… I think the issue would work.

basically I am building an object in the locals var.

  vpc_cidrs_routes = (length(aws_vpc_peering_connection.this) > 0 && length(data.aws_route_table.all_selected) > 0) ? [
    for key, route_table in data.aws_route_table.all_selected : {
      key            = route_table.id
      route_table_id = route_table.id

      vpc_peering_connection_id = element([for connection in aws_vpc_peering_connection.this :
      connection.id if route_table.vpc_id == connection.vpc_id], 1)

      destination_cidr_block = aws_vpc.this.cidr_block
    }
  ] : []

this basically is building a map routes so I can feed it to a aws_route for the peering connection(s)

I just need to figure a fix for the data.aws_route_tables.name.ids

Hi @freibuis,

Can you say more about what happened when you tried sort(data.aws_route_tables.resource.ids)? If the unordered nature of the set of ids were the problem then sort ought to address that because it would cause the result to always be consistently ordered lexically by the id, so it still seems to be that something different is going on. It would be helpful if you could show a smaller example illustrating just the problem you’re describing along with the result it’s producing and the result you expected it to produce; unfortunately with what you’ve shared so far it’s hard for me to determine what parts of it are significant to the discussion or not.