Terraform: Allow to omit resource keys defined in a config based on the value

Created on 29 Nov 2016  ยท  5Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.7.11

Affected Resource(s)

All.

Terraform Configuration Files

resource "aws_elasticache_replication_group" "stack_redis" {
  ...
  node_type = "cache.t2.micro"
  snapshot_window = "${var.elasticache_snapshot_window}"
  ...
}

Debug Output

* aws_elasticache_replication_group.stack_redis: Error creating Elasticache Replication Group: InvalidParameterCombination: The requested configuration does not support snapshotting. Snapshot window parameter should not be specified.

Since just defining the snapshot_window parameter will cause AWS CLI to complain it should not be defined at all as I'm attempting to enable snapshotting for a t2.micro instance, it would be nice if I could pass in a value or somehow define the key to be omitted, conditionally (note the node type value is dynamic in my case). E.g.

resource "aws_elasticache_replication_group" "stack_redis" {
  ...
  snapshot_window = "${omit(var.elasticache_snapshot_window)}"
  ...
}

Something like that in case the value is empty - whatever that must be defined as, e.g:

elasticache_snapshot_window = null
bug provideaws

Most helpful comment

Tagging this as an AWS bug, we should just detect empty string in this case and replace it.

I'm open to returning an "empty" type in the future from HIL though @apparentlymart

All 5 comments

Hi @jurajseffer,

For string attributes Terraform doesn't really distinguish between the empty string and null, so it's possible that setting this value to the empty string would do what you need here. For example:

variable "instance_type" {
}

variable "instance_type_snapshot_window" {
    type = "map"
    default = {
        "cache.t2.micro": "",
        "cache.m3.large": "05:00-09:00"
    }
}

resource "aws_elasticache_replication_group" "stack_redis" {
    node_type = "${var.instance_type}"    
    snapshot_window = "${lookup(var.instance_type_snapshot_window, var.instance_type)}"
    // ...
}

In some future version of Terraform it's likely that the interpolation syntax will gain some support for boolean logic so we can more cleanly deal with conditionals, but for now looking up values in a map is the usual pattern for doing things conditionally.

@apparentlymart Thanks for that. I tried setting it to an empty string and passing that in but that didn't work either - same message came back from aws.

Tagging this as an AWS bug, we should just detect empty string in this case and replace it.

I'm open to returning an "empty" type in the future from HIL though @apparentlymart

affects me too...I wonder if there's a workaround.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings