value of 'count' cannot be computed while using modules.

900 views
Skip to first unread message

Vamshi krishna

unread,
Mar 1, 2017, 10:16:50 PM3/1/17
to Terraform
I was using terraform modules for IAM user creation, add multiple inline policies and multiple policy_arn's to the user after creation. But now I got an issue where I i create an IAM_POLICY and get the arn of this policy and i am trying to add it as `policy_arn ="{module.policy.policy_arn}"` but i was getting the error, value of 'count' cannot be computed.
My current version of terraform is `0.8.7`

module/user/users.tf
variable user {}
variable policy_arn {
   type = "list"
   default = ["default"]
}

variable policy_file {
  type = "list"
  default = ["default"]
}

resource "aws_iam_user" "user" {
  name  = "${var.user}"
}

resource "aws_iam_access_key" "key" {
  user  = "${var.user}"
}

resource "aws_iam_user_policy" "user_policy" {
    count = "${element(var.policy_file, 0) =="default" ? 0: length(var.policy_file)}"
    name = "${element(var.policy_file,count.index)}"
    user = "${var.user}"
    policy = "${file("../policies/${element(var.policy_file,count.index)}.json")}"
    depends_on = ["aws_iam_user.user"]
}

resource "aws_iam_user_policy_attachment" "policy_attach" {
    count = "${element(var.policy_arn, 0) =="default" ? 0: length(var.policy_arn)}"
    user  = "${var.user}"
    policy_arn = "${element(var.policy_arn, count.index)}"
    depends_on = ["aws_iam_user.user"]
}



module/policy/policy.tf

variable policy_file {
  type = "string"
  default = "default"
}

variable description {
  type = "string"
  default = "policy description"
}

resource "aws_iam_policy" "policy" {
    path = "/"
    description = "$(var.description}"
    name = "${var.policy_file}"
    policy = "${file("../policies/${var.policy_file}.json")}"
}


module "app_user" {
  source = "../module/user"  
  user = "app-user"     
  policy_file = ["ec2_access", "rds_access", "${module.test_policy.policy_arn}" ]
  policy_arn = [ "arn:aws:iam::aws:policy/ReadOnlyAccess","arn:aws:iam::aws:policy/AmazonSQSFullAccess"]
}

module "test_policy" {
  source = "../module/policy/policy.tf"
  policy_file = "test_policy"
  description = "Read access to the autoscale event queue"
}

output "policy_arn" {
  value = "${module.test_policy.policy_arn}"
}



when i do terraform plan i was getting the error the aws_iam_user_policy.user_policy: value of 'count' cannot be computed.
now i am not sure. how would i get the arn of the policy created in other module to the current policy_arn to the user.

Any suggestions and modifications to my modules will be appreciated. thanks.

Vamshi krishna

unread,
Mar 1, 2017, 10:33:10 PM3/1/17
to Terraform
small correction to main.tf 
  policy_file = ["ec2_access", "rds_access" ]
  policy_arn = [ "arn:aws:iam::aws:policy/ReadOnlyAccess","arn:aws:iam::aws:policy/AmazonSQSFullAccess", "${module.test_policy.policy_arn}"]

Ruben Rodriguez

unread,
Mar 8, 2017, 6:52:55 PM3/8/17
to Terraform
I'm having a similar problem with a module of mine.  I hope there will be some response here soon.  Have you submitted an issue on github?

Vamshi krishna

unread,
Mar 9, 2017, 5:22:20 PM3/9/17
to Terraform
Yep. raised an issue today. feel free to add more information. 
Reply all
Reply to author
Forward
0 new messages