Multiple Conditionals for comparison (0.12.7)

Hello,

I am having difficulties wrapping my head around doing multiple comparisons in terraform. I have 3 types of roles set in var.role: default, frontend, backend.

I have defined the following local variables that contain userdata:

locals {
# default
  eks_worker_userdata = <<USERDATA
#!/bin/bash
set -o xtrace
/etc/eks/bootstrap.sh --apiserver-endpoint '${var.eks_cluster_endpoint}' --b64-cluster-ca '${var.eks_certificate_auth}' --kubelet-extra-args '--node-labels="kiam-agent=true" --node-labels="kubernetes.io/role=default"' '${var.eks_cluster_name}'
USERDATA

# Frontend
  eks_worker_frontend_userdata = <<USERDATA
#!/bin/bash
set -o xtrace
/etc/eks/bootstrap.sh --apiserver-endpoint '${var.eks_cluster_endpoint}' --b64-cluster-ca '${var.eks_certificate_auth}' --kubelet-extra-args '--register-with-taints="frontend=true:NoSchedule" --node-labels="kiam-agent=true" --node-labels="frontend=true" --node-labels="kubernetes.io/role=frontend"' '${var.eks_cluster_name}'
USERDATA

# Backend
  eks_worker_backend_userdata = <<USERDATA
#!/bin/bash
set -o xtrace
/etc/eks/bootstrap.sh --apiserver-endpoint '${var.eks_cluster_endpoint}' --b64-cluster-ca '${var.eks_certificate_auth}' --kubelet-extra-args '--register-with-taints="backend=true:NoSchedule" --node-labels="kiam-agent=true" --node-labels="backend=true" --node-labels="kubernetes.io/role=backend"' '${var.eks_cluster_name}'
USERDATA

}

I also have a launch configuration template, but want to do something along the lines of:

resource "aws_launch_template" "eks_worker_lt" {
  user_data = if var.role == "default"; then base64encode(local.eks_worker_userdata)
 elseif
   var.role=="backend"; then base64encode(local.eks_worker_backend_userdata)
elseif
   var.role=="frontend"; then base64encode(local.eks_worker_frontend_userdata)
}

How can i write this into a HCL format?

Thank you

The most straightforward way to do something like this is to create a map and then index into it using your role string:

locals {
  userdata_by_role = {
    default  = local.eks_worker_userdata
    backend  = local.eks_worker_backend_userdata
    frontend = local.eks_worker_frontend_userdata
  }
}

resource "aws_launch_template" "eks_worker_lt" {
  # ...
  user_data = base64encode(local.userdata_by_role[var.role])
}

Since your three strings are already local values anyway, you might elect to make it more concise by just inlining them into the map definition:

locals {
  userdata_by_role = {
    default = <<-USERDATA
      #!/bin/bash
      ...
    USERDATA
    frontend = <<-USERDATA
      #!/bin/bash
      ...
    USERDATA
    backend = <<-USERDATA
      #!/bin/bash
      ...
    USERDATA
  }
}

(Note that if you use <<-USERDATA rather than just <<USERDATA then you can activate Terraform’s indented heredoc mode, where you can indent the contents of the string and Terraform will automatically remove enough leading spaces so that the least-indented lines will end up being flush with the left column. That’s often helpful for readability when multi-line strings are embedded into other constructs like this.)