-1

How can I concatenate string and a variable in terraform. I am using terraform version 1.7

Name = "Test (Environment_Name)" where environment_name will be test,stage and prod.

    resource "fusionauth_tenant" "tanant" {
  name = "Test (Environment_name)"
  email_configuration {
    default_from_name                 = "FusionAuth [Environment_name]"
    verification_email_template_id    = fusionauth_email.verification_template.id
  }
cloudbud
  • 2,948
  • 5
  • 28
  • 54

1 Answers1

5

Examples of how to append a string and a variable.

settings.tf:

locals {
  bucket_prefix = "test-bucket"
}

And then you want to create three S3 buckets.

s3.tf:

resource "aws_s3_bucket" "a" {
  bucket = "${local.bucket_prefix}-app"
}
//name = test-bucket-app


resource "aws_s3_bucket" "b" {
  bucket = local.bucket_prefix
}
//name = test-bucket


resource "aws_s3_bucket" "c" {
  bucket = "my-bucket"
}
//name = my-bucket

If you want to append a variable from var or get a name from a resource, it will follow the same pattern. It will always be:

"${var.name.value}-my-string"
Juan Fontes
  • 738
  • 3
  • 16
  • local.var will be `Test` and my environment will be var.env_name, right – cloudbud Oct 13 '21 at 11:33
  • 1
    You mean `"${var.value1}-${var.value2}"`? – Marcin Oct 13 '21 at 11:37
  • 1
    Yeah, I posted `value` but I did change it to make it easy to understand the concept of using two vars. I've deleted the comment, thanks @Marcin. – Juan Fontes Oct 13 '21 at 11:38
  • By the way, no matter if you are using variables, locals, or resources attributes, the rule is the same, if you need to append it to a string, you need to put it as I've posted in the answer. – Juan Fontes Oct 13 '21 at 11:40
  • ${local.tenant_prefix}${var.env_name} - like this – cloudbud Oct 13 '21 at 12:09
  • how can I add spaces in between the two vars – cloudbud Oct 13 '21 at 12:19
  • `"${local.tenant_prefix} ${var.env_name}"` -- just it. -- But you need to know if the resource that you are creating, accepts space in its attribute, otherwise it will fail. – Juan Fontes Oct 13 '21 at 12:20
  • can you help me with another query regarding templates in terraform posted in a new question https://stackoverflow.com/questions/69555659/how-can-i-variablize-a-template-and-text-file-in-terraform – cloudbud Oct 13 '21 at 12:33