Hi team,
Considering following use case. Enabling bedrock logging with tf module. It is regional resource and I’m looking a way to implement some shared state across all instances of the same module. Basically I want the first module deployment to create a bucket and iam role and all other deployments would lookup for that.
I guess explicit var like seed_module = true would work but I’m just thinking if that can be further reduced.
Currently I’ve stuck that init deployment works but next doesn’t cause relying on the data resource and there again the chicken and and an egg question.
Code and error
data "aws_iam_role" "external_role" {
count = var.cw_logging_enabled && var.logging_role_name != null ? 1 : 0
name = var.logging_role_name
}
data "aws_iam_roles" "this_module_roles" {
count = var.cw_logging_enabled && var.logging_role_name == null ? 1 : 0
name_regex = "${local.tf_module}.*"
path_prefix = "/${var.namespace}/"
}
locals {
ext_role_arn = var.cw_logging_enabled && var.logging_role_name != null ? data.aws_iam_role.external_role[0].arn : null
int_role_name = var.cw_logging_enabled && var.logging_role_name == null && length(data.aws_iam_roles.this_module_roles[0].names) == 1 ? tolist(data.aws_iam_roles.this_module_roles[0].names)[0] : null
create_role = alltrue([var.cw_logging_enabled, local.ext_role_arn == null, local.int_role_name == local.name])
effective_role_arn = coalesce(local.ext_role_arn, tolist(data.aws_iam_roles.this_module_roles[0].arns)[0])
}
resource "aws_iam_role" "log_cw_role" {
count = local.create_role ? 1 : 0
name = local.name
assume_role_policy = data.aws_iam_policy_document.log_cw_role_trusted_entity.json
path = "/${var.namespace}/"
tags = local.effective_tags
}
Error:
╷
│ Error: Invalid count argument
│
│ on logging-cw.tf line 62, in resource "aws_iam_role" "log_cw_role":
│ 62: count = local.create_role ? 1 : 0
│
│ The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be
│ created. To work around this, use the -target argument to first apply only the resources that the count depends on.
Appreciate any feedback