Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

terraform0.11 - How do you do simple string concatenation in Terraform?

I must be being incredibly stupid but I can't figure out how to do simple string concatenation in Terraform.

I have the following data null_data_source:

data "null_data_source" "api_gw_url" {
    inputs = {
      main_api_gw = "app.api.${var.env_name == "prod" ? "" : var.env_name}mydomain.com"
    }
}

So when env_name="prod" I want the output app.api.mydomain.com and for anything else - let's say env_name="staging" I want app.api.staging.mydomain.com.

But the above will output app.api.stagingmydomain.com <-- notice the missing dot after staging.

I tried concating the "." if the env_name was anything but "prod" but Terraform errors:

data "null_data_source" "api_gw_url" {
    inputs = {
      main_api_gw = "app.api.${var.env_name == "prod" ? "" : var.env_name + "."}mydomain.com"
    }
}

The error is __builtin_StringToInt: strconv.ParseInt: parsing ""

The concat() function in TF appears to be for lists not strings.

So as the title says: How do you do simple string concatenation in Terraform?

I can't believe I'm asking how to concat 2 strings together XD

Update:

For anyone that has a similar issue I did this horrific workaround for the time being:

main_api_gw = "app.api.${var.env_name == "prod" ? "" : var.env_name}${var.env_name == "prod" ? "" : "."}mydomain.com"

question from:https://stackoverflow.com/questions/55312783/how-do-you-do-simple-string-concatenation-in-terraform

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try Below data resource :

data "null_data_source" "api_gw_url" {
    inputs = {
      main_api_gw = "app.api${var.env_name == "prod" ? "." : ".${var.env_name}."}mydomain.com"
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...