Skip to content

Commit af1e58b

Browse files
committed
progress
1 parent 20d1f7b commit af1e58b

File tree

1 file changed

+86
-147
lines changed

1 file changed

+86
-147
lines changed

articles/azure-developer-cli/manage-environment-variables.md

Lines changed: 86 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ The Azure Developer CLI (`azd`) uses environment variables to store and manage c
1717

1818
In the context of the Azure Developer CLI, environment variables are key-value pairs that are tied to specific named environments like *dev*, *test*, or *prod*. Each `azd` environment maintains its own set of environment variables, allowing you to configure different settings for different deployment targets.
1919

20-
### What are environment variables
20+
Environment variables in `azd` are configuration settings stored in `.env` files within your environment folders in the `.azure` folder. They serve as inputs to:
2121

22-
Environment variables in `azd` are configuration settings stored in `.env` files within your environment folders. They serve as inputs to:
23-
24-
- Infrastructure provisioning processes
2522
- Application deployment workflows
26-
- Runtime configurations for Azure services
27-
- Feature flags and connection parameters
23+
- Configurations for Azure services and connections
24+
- Infrastructure provisioning processes
2825

2926
Unlike traditional environment variables that exist at the operating system level, `azd` environment variables are scoped to specific environments within your project, providing isolation between different deployment targets.
3027

@@ -42,105 +39,40 @@ Each `azd` environment has its own set of variables, allowing for environment-sp
4239

4340
The `azd` environment variables are stored in `.env` files within the environment-specific directories of your project. When you create an environment using `azd env new <name>`, a directory structure is created:
4441

45-
```text
42+
```txt
4643
.azure/
4744
├── <environment-name>/
4845
│ ├── .env # Environment variables for this environment
49-
│ └── main.parameters.json # Infrastructure parameters
5046
```
5147

5248
The `.env` file uses a standard format where each line represents a key-value pair:
5349

54-
```text
50+
```txt
5551
KEY1=value1
5652
KEY2=value2
5753
```
5854

5955
> [!TIP]
6056
> Visit the [Working with environments](work-with-environments.md) article for more information about `azd` environments.
6157
62-
When you run `azd` commands targeting a specific environment, the CLI automatically loads variables from that environment's `.env` file.
58+
When you run `azd` commands such as `azd up`, the CLI automatically loads variables from the select environment's `.env` file.
6359

6460
These variables influence:
6561

6662
- **Infrastructure provisioning**: Variables like `AZURE_LOCATION` and `AZURE_SUBSCRIPTION_ID` determine where and how resources are created.
67-
- **Deployment**: Variables like service endpoints and connection strings control how your application connects to Azure services.
68-
- **Application configuration**: Variables can be passed to your application to control its behavior.
69-
- **Resource naming**: Variables like `RESOURCE_TOKEN` ensure consistent resource naming patterns.
70-
71-
The `.env` file is also updated automatically during operations like `azd init`, `azd provision`, and `azd deploy`, capturing outputs from your infrastructure templates and storing them for future use.
72-
73-
## AZD vs OS environment variables
74-
75-
`azd` environment variables and operating system environment variables serve different purposes and work in different ways:
76-
77-
| Concept | Azure Developer CLI | Operating system |
78-
|---------|------------------------------------------|--------------------------|
79-
| Location | Stored in `.azure/<env-name>/.env` files | Set in your operating system environment |
80-
| Scope | Scoped to a specific named environment within a project | Global to your user session or system |
81-
| Management | Managed using `azd env` commands | Managed using OS-specific commands (`export`, `set`, etc.) |
82-
| Access | Loaded automatically by `azd` commands | Typically loaded explicitly in scripts or applications |
83-
| Target | Tied to Azure resources and deployments | General purpose system configuration |
84-
| Lifecycle | Persist between terminal sessions | May be temporary or persistent depending on how they're set |
85-
86-
`azd` doesn't automatically read or write OS environment variables. However, you can interact with both types of variables using custom scripts:
87-
88-
**Reading OS environment variables in scripts:**
89-
90-
**Bash:**
91-
92-
```bash
93-
# Access OS environment variable
94-
echo "OS variable: $PATH"
63+
- **Deployment**: Variables like service endpoints control how your application connects to Azure services.
64+
- **Application configuration**: Variables can be passed to your application configuration to control its behavior.
65+
- **Resource naming**: Variables like `AZURE_RESOURCE_GROUP` influence resource naming patterns.
9566

96-
# Access azd environment variable
97-
echo "AZD variable: $(azd env get-value MY_VARIABLE)"
98-
```
99-
100-
**PowerShell:**
101-
102-
```powershell
103-
# Access OS environment variable
104-
Write-Host "OS variable: $env:PATH"
105-
106-
# Access azd environment variable
107-
Write-Host "AZD variable: $(azd env get-value MY_VARIABLE)"
108-
```
109-
110-
**Convert azd environment variables to OS environment variables:**
111-
112-
**Bash:**
113-
114-
```bash
115-
# Load all azd environment variables into the current shell session
116-
while IFS='=' read -r key value; do
117-
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//')
118-
export "$key=$value"
119-
done <<EOF
120-
$(azd env get-values)
121-
EOF
122-
```
123-
124-
**PowerShell:**
125-
126-
```powershell
127-
# Load all azd environment variables into the current PowerShell session
128-
foreach ($line in (& azd env get-values)) {
129-
if ($line -match "([^=]+)=(.*)") {
130-
$key = $matches[1]
131-
$value = $matches[2] -replace '^"|"$'
132-
$env:$key = $value
133-
}
134-
}
135-
```
67+
The `.env` file is also updated automatically by `azd` during operations like `azd init`, `azd provision`, and `azd deploy`, capturing outputs from your infrastructure templates and storing them for future use.
13668

