Answers: <p>This looks like a case for a conditional resource.</p>
  <p>First, look up the secrets with a <a href="https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/key_vault_secrets" rel="nofollow noreferrer"><code>data &quot;azurerm_key_vault_secrets&quot;</code></a> block:</p>
  <pre><code>data &quot;azurerm_key_vault_secrets&quot; &quot;example&quot; {
    key_vault_id = data.azurerm_key_vault.keyvault.id
  }
  </code></pre>
  <p>and then a condition on the lookup:</p>
  <pre><code>data &quot;azure_rm_key_vault_secret&quot; &quot;win_admin_pass&quot; {
    count        = contains(data.azurerm_key_vault_secrets.win_admin_pass.names, var.secret_name) ? 1 : 0
    name         = var.secret_name
    key_vault_id = data.azurerm_key_vault.keyvault.id
    
  }
  </code></pre>
  <p>If the secret doesn't exist, the data will look up zero resources.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-07-15T20:10:53.903Z
Answers: Not work in template, getting,<inappropriate value for attribute string required vars: elements example : String required> eoor.
Answers: Nullpointer
Answers: CC BY-SA 4.0
Answers: 2022-07-14T05:19:22.137Z
Answers: 2022-07-15T17:59:18.370Z
Answers: 2022-07-15T17:59:18.370Z
Answers: Bruce Becker
Body: <p>I am using below terraform code for fetch azure secret and this is working fine when secret is exist in the azure KV.</p>
  <p>Getting error when secret is not available in KV.</p>
  <pre><code>data &quot;azurerm_key_vault_secret&quot; &quot;win_admin_pass&quot; {
      name         = &quot;${var.secret_name}&quot;
      key_vault_id = &quot;${data.azurerm_key_vault.keyvault.id}&quot;
  }
  </code></pre>
  <p>In my case, this secret may available or may not available.</p>
  <p>How can we ignore error for this particular task when secret not available, or how can we check if secret exist or not, based on this condition we can fetch and ignore set of code?</p>
  
Tags: <terraform><azure><cloud><terraform-module><secret-management>
Title: How to fetch azure secret if exist in KV using terraform
UserDisplayName: Nullpointer
ThreadText: # How to fetch azure secret if exist in KV using terraform
  
  (Asked by: username_0 on 2022-07-12)
  
  I am using below terraform code for fetch azure secret and this is working fine when secret is exist in the azure KV.
  
  Getting error when secret is not available in KV.
  
  ```
  data "azurerm_key_vault_secret" "win_admin_pass" {
      name         = "${var.secret_name}"
      key_vault_id = "${data.azurerm_key_vault.keyvault.id}"
  }
  
  ```
  
  In my case, this secret may available or may not available.
  
  How can we ignore error for this particular task when secret not available, or how can we check if secret exist or not, based on this condition we can fetch and ignore set of code?
  
  **Tags:** terraform, azure, cloud, terraform-module, secret-management
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-07-14. Score: 1
  
  This looks like a case for a conditional resource.
  
  First, look up the secrets with a [`data "azurerm_key_vault_secrets"`](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/key_vault_secrets) block:
  
  ```
  data "azurerm_key_vault_secrets" "example" {
    key_vault_id = data.azurerm_key_vault.keyvault.id
  }
  
  ```
  
  and then a condition on the lookup:
  
  ```
  data "azure_rm_key_vault_secret" "win_admin_pass" {
    count        = contains(data.azurerm_key_vault_secrets.win_admin_pass.names, var.secret_name) ? 1 : 0
    name         = var.secret_name
    key_vault_id = data.azurerm_key_vault.keyvault.id
    
  }
  
  ```
  
  If the secret doesn't exist, the data will look up zero resources.
  
  > **Comments:**
  > **username_0**: Not work in template, getting,<inappropriate value for attribute string required vars: elements example : String required> eoor.  
  
  ---
  
  