Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Invalid count argument for random_password resource #43

Closed
petur opened this issue Apr 11, 2022 · 4 comments · Fixed by #45
Closed

Error: Invalid count argument for random_password resource #43

petur opened this issue Apr 11, 2022 · 4 comments · Fixed by #45
Labels
bug 🐛 An issue with the system

Comments

@petur
Copy link
Contributor

petur commented Apr 11, 2022

Found a bug? Maybe our Slack Community can help.

Slack Community

Describe the Bug

After upgrading to version 0.15.0, terraform plan fails with the error "Invalid count argument" when planning on an empty configuration (no existing resources) if the variable master_password is set from a random_password resource.

Expected Behavior

The configuration should be planned.

Steps to Reproduce

This is a minimal configuration to reproduce:

resource "random_password" "documentdb" {
  length  = 22
  special = false
}

module "documentdb_cluster" {
  source  = "cloudposse/documentdb-cluster/aws"
  version = "0.15.0"

  master_password = random_password.documentdb.result

  name = "foo"
  vpc_id = ""
  subnet_ids = [""]
}

Run terraform plan to get the error.

Screenshots

╷
│ Error: Invalid count argument
│ 
│   on .terraform/modules/documentdb_cluster/main.tf line 43, in resource "random_password" "password":
│   43:   count   = module.this.enabled && var.master_password != "" ? 0 : 1
│ 
│ 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.
╵

Environment (please complete the following information):

  • OS: Linux
  • Terraform version: 1.1.8
  • Version: 0.15.0

Additional Context

The separate random_password resource is there because this code was written against version 0.14.

The easy workaround for this would be to remove the random_password resource and use the one provided by the module, but that doesn't work for our purposes because the module doesn't return the generated password (in our case, it gets stored in Secrets Manager).

Providing the password as an output would be the easiest fix for this, but another way would be to add a separate generate_password boolean parameter.

Aside - it looks like there may be another bug in the count condition:

count   = module.this.enabled && var.master_password != "" ? 0 : 1

If module.this.enabled is false, then count is always set to 1 - this should surely be the other way around?

@petur petur added the bug 🐛 An issue with the system label Apr 11, 2022
@danvau7
Copy link

danvau7 commented May 10, 2022

Getting the same issue. Can this pull request be merged?

@rafaljanicki
Copy link

Same issue

@achandebois
Copy link

same issue

1 similar comment
@nrosselli-simon
Copy link

same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An issue with the system
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants