Azure Service Principal usage in Jenkinsfile for Terraform Init

Hello,

I’m new to Terraform and i’m trying to use a Jenkins pipeline with the Azure Principal configured in Jenkins Credentials, while i’m able to run the ‘az login’ command with variables directly into terraform, i’m trying to automate the process using jenkins. Jenkins does have the ability to store securely a SP but while trying to create a jenkinsfile to initiate terraform, i’m having problems. The code is below:

stage(‘Terraform Init’){
steps {
withCredentials([azureServicePrincipal(‘CredentialID’)]) {
echo “Initializing Terraform and export variables”
sh ‘terraform init -upgrade -input=false -reconfigure’
echo “Login into Azure”
sh(‘az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID’)
}
}

The error message from jenkins console is:

17:37:34 Terraform has been successfully initialized!
17:37:34 You may now begin working with Terraform. Try running “terraform plan” to see
17:37:34 any changes that are required for your infrastructure. All Terraform commands
17:37:34 should now work.
17:37:34 If you ever set or change modules or backend configuration for Terraform, 17:37:34 rerun this command to reinitialize your working directory. If you forget, other
17:37:34 commands will detect it and remind you to do so if necessary.e[0m 17:37:34 [Pipeline] echo
17:37:34 Login into Azure
17:37:34 [Pipeline] sh
17:37:35 + az login --service-principal -u **** -p **** --tenant ****
17:37:35 Access is denied.

Assistance is appreciated.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.