Pallab
Pallab

Reputation: 2333

How to use format function in Terraform

I am trying to register an application in Azure AD using TF and i would like to define some redirect URIs using the arg as mentioned in the link --> https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application :

reply_urls                 = ["https://replyurl"]

But i have to put 3 URIs and the format of the URI is like this below :

https://(project code)-yellow-(resourcegroup).azurewebsites.net/oauth2-redirect.html
https://(project code)-blue-(resourcegroup).azurewebsites.net/oauth2-redirect.html
https://(project code)-green-(resourcegroup).azurewebsites.net/oauth2-redirect.html

So the second part is changing only and the other part is constant. We cannot hardcode URIs for best practices Also, we need to use format function So can anyone tell how to put the reply_urls using the format function so that only the middle part changes as described above Also, can this be done in any way using concat for that matter

Upvotes: 1

Views: 2211

Answers (1)

Marcin
Marcin

Reputation: 238687

You can pass the project_id and resourcegroup as input parameters to your TF code:

variable "project_id" {}
variable "resource_group" {}

Then your reply_urls could be:

reply_urls  = [
       format("https://%s-yellow-%s.azurewebsites.net/oauth2-redirect.html", var.project_id, var.resource_group),
       format("https://%s-blue-%s.azurewebsites.net/oauth2-redirect.html", var.project_id, var.resource_group),
       format("https://%s-green-%s.azurewebsites.net/oauth2-redirect.html", var.project_id, var.resource_group)  
]

Upvotes: 1

Related Questions