bitbucket terraform pipeline failing when provisioning resource to Azure

Subhasis Tripathy
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 14, 2024

Hello,
I am deploying resource to azure from bitbucket pipeline with terraform code.
when applying terraform plan , i am getting this error.  I have no clue how to fix it.

terraform plan -out=tfplan

Planning failed. Terraform encountered an error while generating this plan.

Error: building AzureRM Client: please ensure you have installed Azure CLI version 2.0.79 or newer. Error parsing json result from the Azure CLI: launching Azure CLI: exec: "az": executable file not found in $PATH.


with provider["registry.terraform.io/hashicorp/azurerm"],

on main.tf line 14, in provider "azurerm":

14: provider "azurerm" {

FYI , I have mentioned these in variable section. still it gives me error.

variable "ARM_SUBSCRIPTION_ID" {
}
variable "ARM_CLIENT_ID" {
}  
variable "ARM_CLIENT_SECRET" {
}
variable "ARM_TENANT_ID" {
}


Please suggest me some solution

1 answer

2 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 16, 2024

Hi @Subhasis Tripathy and welcome to the community!

The error indicates that Azure CLI version 2.0.79 or newer is not installed in the build environment.

If your Pipelines builds run on Atlassian infrastructure or with a self-hosted Linux Docker runner, they use Docker containers. For every step of your build, a Docker container starts (the build container) using the image you have specified in your bitbucket-pipelines.yml file. The definition will look like this:

image: atlassian/default-image:4

and it may be defined either at the top of the file (and applies to all steps), or at a step level.

If you want to use a certain tool during your build, it either needs to be preinstalled in the Docker image you use as a build container, or you can install it during the build by adding the necessary commands in your bitbucket-pipelines.yml file in that specific step.

So, you can either use a different Docker image as a build container that has Azure CLI version 2.0.79 or newer preinstalled, or you can add commands in your yml file to install Azure CLI 2.0.79 or newer.

In case the pipelines builds run with a self-hosted Linux Shell, Windows, or MacOS runner, these runners don't use Docker containers, and the builds run directly on the host. Any tools you want to use during the build need to be installed on the host machine.

Please feel free to reach out if you have any questions!

Kind regards,
Theodora

Subhasis Tripathy
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 19, 2024

Hello Theodora,

Thanks for giving solution. But still i am facing error while running terraform plan. Here is the code I am using 
###############

image:
  name: atlassian/default-image:4

pipelines:
  default:
    - step:
        name: Terraform Plan
        script:
          - pipe: microsoft/azure-cli-run:1.1.0
            variables:
              AZURE_APP_ID: $AZURE_APP_ID
              AZURE_PASSWORD: $AZURE_PASSWORD
              AZURE_TENANT_ID: $AZURE_TENANT_ID
              CLI_COMMAND: 'az account show'
              AZ_CLI_VERSION: '2.0.79'


    - step:
        name: Terraform Plan
        image: hashicorp/terraform:light
        script:
          - export TF_VAR_azure_subscription_id=$ARM_SUBSCRIPTION_ID
          - export TF_VAR_azure_client_id=$AZURE_APP_ID
          - export TF_VAR_azure_client_secret=$AZURE_PASSWORD
          - export TF_VAR_azure_tenant_id=$ARM_TENANT_ID
          - terraform init
          - terraform validate
          - terraform plan
###################
It successfully completes 1 st step, but in step 2 error 

#
terraform plan

Planning failed. Terraform encountered an error while generating this plan.

Error: building AzureRM Client: please ensure you have installed Azure CLI version 2.0.79 or newer. Error parsing json result from the Azure CLI: launching Azure CLI: exec: "az": executable file not found in $PATH.

with provider["registry.terraform.io/hashicorp/azurerm"],

on main.tf line 14, in provider "azurerm":

14: provider "azurerm" {


Please help me to fix this problem. Thanks!!

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 20, 2024

Hi @Subhasis Tripathy,

The first step of your pipeline runs on a separate Docker container and it uses the pipe https://bitbucket.org/microsoft/azure-cli-run/src/master/. Please note that AZ_CLI_VERSION is not a valid variable for the pipe and this step won't have any effect on the second step.

The second step that fails is running on a Docker container based on the image hashicorp/terraform:light and this Docker image doesn't have Azure CLI preinstalled.

This Docker image is using Alpine Linux v3.17 and I don't see any details on how to install Azure CLI on Alpine Linux in Azure's official documentation.

I searched online and the following answer to a StackOverflow post was helpful:

I was able to install the latest version of Azzure CLI in a step using hashicorp/terraform:light as follows:

- step:
image: hashicorp/terraform:light
script:
- apk add --no-cache --update python3 py3-pip
- apk add --no-cache --update --virtual=build gcc musl-dev python3-dev libffi-dev openssl-dev cargo make
- pip3 install --no-cache-dir --prefer-binary azure-cli
- apk del build
- az --version

The last command is to check the version and ensure that Azure CLI is installed.

Can you try adding these commands at the beginning of the second step and let me know how it goes?

If you are familiar with Docker, another option would be to build your own custom Docker image, with base image hashicorp/terraform:light, install Azure CLI on that image, and then use it as a build container. You can find more info on building your own custom image here:

Kind regards,
Theodora

Like Sabine Mayer likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events