ok, for completeness here if the code in module 1
module "role" {
count = var.publish_flow_log_to_cloud_watch ? 1 : 0
should_create_iam_role = true
should_require_mfa = false
iam_role_name = local.role_name
iam_role_permissions_boundary = var.cloudwatch_iam_role_permissions_boundary
assume_role_services = ["vpc-flow-logs.amazonaws.com"]
assume_role_custom_conditions = {
cond1 = {
test = "StringEquals"
values = ["${data.aws_caller_identity.current.account_id}"]
variable = "aws:SourceAccount"
},
cond2 = {
test = "ArnLike"
values = ["arn:aws:ec2:*:${data.aws_caller_identity.current.account_id}:vpc-flow-log/${aws_flow_log.cloudwatch[0].arn}"]
variable = "aws:SourceArn"
}
}
custom_iam_policy = !var.use_managed_iam_policies ? data.aws_iam_policy_document.role_policy.json : ""
custom_iam_policy_name = local.role_name
iam_policy_json = var.use_managed_iam_policies ? data.aws_iam_policy_document.role_policy.json : ""
iam_policy_json_name = local.role_name
tags = merge(var.tags, local.tags)
}
data "aws_iam_policy_document" "role_policy" {
statement {
sid = "AWSVPCFlowLogs"
effect = "Allow"
resources = ["*"]
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams"
]
}
}
In the Role Module this is the resource where the error is been raised:
data "aws_iam_policy_document" "managed_policy" {
count = ((var.should_create_iam_group && var.iam_group_assume_role_arns != null) || length(var.iam_policy_json) > 0 || var.should_require_mfa) ? 1 : 0 #LINE 86
source_policy_documents = [
length(data.aws_iam_policy_document.assume_roles) > 0 ? data.aws_iam_policy_document.assume_roles[0].json : "",
length(var.iam_policy_json) > 0 ? var.iam_policy_json : "",
var.should_require_mfa && var.should_create_iam_group ? module.iam_policies.require_mfa_policy : ""
]
}
*note
var.should_create_iam_group defaults to false
var.iam_group_assume_role_arns defaults to null
Here is the TF output
Error: Invalid count argument
on .terraform/modules/baseline.mod1/modules/mod2/main.tf line 86, in data "aws_iam_policy_document" "managed_policy":
count = ((var.should_create_iam_group && var.iam_group_assume_role_arns != null) || length(var.iam_policy_json) > 0 || var.should_require_mfa) ? 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.
The fix i had to apply was in the original call to the module, i changed âiam_policy_json = data.aws_iam_policy_document.role_policy.jsonâ into
iam_policy_json = var.use_managed_iam_policies ? jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource" : [
"arn:aws:logs:*:*:*"
]
}
]
}) : ""