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

'NoZeroValues' validation restricts conditional resources #238

Closed
ianmuscat opened this issue May 24, 2019 · 2 comments
Closed

'NoZeroValues' validation restricts conditional resources #238

ianmuscat opened this issue May 24, 2019 · 2 comments

Comments

@ianmuscat
Copy link

Terraform Version

terraform -v
Terraform v0.12.0
+ provider.digitalocean v1.3.0

Affected Resource(s)

Please list the resources as a list, for example:

  • digitalocean_database_cluster
  • (probably other resources too)

Terraform Configuration Files

terraform {
  required_version = ">= 0.12.0"
}

provider "digitalocean" {
  version = "~> 1.3"
}

resource "digitalocean_database_cluster" "postgres" {
  name       = "postgres-test"
  region     = "ams3"
  engine     = "pg"
  version    = "11"
  size       = "db-s-1vcpu-1gb"
  node_count = "${terraform.workspace == "prod" ? 1 : 0}"
}

Expected Behaviour

  • Skip the creation of the resource if it does not yet exist
  • Destroy the resource if it currently exists

Actual Behaviour

The NoZeroValues validation rule restricts this behaviour.

terraform plan

Error: node_count must not be zero

  on main.tf line 5, in resource "digitalocean_database_cluster" "postgres":
   5: resource "digitalocean_database_cluster" "postgres" {

Steps to Reproduce

  1. terraform workspace new dev
  2. terraform plan

Important Factoids

The example I am using above makes use of Terraform Workspaces, however, this could probably be applied to other scenarios too.

References

@andrewsomething
Copy link
Member

Hi @ianmuscat! Rather than node_count, you should use count for creating a resource conditionally. The node_count attribute is specific to the digitalocean_database_cluster resource that maps to num_nodes in the DigitalOcean database API. From the API docs:

The number of nodes in the database cluster. Valid values are are 1-3. In addition to the primary node, up to two standby nodes may be added for highly available configurations. The value is inclusive of the primary node. For example, setting the value to 2 will provision a database cluster with a primary node and one standby node.

Using the config below, I was able to successfully make the resource conditional based on the workspace:

resource "digitalocean_database_cluster" "postgres" {
  count      = "${terraform.workspace == "prod" ? 1 : 0}"
  name       = "postgres-test"
  region     = "ams3"
  engine     = "pg"
  version    = "11"
  size       = "db-s-1vcpu-1gb"
  node_count = 1
}

Example output in default workspace:

asb@asb-do:/tmp/terra-test$ terraform workspace select default
Switched to workspace "default".
asb@asb-do:/tmp/terra-test$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

Example output in prod workspace:

asb@asb-do:/tmp/terra-test$ terraform workspace select prod
Switched to workspace "prod".
asb@asb-do:/tmp/terra-test$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # digitalocean_database_cluster.postgres[0] will be created
  + resource "digitalocean_database_cluster" "postgres" {
      + database   = (known after apply)
      + engine     = "pg"
      + host       = (known after apply)
      + id         = (known after apply)
      + name       = "postgres-test"
      + node_count = 1
      + password   = (known after apply)
      + port       = (known after apply)
      + region     = "ams3"
      + size       = "db-s-1vcpu-1gb"
      + uri        = (known after apply)
      + user       = (known after apply)
      + version    = "11"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Please, let us know if this resolves the issue for you!

@ianmuscat
Copy link
Author

@andrewsomething thank you very much for the clarification, this makes much more sense now 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants