> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ahmadraza.in/llms.txt
> Use this file to discover all available pages before exploring further.

# AZ Key Vault

## **1. Log in to Azure**

First, authenticate your local machine with Azure:

```sh theme={null}
az login
```

If you have multiple subscriptions, set the one you want to use:

```sh theme={null}
az account set --subscription "<your-subscription-id>"
```

***

## **2. Create an Azure Key Vault**

Run the following command to create a Key Vault in a resource group:

```sh theme={null}
az keyvault create --name demo-vault --resource-group <your-resource-group> --location <your-region>
```

🔹 Replace `<your-resource-group>` with your existing resource group. If you don’t have one, create it:

```sh theme={null}
az group create --name demo-rg --location eastus
```

Then, create the Key Vault:

```sh theme={null}
az keyvault create --name demo-vault --resource-group demo-rg --location eastus
```

***

## **3. Store a Secret in Key Vault**

Let's add a secret named **`demo-app`** with a sample value:

```sh theme={null}
az keyvault secret set --vault-name demo-vault --name demo-app --value "super-secret-value"
```

You can add more secrets:

```sh theme={null}
az keyvault secret set --vault-name demo-vault --name database-password --value "mypassword123"
```

***

## **4. Retrieve the Secret**

Now, fetch the secret value:

```sh theme={null}
az keyvault secret show --vault-name demo-vault --name demo-app --query value -o tsv
```

🔹 Expected output:

```
super-secret-value
```

If you want to retrieve and export it into a `.env` file:

```sh theme={null}
echo "demo_app=$(az keyvault secret show --vault-name demo-vault --name demo-app --query value -o tsv)" > .env
```

For multiple secrets:

```sh theme={null}
secrets=("demo-app" "database-password")

for secret in "${secrets[@]}"; do
    value=$(az keyvault secret show --vault-name demo-vault --name "$secret" --query value -o tsv)
    echo "$secret=$value"
done > .env
```

***

## **5. Verify `.env` File**

```sh theme={null}
cat .env
```

Expected output:

```
demo-app=super-secret-value
database-password=mypassword123
```

***

## **6. Grant Access to Your Machine (If Needed)**

If you get a **permission error**, give your account access:

```sh theme={null}
az keyvault set-policy --name demo-vault --upn <your-email> --secret-permissions get list
```

Or for a **Service Principal**:

```sh theme={null}
az keyvault set-policy --name demo-vault --spn <your-client-id> --secret-permissions get list
```

***

## **7. Cleanup (Optional)**

If you want to delete the Key Vault:

```sh theme={null}
az keyvault delete --name demo-vault --resource-group demo-rg
```

***

### **Summary**

| Step                  | Command                                                                                                          |
| --------------------- | ---------------------------------------------------------------------------------------------------------------- |
| Log in                | `az login`                                                                                                       |
| Create Resource Group | `az group create --name demo-rg --location eastus`                                                               |
| Create Key Vault      | `az keyvault create --name demo-vault --resource-group demo-rg --location eastus`                                |
| Store Secret          | `az keyvault secret set --vault-name demo-vault --name demo-app --value "super-secret-value"`                    |
| Retrieve Secret       | `az keyvault secret show --vault-name demo-vault --name demo-app --query value -o tsv`                           |
| Export to `.env`      | `echo "demo_app=$(az keyvault secret show --vault-name demo-vault --name demo-app --query value -o tsv)" > .env` |

This should get everything working smoothly. Let me know if you face any issues!
