Skip to content

Commit 481f9c3

Browse files
committed
updates
1 parent af1e58b commit 481f9c3

File tree

1 file changed

+62
-40
lines changed

1 file changed

+62
-40
lines changed

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

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The `.env` file is also updated automatically by `azd` during operations like `a
7070

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

73-
### CLI commands
73+
### Use CLI commands
7474

7575
The recommended way to set an environment variable is using the `azd env set` command, which includes checks to ensure valid values:
7676

@@ -96,25 +96,6 @@ To verify that your environment variable was set correctly:
9696
azd env get-value API_TIMEOUT
9797
```
9898

99-
### Edit the .env file
100-
101-
You can manually edit the `.env` file located in your environment directory at `.azure/<environment-name>/.env`:
102-
103-
```text
104-
# Example .env file
105-
AZURE_ENV_NAME=dev
106-
AZURE_LOCATION=eastus
107-
AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
108-
API_TIMEOUT=5000
109-
```
110-
111-
While direct editing works, using the `azd env set` command is preferred because it:
112-
113-
- Ensures proper formatting
114-
- Validates the changes
115-
- Avoids potential syntax errors
116-
- Works consistently across different environments
117-
11899
### Output from Bicep
119100

120101
A powerful feature of `azd` is its ability to automatically capture output parameters from your Bicep infrastructure templates as environment variables. For example, when you define an output parameter in your `main.bicep` file:
@@ -132,15 +113,13 @@ API_ENDPOINT=https://api-dev-123456.azurewebsites.net
132113
This approach ensures that your application always has access to the most current resource information, such as:
133114

134115
- Service endpoints and URLs
135-
- Connection strings
136116
- Resource names and identifiers
137-
- API keys (when properly secured)
138117

139118
## Get and use environment variables
140119

141-
Once set, you can access environment variables in several contexts:
120+
Once set, you can access environment variables in several contexts.
142121

143-
### Using CLI commands
122+
### CLI commands
144123

145124
To view all environment variables for the current environment:
146125

@@ -160,26 +139,69 @@ For machine-readable output (useful in scripts):
160139
azd env get-values --output json
161140
```
162141

163-
### Pass environment variables to Bicep files
142+
## Use environment variables in infrastructure files
143+
144+
You can use environment variables to customize your infrastructure templates. This is useful for naming, tagging, or configuring resources based on the current environment.
145+
146+
Consider the following common flow:
147+
148+
1. During `azd init`, `azd` sets these environment variables based on the user's response to prompts:
164149

165-
Environment variables can be referenced in Bicep parameter files (`main.parameters.json`) using a special substitution syntax:
150+
```output
151+
AZURE_ENV_NAME=myapp-dev
152+
AZURE_LOCATION=eastus2
153+
```
166154
167-
```json
168-
{
169-
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
170-
"contentVersion": "1.0.0.0",
171-
"parameters": {
172-
"location": {
173-
"value": "${AZURE_LOCATION}"
174-
},
175-
"environmentName": {
176-
"value": "${AZURE_ENV_NAME}"
155+
2. Reference those variables in `main.parameters.json` in the `infra` folder. `azd` substitutes the values during provisioning and passes the resolved parameters to Bicep:
156+
157+
```json
158+
{
159+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
160+
"contentVersion": "1.0.0.0",
161+
"parameters": {
162+
"name": {
163+
"value": "${AZURE_ENV_NAME}"
164+
},
165+
"location": {
166+
"value": "${AZURE_LOCATION}"
167+
}
168+
}
177169
}
178-
}
179-
}
180-
```
170+
```
171+
172+
3. Define matching parameters in your Bicep template:
173+
174+
```bicep
175+
@description('Name of the environment used to derive resource names and tags.')
176+
param name string
177+
178+
@minLength(1)
179+
@description('Primary Azure region for all resources.')
180+
param location string
181+
```
182+
183+
`azd` supplies these Bicep parameters with substituted values in `main.parameters.json`.
184+
185+
4. Use the parameters for resource naming and tags so you can easily identify which environment a resource belongs to:
186+
187+
```bicep
188+
var resourceToken = toLower(uniqueString(resourceGroup().id, name, location))
189+
190+
resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
191+
name: 'st${resourceToken}'
192+
location: location
193+
sku: {
194+
name: 'Standard_LRS'
195+
}
196+
kind: 'StorageV2'
197+
tags: {
198+
Environment: name
199+
Project: 'myproject'
200+
}
201+
}
202+
```
181203
182-
When `azd` processes this file during provisioning, it automatically substitutes the references with the actual environment variable values from the current environment's `.env` file. This behavior is useful when you want to influence provisioned resources using `azd` environment variables.
204+
This pattern keeps your templates flexible, enables per-environment customization without code changes, and improves resource governance (naming, tagging, and discovery).
183205
184206
### Hooks
185207

0 commit comments

Comments
 (0)