13769
## Set Environment Variables
13870

13971
You can use different methods to set `azd` environment variables, depending on the scenario.
14072

14173
### CLI commands
14274

143-
The most direct way to set an environment variable is using the `azd env set` command:
75+
The recommended way to set an environment variable is using the `azd env set` command, which includes checks to ensure valid values:
14476

14577
```azdeveloper
14678
azd env set <key> <value>
@@ -228,7 +160,7 @@ For machine-readable output (useful in scripts):
228160
azd env get-values --output json
229161
```
230162

231-
### Bicep files
163+
### Pass environment variables to Bicep files
232164

233165
Environment variables can be referenced in Bicep parameter files (`main.parameters.json`) using a special substitution syntax:
234166

@@ -253,7 +185,7 @@ When `azd` processes this file during provisioning, it automatically substitutes
253185

254186
In custom scripts and [hooks](azd-extensibility.md) defined in your `azure.yaml` file, you can access environment variables using the `azd env get-values` command:
255187

256-
**Bash:**
188+
### [Bash](#tab/bash)
257189

258190
```bash
259191
#!/bin/bash
@@ -270,7 +202,7 @@ EOF
270202
echo "API endpoint: $API_ENDPOINT"
271203
```
272204

273-
**PowerShell:**
205+
### [PowerShell](#tab/powershell)
274206

275207
```powershell
276208
Write-Host "Loading azd .env file from current environment"
@@ -286,6 +218,8 @@ foreach ($line in (& azd env get-values)) {
286218
Write-Host "API endpoint: $env:API_ENDPOINT"
287219
```
288220

221+
---
222+
289223
You can define hooks in your `azure.yaml` file to run these scripts at specific points in the `azd` lifecycle:
290224

291225
```yaml
@@ -324,75 +258,93 @@ To refresh your local environment variables from the current state of your Azure
324258
azd env refresh
325259
```
326260

327-
The `azd env refresh` command is particularly useful when:
261+
Refreshing your environment is useful when:
328262

329-
- Resources have been modified outside of `azd` (through the Azure portal or other tools)
330-
- Team members have updated the environment
331-
- You've switched to a different machine or workspace
332-
- You need to ensure your local configuration matches the deployed state
263+
- You want to ensure your local `.env` file reflects the latest outputs from your infrastructure (like connection strings, endpoints, etc.).
264+
- You need to sync environment variables after a teammate updated the environment.
333265

334-
## Common environment variables
266+
## AZD vs OS environment variables
335267

336-
`azd` sets and uses several common environment variables across all environments:
268+
`azd` environment variables and operating system environment variables serve different purposes and work in different ways:
337269

