Azure _ Create a temporary password for Terraform Cloud

Posted By : Anshuman Abhishek | 26-Nov-2020

In AWS and GCP there are options to create temporary credentials/password which can be used in terraform cloud. But in Azure, this temporary credentials/keys feature is not available.

 

But we create a temporary Service Principal or Managed Service Identity using a bash script. Let’s see how

 

Also Read: Cloud Providers Comparision AWS Vs Google Vs Azure

 

  1. Install Terraform

 

Run the below commands to install

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

sudo apt-get update && sudo apt-get install terraform

 

  1. Install Azure CLI

 

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

 

Then log in to your Azure account using 

az login


 

  1. Create a Bash Script

         

#!/usr/bin/env bash

 

# Load config file and common packages file

. variables

 

#check variables are set or not

if [ -v $USER ] || [ -v $PASSWORD ] || [ -v $ARM_SUBSCRIPTION_ID ] || [ -v $EXPIRYTIME ]; then

echo "Set All Values in Config file: EXITING"

exit 1

fi

 

#To login into main account

az login -u $USER -p $PASSWORD

 

#This will create a user

values=$(az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/$ARM_SUBSCRIPTION_ID")

 

#Parse JSON Values

ARM_CLIENT_ID=`echo $values | jq '.appId'`

ARM_CLIENT_SECRET=`echo $values | jq '.password'`

ARM_TENANT_ID=`echo $values | jq '.tenant'`

 

#Remove double quotes

ARM_CLIENT_ID=$(eval echo $ARM_CLIENT_ID)

ARM_CLIENT_SECRET=$(eval echo $ARM_CLIENT_SECRET)

ARM_TENANT_ID=$(eval echo $ARM_TENANT_ID)

 

#Set time

dateformat='date -d "+ '${EXPIRYTIME}' minutes" +"%Y-%m-%dT%H-%M-%S+00:00"'

expiry=$(eval "${dateformat}")

echo $expiry

#Set expiry

az ad app credential reset --id $ARM_CLIENT_ID --password $ARM_CLIENT_SECRET --end-date $expiry

 

#To check the expiry date and time uncomment the below line

#az ad app credential list --id $ARM_CLIENT_ID

 

#Export env variables

export ARM_CLIENT_ID

export ARM_CLIENT_SECRET

export ARM_SUBSCRIPTION_ID

export ARM_TENANT_ID



 

 

  1. Set variables

#Time in minutes

USER=''"
PASSWORD=''"
ARM_SUBSCRIPTION_ID=""
EXPIRYTIME=30

  1. Run Script

bash scriptname.sh

#it will generate the required keys
 

  1. Check credentials

We can verify the credentials using below commands

 

az login --service-principal -u CLIENT_ID -p CLIENT_SECRET --tenant TENANT_ID
az vm list-sizes --location westus
az account list-locations
az logout

 

 We have vast experience in cloud computing services and we let you seamlessly deploy your Java to other applications on platforms like AWS, Azure, and Google Cloud.

 

About Author

Author Image
Anshuman Abhishek

He has experience in Linux, Kubernetes, and different open source application. He has also worked on different clouds like IBM and AWS.

Request for Proposal

Name is required

Comment is required

Sending message..