microsoft / azure-pipelines-terraform

Azure Pipelines tasks for installing Terraform and running Terraform commands in a build or release pipeline.
MIT License
95 stars 60 forks source link

terraform show errors with a "could not load schema" #127

Closed UppyAU closed 1 year ago

UppyAU commented 1 year ago

Hello, I am having an issue with the terraform show command on an outputted tfplan. The tfplan will save fine and publish to the artefacts. I have also managed to run all the commands locally without any issue using the same statefile.

The pipeline is running on an Microsoft provided cloud agent running ubuntu.

Below are my task for show and my error. Any suggestions would be appreciated.

My YAML Pipeline Task

   - task: TerraformTaskV3@3
        displayName: 'Terraform Show - Human Readable'
        inputs:
          provider: 'azurerm'
          command: 'show'
          commandOptions: '$(System.DefaultWorkingDirectory)/terraform/tfoutput/azuremonitor1p-tf.tfplan'
          outputTo: 'file'
          fileName: '$(System.DefaultWorkingDirectory)/terraform/tfoutput/azuremonitor1p-tf.ansi'
          environmentServiceNameAzureRM: '$(EnvServiceConnectionID)'

Error within the pipeline

│ Error: Failed to load plugin schemas
│ 
│ Error while loading schemas for plugin components: Failed to obtain
│ provider schema: Could not load the schema for provider
│ registry.terraform.io/hashicorp/azurerm: failed to instantiate provider
│ "registry.terraform.io/hashicorp/azurerm" to obtain schema: unavailable
│ provider "registry.terraform.io/hashicorp/azurerm"..
mericstam commented 1 year ago

Which version of terraform do you use?

UppyAU commented 1 year ago

Using Terraform v1.3.7

mericstam commented 1 year ago

what does you terraform look like? especially the top of the file like:

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "=3.0.1" } } }

provider "azurerm" { features {} }

UppyAU commented 1 year ago

The pipeline also pulls in two repos. A main and a modules.

terraform {
  required_version = ">=0.13"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=3.40.0"
    }
  }
}

provider "azurerm" {
  alias = "core"

  subscription_id = var.CORE_SUB_ID
  tenant_id       = var.TENANT_ID
  client_id       = var.CORE_CLIENT_ID
  client_secret   = var.CORE_CLIENT_SECRET

  features {}
}

provider "azurerm" {
  alias = "prod"

  subscription_id = var.PROD_SUB_ID
  tenant_id       = var.TENANT_ID
  client_id       = var.PROD_CLIENT_ID
  client_secret   = var.PROD_CLIENT_SECRET

  features {}
}

provider "azurerm" {
  alias = "dev"

  subscription_id = var.DEV_SUB_ID
  tenant_id       = var.TENANT_ID
  client_id       = var.DEV_CLIENT_ID
  client_secret   = var.DEV_CLIENT_SECRET

  features {}
}
mericstam commented 1 year ago

It is working locally but fails in the pipeline? Has it ever worked in the pipeline or is this your first attempt and it fails?

UppyAU commented 1 year ago

It worked when it was a mono repo. When I moved the modules out it seems to no longer work. It is still generating a successful plan

UppyAU commented 1 year ago

Wondering if it has something to do with expected paths changing when you checkout multiple repos 🤔

mericstam commented 1 year ago

not sure, it might. so modules will end up in ex. c:\agent_work\s\modules and the rest under c:\agent_work\s\main But you should have gotten other path errors as well I would imagine?

UppyAU commented 1 year ago

I'm defining the path for the modules so they end up as (terraform/modules-Tf/) folder with the main repo in the terraform folder