338-
| Variable | Description | Example | When Set |
339-
|----------|-------------|---------|---------|
340-
| `AZURE_ENV_NAME` | Name of the current environment | `dev` | When environment is created |
341-
| `AZURE_LOCATION` | Azure region where resources are deployed | `eastus` | During first provisioning |
342-
| `AZURE_SUBSCRIPTION_ID` | ID of the Azure subscription used | `00000000-0000-0000-0000-000000000000` | During first provisioning |
343-
| `AZURE_RESOURCE_GROUP` | Name of the resource group | `rg-myapp-dev` | During provisioning |
344-
| `AZURE_PRINCIPAL_ID` | The running user/service principal ID | `00000000-0000-0000-0000-000000000000` | During provisioning |
345-
| `RESOURCE_TOKEN` | Unique token used to generate resource names | `1a2b3c` | During provisioning |
270+
| Concept | Azure Developer CLI | Operating system |
271+
|---------|------------------------------------------|--------------------------|
272+
| Location | Stored in `.azure/<env-name>/.env` files | Set in your operating system environment |
273+
| Scope | Scoped to a specific named environment within a project | Global to your user session or system |
274+
| Management | Managed using `azd env` commands | Managed using OS-specific commands (`export`, `set`, etc.) |
275+
| Access | Loaded automatically by `azd` commands | Typically loaded explicitly in scripts or applications |
276+
| Target | Tied to Azure resources and deployments | General purpose system configuration |
277+
| Lifecycle | Persist between terminal sessions | May be temporary or persistent depending on how they're set |
346278

347-
Additionally, `azd` automatically sets service-specific variables based on the services defined in your `azure.yaml` file:
279+
`azd` doesn't automatically read or write OS environment variables. However, you can interact with both types of variables using custom scripts.
348280

349-
### App Service
281+
**Read `azd` environment variables and OS environment variables**:
350282

351-
```output
352-
SERVICE_WEB_IDENTITY_PRINCIPAL_ID=00000000-0000-0000-0000-000000000000
353-
SERVICE_WEB_NAME=app-web-dev-1a2b3c
354-
SERVICE_WEB_ENDPOINT=https://app-web-dev-1a2b3c.azurewebsites.net
355-
```
283+
### [Bash](#tab/bash)
356284

357-
### Container Apps
285+
```bash
286+
# Access OS environment variable
287+
echo "OS variable: $PATH"
358288

359-
```output
360-
SERVICE_API_IDENTITY_PRINCIPAL_ID=00000000-0000-0000-0000-000000000000
361-
SERVICE_API_NAME=ca-api-dev-1a2b3c
362-
SERVICE_API_ENDPOINT=https://ca-api-dev-1a2b3c.azurecontainerapps.io
363-
SERVICE_API_IMAGE_NAME=cr-dev-1a2b3c.azurecr.io/api:latest
289+
# Access azd environment variable
290+
echo "AZD variable: $(azd env get-value MY_VARIABLE)"
364291
```
365292

366-
### Azure OpenAI
293+
### [PowerShell](#tab/powershell)
294+
295+
```powershell
296+
# Access OS environment variable
297+
Write-Host "OS variable: $env:PATH"
367298
368-
```output
369-
AZURE_OPENAI_SERVICE=oai-dev-1a2b3c
370-
AZURE_OPENAI_DEPLOYMENT=gpt-35-turbo
371-
AZURE_OPENAI_ENDPOINT=https://oai-dev-1a2b3c.openai.azure.com/
299+
# Access azd environment variable
300+
Write-Host "AZD variable: $(azd env get-value MY_VARIABLE)"
372301
```
373302

374-
### Cosmos DB
303+
---
375304

376-
```output
377-
AZURE_COSMOS_ENDPOINT=https://cosmos-dev-1a2b3c.documents.azure.com:443/
378-
AZURE_COSMOS_DATABASE_NAME=mydb
379-
AZURE_COSMOS_CONTAINER_NAME=items
380-
```
305+
**Convert `azd` environment variables to OS environment variables:**
381306

382-
### Key Vault
307+
### [Bash](#tab/bash)
383308

