简体   繁体   中英

If else condtion using terrafrom modules

I want to create an if else condition in my code. Let's say I want to provision a server, I just want to make sure if (name = abc or name=xyz) & (type=pqr) then my instance type=jkl.

I am unable to set up such a condition in my variables.tf file. PS: I am a newbie in Terraform(2hrs old). thanks

here's an example. I want instance_type to automatically pick up a value "pqr"

testabc.tf

module "testabc" {
 source ="/modules/xyz"
 name = "abc"
 hostname = "jdksnkfjsdn"
 instance_type = "hfd"
}

The way you have described what you want to achieve is not possible the way you are trying to do it. However, with some additional configuration, that can be done. For example, if you were to create variables for the root module, then you could do what you want.

variable "name" {
  type        = string
  description = "Instance name."
}

variable "type" {
  type        = string
  description = "Some type."
}

Then, in the module call, you would adjust the code to something along the lines of:

module "testabc" {
 source        = "/modules/xyz"
 name          = var.name
 hostname      = "jdksnkfjsdn"
 instance_type = (var.name == "abc" || var.name == "xyz" ) && (var.type == "pqr") ? "jkl" : "some other instance type"
}

The above example is using conditional expression [1] in terraform. It is similar to ternary operators in programming languages, as if the first part of the expression evaluates to true , then the value after the ? will be assigned to the instance_type argument. If it evaluates to false , instance_type will be assigned a value after : .

In other words, if

(var.name == "abc" || var.name == "xyz" ) && (var.type == "pqr")

evaluates to true , then instance_type = "jkl" . If the above expression is false, then instance_type = "some other instance type" .


[1] https://developer.hashicorp.com/terraform/language/expressions/conditionals

I hope that you know what and why you want to achieve this. Adding multiple conditions will make your Terraform script more complicated ( read more about Cognitive Complexity ), and then you may lose the advantage of using a declarative language like HCL . Yes, you may use it in MANY cases, but in general you may want to have a predictable state that's represented by an explicit Terraform scripts.

Now, you may want to practice more with declarative languages so you may rethink your logic to achieve what you want differently than by using a "conditional default value" which is not possible with Terraform.

From your other question ( terraform modules if else condition ), I see the following:

if (stack == dev | stack == staging ) & type = xyz):
  instance_type =m5.large
else:
  instance_type= anything_else

You can make it easier to something like (in pseudo-code):

if (stack != "prod" && type == xyz):
  instance_type =m5.large
else:
  instance_type= anything_else

And this would make your module:

variable "name" {
  type        = string
  description = "Instance name."
}

variable "environment" {
  type        = string
  description = "Instance environment"
}

variable "type" {
  type        = string
  description = "Some type."
}

module "testabc" {
 source        = "/modules/xyz"
 name          = "${var.environment}-${var.name}"
 hostname      = "jdksnkfjsdn"
 instance_type = (var.environment != "prod"  && var.type == "pqr") ? "jkl" : "some other instance type"
}

Or, you can have multiple *.tfvars files where you set values for your variables, so you would have configuration like:

# dev.tfvars

name = "my dev instance name"
type = "my-dev-instance-type"

and

# prod.tfvars

name = "my production instance name"
type = "my-prod-instance-type"

Then your module would look like:

variable "name" {
  type        = string
  description = "Instance name."
}

variable "type" {
  type        = string
  description = "Some type."
}

module "testabc" {
 source        = "/modules/xyz"
 name          = var.name
 hostname      = "jdksnkfjsdn"
 instance_type = var.type
}

And when applying you pass the -env-file parameter like:

terraform apply -var-file="dev.tfvars"

If none of these approaches is not suitable for you, please refer to @Marko E's answer above since the values in the default argument should be known before , and they shouldn't refer to something else in the configuration.

Also, you may have a look at local values where you can define assign an object or result of an expression to a local variable:

variable "name" {
  type        = string
  description = "Instance name."
}

variable "type" {
  type        = string
  description = "Some type."
}

locals {
  isSomething = (var.name == "abc" || var.name == "xyz" ) && (var.type == "pqr")
}

module "testabc" {
 source        = "/modules/xyz"
 name          = var.name
 hostname      = "jdksnkfjsdn"
 instance_type = local.isSomething ? "jkl" : "some other instance type"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM