68

I have the following condition:

resource "aws_elastic_beanstalk_application" "service" {
  appversion_lifecycle {
    service_role          = "service-role"
    delete_source_from_s3 = "${var.env == "production" ?   false : true}"
  }
}

If var.env is set to production, I get the result I want.

However if var.env is not defined, terraform plan will fail because the variable was never defined.
How can I get this to work, without ever having to define that variable?

1
  • Do you mean without having to define a default value? Commented Nov 8, 2018 at 16:09

3 Answers 3

116

Seems these days you can also use try to check if something is set.

try(var.env, false)

After that your code will work since var.env is now defined with the value false even if var.env was never defined somewhere.

https://www.terraform.io/docs/configuration/functions/try.html

Sign up to request clarification or add additional context in comments.

4 Comments

This answer helped me out on my current project, creating a module for security groups & rules. I haven't gotten it fully working yet but this allows for me to set values to null when I'm flattening a list built out of a complex map.
This is a WRONG answer. See the terraform doc link at the end of the page. The try function will not catch errors relating to constructs that are provably invalid even before dynamic expression evaluation, such as a malformed reference or a reference to a top-level object that has not been declared. As a result, neither var.non_existent or local.non_existent are supported.
@t7tran, in this specific case it is the correct answer. Also what you imply is described in the link too so whats the point here?
There's a bit of confusion here , i came across this question/answer looking for something similar to what the OP is asking , and from the way the question is asked it's not very clear that the var.env IS defined with a variable block but no values has been set ... probably that's what threw off @t7tran
41

if you are using Terraform 0.12 or later, you can assign the special value null to an argument to mark it as "unset".

variable "env" {
    type = "string"
    default = null
}

You can't just leave it blank, not with the current versions.

2 Comments

but how to check it later that it is unset ?
Apparently you can check var.env == null
19

You can have the default of the variable set to an empty string:

variable "env" {
  description = "Env where the module is deployed."
  type        = string
  default     = ""
}

Once that is done, your check var.env == "production" will produce false and the argument delete_source_from_s3 will be assigned to the value true.

Side note, there is no need for interpolation in the statement,

"${var.env == "production" ? false : true}"

just go with,

delete_source_from_s3 = var.env == "production" ? false : true

https://discuss.hashicorp.com/t/how-do-write-an-if-else-block/2563

1 Comment

delete_source_from_s3 = var.env != "production" is the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.