UppyAU commented 1 year ago
Starting: tf show
==============================================================================
Task         : Terraform
Description  : Execute terraform commands to manage resources on AzureRM, Amazon Web Services(AWS) and Google Cloud Platform(GCP)
Version      : 3.209.19
Author       : Microsoft Corporation
Help         : [Learn more about this task](https://aka.ms/AAf0uqr)
==============================================================================
/opt/hostedtoolcache/terraform/1.3.7/x64/terraform show /home/vsts/work/1/s/terraform/tfoutput/azuremonitor1p.tfplan
2023-01-22T12:31:39.737Z [INFO]  Terraform version: 1.3.7
2023-01-22T12:31:39.737Z [DEBUG] using github.com/hashicorp/go-tfe v1.9.0
2023-01-22T12:31:39.737Z [DEBUG] using github.com/hashicorp/hcl/v2 v2.15.0
2023-01-22T12:31:39.737Z [DEBUG] using github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2
2023-01-22T12:31:39.737Z [DEBUG] using github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
2023-01-22T12:31:39.737Z [DEBUG] using github.com/zclconf/go-cty v1.12.1
2023-01-22T12:31:39.737Z [INFO]  Go runtime version: go1.19.4
2023-01-22T12:31:39.737Z [INFO]  CLI args: []string{"/opt/hostedtoolcache/terraform/1.3.7/x64/terraform", "show", "/home/vsts/work/1/s/terraform/tfoutput/azuremonitor1p.tfplan"}
2023-01-22T12:31:39.737Z [TRACE] Stdout is not a terminal
2023-01-22T12:31:39.737Z [TRACE] Stderr is not a terminal
2023-01-22T12:31:39.737Z [TRACE] Stdin is not a terminal
2023-01-22T12:31:39.737Z [DEBUG] Attempting to open CLI config file: /home/vsts/.terraformrc
2023-01-22T12:31:39.737Z [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2023-01-22T12:31:39.737Z [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2023-01-22T12:31:39.737Z [DEBUG] ignoring non-existing provider search directory /home/vsts/.terraform.d/plugins
2023-01-22T12:31:39.737Z [DEBUG] ignoring non-existing provider search directory /home/vsts/.local/share/terraform/plugins
2023-01-22T12:31:39.737Z [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2023-01-22T12:31:39.737Z [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2023-01-22T12:31:39.738Z [INFO]  CLI command args: []string{"show", "/home/vsts/work/1/s/terraform/tfoutput/azuremonitor1p.tfplan"}
2023-01-22T12:31:39.765Z [DEBUG] checking for provisioner in "."
2023-01-22T12:31:39.765Z [DEBUG] checking for provisioner in "/opt/hostedtoolcache/terraform/1.3.7/x64"
2023-01-22T12:31:39.765Z [TRACE] terraform.NewContext: starting
2023-01-22T12:31:39.765Z [TRACE] terraform.NewContext: complete
2023-01-22T12:31:39.765Z [TRACE] LoadSchemas: retrieving schema for provider type "registry.terraform.io/hashicorp/azurerm"
2023-01-22T12:31:39.765Z [TRACE] terraform.contextPlugins: Initializing provider "registry.terraform.io/hashicorp/azurerm" to read its schema
╷
│ Error: Failed to load plugin schemas
│ 
│ Error while loading schemas for plugin components: Failed to obtain
│ provider schema: Could not load the schema for provider
│ registry.terraform.io/hashicorp/azurerm: failed to instantiate provider
│ "registry.terraform.io/hashicorp/azurerm" to obtain schema: unavailable
│ provider "registry.terraform.io/hashicorp/azurerm"..
╵
Finishing: tf show
UppyAU commented 1 year ago

I have managed to resolve this by declaring a workingDirectory for the show task.

      - task: TerraformTaskV3@3
        displayName: 'tf show'
        inputs:
          provider: 'azurerm'
          command: 'show'
          commandOptions: 'tfoutput/azuremonitor1p.tfplan'
          outputTo: 'file'
          fileName: '$(System.DefaultWorkingDirectory)/terraform/tfoutput/azuremonitor1ptf.ansi'
          workingDirectory: '$(System.DefaultWorkingDirectory)/terraform/'
          environmentServiceNameAzureRM: '$(EnvServiceConnectionID)'