0
votes

I'm having a problem with my terraform configuration for AWS, i hope someone can help me out.

I have a production variable that i utilize to create an RDS cluster:

resource "aws_rds_cluster" "cluster" {
  count = "${var.production ? 1 : 0}"
  ...
}

So obviously, the resource is ctreated if production = true. In another resource i want to reference the endpoint attribute of this cluster, if production was set to true. If not, i want to use another variable:

module "ecs" {
  aws_ecs_taskdefinition-environmentVariable-list = <<EOF
[
  { "name" : "SomeName", "value" : "${var.production ? aws_rds_cluster.cluster.endpoint : var.ENV_DATABASE_HOST}:3306" },
]
EOF
}

The issue i'm having, is that when production is false, it is as if terraform tries to resolve the 'aws_rds_cluster.cluster.endpoint', even though that is not the value supposed to be used. And that obviously fails, since in production that resource has count 0:

module.ecs.var.aws_ecs_taskdefinition-environmentVariable-list: Resource 'aws_rds_cluster.cluster' not found for variable 'aws_rds_cluster.cluster.endpoint'

I have the same kind of issue in the relation between a securityGroup and a securityGroupRule for that RDS cluster. Even though the count of the rule is set to 0, it seems like terraform tries to resolve the id of the group it references, which it obviously cant because the group also have a count of 0.

1
What do you do when it's not production? Use an in memory database? Have you considered pushing the RDS cluster endpoint address into the ecs module and using a data source to fetch it from there? It would cause you an issue on the first plan if the RDS cluster hadn't yet been created but you could potentially separate the RDS cluster and the ECS stuff into separate parts for Terraform to work on. - ydaetskcoR
@ydaetskcoR The thing is, they should not be considered as seperate parts from terraforms perspective. This cluster is only serving grafana - hence it's part of the grafana configuration. In memory database is not an option, grafana is running on ECS and if the task goes down so does the in memory database. Unless i make sure grafana always runs on the same ec2 with the inmemory, but that kinda defeats the purpose of using ecs. Also, that doesn't solve my initial problem, that i want to create "internally dependent" resources in some condition (i also have that problem in other configs) - Frederik Nygaard Svendsen
What do you do instead of a cluster in non prod then? - ydaetskcoR
@ydaetskcoR I have a "common" cluster that several other applications use as well. This is to minimize cost. That way, on DEV/TEST several applications use the same cluster, but in prod each application has its own cluster. This way, DB load on one application wont affect any other application. - So the terraform configuration for the dev/test clusters do not belong with the grafana terraform configuration, it has is its own separate configuration - Frederik Nygaard Svendsen
@ydaetskcoR Sorry, i think i misread your first comment, i thought you suggested using an in memory database instead :) Regarding pushing the endpoint address into the ecs module and using the output, does that not just move the problem to the module? - Frederik Nygaard Svendsen

1 Answers

0
votes

I had the similar issue for which i used splat syntax. However I have to assign a variable (not list). Iam sure it works here as well.

resource "aws_rds_cluster" "cluster" {
  count = "${var.production ? 1 : 0}"
  ...
}

module "ecs" {
xyz_endpoint = "${join("", aws_rds_cluster.cluster.endpoint)}"
.............
}

Statement #1:
xyz_endpoint = "${join("", aws_rds_cluster.cluster.endpoint)}"
Statement #2:
xyz_endpoint = ${var.production ? aws_rds_cluster.cluster.endpoint : ""}

Both above statements yield the same result however when the resource 'aws_rds_cluster.cluster' is not created statement #2 produces error.

Why this is so?

Slats cause attributes to be accessed lazily

Please let me know if this doesnt work (after changing it for your scenario).

Regards Sudhakar