0
votes

I am using Azure provider and storing the terraform state in Azure blob storage. Using the below code snippet for this.

data "terraform_remote_state" "xxxxxx" {
  backend = "azurerm"

  config = {
    container_name       = "terraform-state"
    resource_group_name = "${var.remote_state_resource_group}"
    storage_account_name = "${var.remote_state_storage_account}"
    access_key           = "${var.remote_state_credentials}"
    key                  = "${var.cluster_name}-k8s-worker"
  }

  defaults = {}
}

If i run the above code with latest version of terraform version 0.12.x, it is failing with below error. But running the same code with 0.11.x, it is working as expected.

Error message:

    Error: Unable to find remote state

  on example2.tf line 20, in data "terraform_remote_state" "xxxxxx":
  20: data "terraform_remote_state" "xxxxxx" {

  No stored state was found for the given workspace in the given backend.

Any one faced similar issue in terraform 0.12.x with Azure blob storage.

2
You need to provide more info about the terraform code you used.Charles Xu
Any updates for your question? Does my answer help you solve it? If yes, please accept it.Charles Xu
What is the reason that you do not give any response and also do not accept?Charles Xu

2 Answers

0
votes

I think the possible reasons would be here:

  1. use the wrong storage account
  2. use the wrong container name
  3. use the wrong key

All the above reasons will cause the error you got. And the remote state works fine in terraform version 0.12.x.

0
votes

I have encountered this issue when I have one terraform configuration that stores state in azurerm and then I want to use that state in another terraform configuration as a remote azurerm data source.

Specifically the issue appears when the first configuration uses terraform workspaces. The azurerm backend silently appends a suffix of the form env:${terraform.workspace} at the end of the blob key. You must explicitly correct for this in the data source.

If the backend of the first configuration looks like this:

terraform {
    backend "azurerm" {
        resource_group_name  = "rg-myapp"
        storage_account_name = "myappterraform"
        container_name       = "tfstate"
        key                  = "myapp.tfstate"
    }
}

The data source of the second configuration must look like this:

data "terraform_remote_state" "myapp" {
    backend = "azurerm"
    config = {
        resource_group_name  = "rg-myapp"
        storage_account_name = "myappterraform"
        container_name       = "tfstate"
        key                  = "myapp.tfstateenv:${terraform.workspace}"
    }
}