how to evaluate a variable is set and add or remove attribute for a resource

2,724 views
Skip to first unread message

Ash

unread,
Sep 7, 2015, 1:20:55 AM9/7/15
to Terraform
I have created a RDS db template and want to add the snapshot identifier attribute only if the snapshot variable is set like we do in cloudformation 

"MyDB" : {
  "Type" : "AWS::RDS::DBInstance",
  "Properties" : {
    "AllocatedStorage" : "5",
    "DBInstanceClass" : "db.m1.small",
    "Engine" : "MySQL",
    "EngineVersion" : "5.5",
    "MasterUsername" : { "Ref" : "DBUser" },
    "MasterUserPassword" : { "Ref" : "DBPassword" },
    "DBParameterGroupName" : { "Ref" : "MyRDSParamGroup" },
    "DBSnapshotIdentifier" : {
      "Fn::If" : [
        "UseDBSnapshot",
        {"Ref" : "DBSnapshotName"},
        {"Ref" : "AWS::NoValue"}
      ]
    }
  }
}

Paul Hinze

unread,
Sep 8, 2015, 10:17:30 AM9/8/15
to terrafo...@googlegroups.com
Good question!

We don't have a facility for conditional omission of attributes today, but I think there's a fairly simple workaround here where we just ensure that if "snapshot_identifier" is blank, we treat that as no value and don't restore from a snapshot.

That way you can do something like this:

variable "snapshot" { default = "" }
resource "aws_db_instance" {
  # ...
  snapshot_identifier = "${var.snapshot}"
}

Then you just set `snapshot` to the snapshot name when you want to use one, and leave it blank when you don't.

I'm fairly certain that should work today, but let me know if you have trouble with it and we can make sure it is supported.

Paul


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/83c47ee2-d5a3-4d61-a483-83b2838aa62b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ashley Avileli

unread,
Sep 10, 2015, 11:10:46 PM9/10/15
to terrafo...@googlegroups.com

On Wed, Sep 9, 2015 at 12:17 AM, Paul Hinze <pa...@hashicorp.com> wrote:
variable "snapshot" { default = "" }

Thanks Paul Hinze that trick worked

Thomas Clark

unread,
Nov 10, 2015, 1:16:38 PM11/10/15
to Terraform
I am running in to a similar issue, only this configuration is not working for me. Sorry if there is a simple solution this is my first week working with terraform.

resource "aws_db_instance" "admin-interface" {

final_snapshot_identifier = "${var.snapshot}"
}

variable "snapshot" { default = ""}

error: aws_db_instance.admin-interface: only alphanumeric characters and hyphens allowed in "final_snapshot_identifier"

Seems like 'final_snapshot_identifier' doesn't like an empty string. I would like to just like exclude "final_snapshot_identifier" in my staging env resource completely, but it seems from the previous post that is not possible without defining a whole new resource.

Not really sure where to go from here. I hope this is enough info.
Thanks!
Thomas

Paul Hinze

unread,
Nov 24, 2015, 8:27:18 PM11/24/15
to terrafo...@googlegroups.com
Hi Thomas,

Yeah that sounds like it might be a validation bug. Can you file a GitHub issue with steps to reproduce?

Thanks!

Paul

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

mschu...@demonware.net

unread,
Nov 27, 2015, 7:39:17 PM11/27/15
to Terraform
Hey. Sorry to jump in here but are you saying that all attributes in all resources should be able to accept "" as a value and will thus ignore that attribute? I have tried doing this on the aws_route_table resource and that was not my experience. 

Thanks 

Paul Hinze

unread,
Dec 3, 2015, 7:13:03 PM12/3/15
to terrafo...@googlegroups.com
Sorry to jump in here but are you saying that all attributes in all resources should be able to accept "" as a value and will thus ignore that attribute?

It depends on the attribute. Some Optional attributes are Computed - for those, changing them to "" means "let the provider set any value here". For attributes that are not Computed, changing their value to "" means an explicit "make this attribute empty.

Happy to answer further questions if you saw some confusing behavior!

Paul

md...@justin.tv

unread,
Dec 3, 2015, 11:47:41 PM12/3/15
to Terraform
I have a related question. Going back to the default = "" behaviour you mentioned in https://groups.google.com/d/msg/terraform-tool/xA3QPNX84Bg/SEeBCEt5MQAJ, is there such a default for integer values?

As an example, if I try to set default = "" for route53's weight value, I get `aws_route53_record.some_elb: cannot parse '' as int: strconv.ParseInt: parsing "": invalid syntax` since it's trying to parse an empty string as an int.

Is there a workaround for integers? Will there be support for this in the future? Thanks!

Paul Hinze

unread,
Dec 4, 2015, 5:55:57 PM12/4/15
to terrafo...@googlegroups.com
Good question! Currently the internals of helper/schema don't allow us to distinguish between "zero value" config and "not provided config" - so when weight is not specified as all, we get 0 back internally.

So given that, you can just specify 0 and it should be just like you didn't specify anything.

Hope this helps!

Paul

Reply all
Reply to author
Forward
0 new messages