-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
  }
6
  • "${var}-some-string-${var2}-some-string" Oct 13, 2021 at 11:17
  • will that work? how can I do using local?
    – cloudbud
    Oct 13, 2021 at 11:18
  • "${local.value}-some-string", yes it will work. Oct 13, 2021 at 11:19
  • can you provide me with the code?
    – cloudbud
    Oct 13, 2021 at 11:19
  • Well, I already did it. but let me post an answer then. Oct 13, 2021 at 11:21

1 Answer 1

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"
8
  • local.var will be Test and my environment will be var.env_name, right
    – cloudbud
    Oct 13, 2021 at 11:33
  • 1
    You mean "${var.value1}-${var.value2}"?
    – Marcin
    Oct 13, 2021 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. Oct 13, 2021 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. Oct 13, 2021 at 11:40
  • ${local.tenant_prefix}${var.env_name} - like this
    – cloudbud
    Oct 13, 2021 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.