Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate two variables in terraform

Tags:

terraform

i'm trying to create a kubernetes cluster from kops using terraform,following code is a part of infrastructure, i'm trying to create the name concatenate with two variables, and i'm getting illegal char error line two, error happens because im trying to define the name with concatenate variables. is it possible in terraform?

resource "aws_autoscaling_group" "master-kubernetes" {
  name                 = "master-"${var.zone}".masters."${var.cluster_name}""
  launch_configuration = "${aws_launch_configuration.master-kubernetes.id}"
  max_size             = 1
  min_size             = 1
  vpc_zone_identifier  = ["${aws_subnet.subnet-kubernetes.id}"]
like image 446
Frodo Avatar asked Sep 14 '18 12:09

Frodo


3 Answers

Try this:

resource "aws_autoscaling_group" "master-kubernetes" {
  name = "master-${var.zone}.masters.${var.cluster_name}"
  # ... other params ...
}
like image 59
KJH Avatar answered Oct 16 '22 22:10

KJH


With latest terraform 0.12.x terraform format doc , you could do better like:

resource "aws_autoscaling_group" "master-kubernetes" {
    name = format("master-%s.masters.%s", var.zone, var.cluster_name)
}
like image 23
Jay Avatar answered Oct 16 '22 23:10

Jay


I would say rather concatenating at resource level use the locals first define a variable in locals and then you can utilize it at resource level

Locals declaration

locals {
  rds_instance_name = "${var.env}-${var.rds_name}"
}

Resource Level declaration

resource "aws_db_instance" "default_mssql" {
  count                     = var.db_create ? 1 : 0
  name                      = local.rds_instance_name
........
}

This is as simple as its need to be ....

like image 1
Mansur Ul Hasan Avatar answered Oct 17 '22 00:10

Mansur Ul Hasan