384-
```output
385-
AZURE_KEY_VAULT_NAME=kv-dev-1a2b3c
386-
AZURE_KEY_VAULT_ENDPOINT=https://kv-dev-1a2b3c.vault.azure.net/
309+
```bash
310+
# Load all azd environment variables into the current shell session
311+
while IFS='=' read -r key value; do
312+
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//')
313+
export "$key=$value"
314+
done <<EOF
315+
$(azd env get-values)
316+
EOF
387317
```
388318

389-
### Storage Account
319+
### [PowerShell](#tab/powershell)
390320

391-
```output
392-
AZURE_STORAGE_ACCOUNT=stdev1a2b3c
393-
AZURE_STORAGE_ENDPOINT=https://stdev1a2b3c.blob.core.windows.net/
321+
```powershell
322+
# Load all azd environment variables into the current PowerShell session
323+
foreach ($line in (& azd env get-values)) {
324+
if ($line -match "([^=]+)=(.*)") {
325+
$key = $matches[1]
326+
$value = $matches[2] -replace '^"|"$'
327+
$env:$key = $value
328+
}
329+
}
394330
```
395331

332+
---
333+
334+
## Common environment variables
335+
336+
`azd` sets and uses several common environment variables across all environments:
337+
338+
| Variable | Description | Example | When Set |
339+
|----------|-------------|---------|---------|
340+
| `AZURE_ENV_NAME` | Name of the current environment | `dev` | When environment is created |
341+
| `AZURE_LOCATION` | Azure region where resources are deployed | `eastus` | During first provisioning |
342+
| `AZURE_SUBSCRIPTION_ID` | ID of the Azure subscription used | `00000000-0000-0000-0000-000000000000` | During first provisioning |
343+
| `AZURE_RESOURCE_GROUP` | Name of the resource group | `rg-myapp-dev` | During provisioning |
344+
| `AZURE_PRINCIPAL_ID` | The running user/service principal ID | `00000000-0000-0000-0000-000000000000` | During provisioning |
345+
| `AZURE_PRINCIPAL_TYPE` | The type of a principal in the environment. | `1a2b3c` | During provisioning |
346+
| `AZURE_TENANT_ID` | The type of a principal in the environment. | `1a2b3c` | During provisioning |
347+
396348
## Secrets and sensitive data considerations
397349

398350
While environment variables are convenient for configuration, they require special handling for sensitive data:
@@ -413,30 +365,17 @@ While environment variables are convenient for configuration, they require speci
413365

414366
For sensitive data, consider these more secure approaches:
415367

416-
1. **Azure Key Vault references**: Store secrets in Azure Key Vault and reference them in your `.env` file:
368+
- **Azure Key Vault references**: Store secrets in Azure Key Vault and reference them in your `.env` file:
417369

418370
```azdeveloper
419-
azd env set-secret DB_PASSWORD
371+
azd env set-secret <secret-value>
420372
```
421373

422374
This command creates a Key Vault secret and stores a reference to it in your `.env` file rather than the actual value.
423375

424-
2. **Managed identities**: Configure your Azure services to use managed identities instead of connection strings or access keys.
425-
426-
3. **Environment-specific security**: Apply stricter security controls to production environments than development ones.
427-
428-
4. **Just-in-time secrets**: Generate short-lived credentials during deployment rather than storing persistent secrets.
429-
430-
### Source control best practices
431-
432-
When working with `.env` files and source control:
433-
434-
- Add `.azure/**/.env` to your `.gitignore` file to prevent accidental commits
435-
- Consider using template `.env.example` files with placeholder values for documentation
436-
- Document required environment variables in your README file
437-
- Use CI/CD pipelines to inject secrets during deployment rather than storing them in repositories
438-
- Regularly rotate sensitive credentials
439-
- Consider using tools like pre-commit hooks to prevent secret leaks
376+
- **Managed identities**: Configure your Azure services to use managed identities instead of connection strings or access keys.
377+
- **Environment-specific security**: Apply stricter security controls to production environments than development ones.
378+
- **Just-in-time secrets**: Generate short-lived credentials during deployment rather than storing persistent secrets.
440379

441380
## Next steps
442381

0 commit comments

Comments
 (0)