0
votes

I am trying to create AKS by using terraform, In service principle block we need to pass client_id and client_secret. Terraform has the ability to read env variables and source them if they are prepended as TF_VAR_name.

Terraform also mentioned that for provider block we can export the client related variables as ARM_CLIENT_name. So my question is how to use those ARM variables for provisioning my AKS.

Right now I am doing like this


  - export ARM_CLIENT_ID=$AZ_USERNAME
  - export ARM_CLIENT_SECRET=$AZ_PASSWORD
  - export ARM_TENANT_ID=$AZ_TENANT
  - export ARM_SUBSCRIPTION_ID=AZ_SUBSCRIPTION_ID

If I can't refer to above env variables then I should do

 - export ARM_CLIENT_ID=$AZ_USERNAME
 - export TF_VAR_client_id=$AZ_PASSWORD
 - export ARM_CLIENT_SECRET=$AZ_PASSWORD
 - export TF_VAR_client_secret=$AZ_PASSWORD
 - export ARM_TENANT_ID=$AZ_TENANT
 - export ARM_SUBSCRIPTION_ID=AZ_SUBSCRIPTION_ID
1
Any more questions? Does it solve your problem? If yes, please accept it.Charles Xu

1 Answers

2
votes

What you display in the question are two different situations.

One is that the authentications for Azure providers. It can quote the necessary input from the environment variables such as ARM_CLIENT_ID=$AZ_USERNAME, ARM_CLIENT_SECRET=$AZ_PASSWORD, ARM_TENANT_ID and ARM_SUBSCRIPTION_ID.

Another one is to quote the normal variables. You can export all the variables you need as environment variables with the prefix TF_VAR_, but another thing you need to do is that you also need to define the variables inside the Terraform file. As it shows here:

As a fallback for the other ways of defining variables, Terraform searches the environment of its own process for environment variables named TF_VAR_ followed by the name of a declared variable.

For example, if you want to quote the variable aksname from the environment variable, then you need to do two things:

  1. export the environment variable aksname with a prefix TF_VAR_:

export TF_VAR_aksname=example-aks

  1. define the variable aksname inside the Terraform file and quote it, here I just quote it in the output block:
variable "aksname" {}

output "aks_name" {
  value = "${var.aksname}"
}

Then the output will like this:

enter image description here