Skip to content

Commit 7743f51

Browse files
Merge pull request #7662 from MicrosoftDocs/main
Auto Publish – main to live - 2025-08-08 23:00 UTC
2 parents 1ec4438 + cc8c51a commit 7743f51

18 files changed

+383
-32
lines changed

articles/azure-developer-cli/TOC.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@
9999
href: remote-environments-support.md
100100
- name: Azure Deployment Environments integration
101101
href: ade-integration.md
102-
- name: Work with environments
102+
- name: Environments
103103
items:
104+
- name: Overview
105+
href: environments-overview.md
106+
- name: Work with environments
107+
href: work-with-environments.md
104108
- name: Manage environment variables
105109
href: manage-environment-variables.md
106110
- name: Environment variables FAQ

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ ms.custom: devx-track-azdevcli
1313

1414
This article answers frequently asked questions about working with environment variables and the Azure Developer CLI (`azd`).
1515

16+
> [!TIP]
17+
> For a comprehensive guide on creating and managing environments in Azure Developer CLI, see [Work with environments in Azure Developer CLI](work-with-environments.md).
18+
1619
### What are the differences between `azd` environment variables and system environment variables?
1720

1821
`azd` environment variables are stored in the `.env` file in the `.azure/<environment name>` directory of your project and are separate from your system/OS environment variables. `azd` environment variables configure template provisioning and deployment tasks and are accessible using commands such as [`azd env`](/azure/developer/azure-developer-cli/reference#azd-env) or [`azd env get-values`](/azure/developer/azure-developer-cli/reference#azd-env-get-values).
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
title: Azure Developer CLI Environments Overview
3+
description: Learn essential concepts about environments using Azure Developer CLI (azd).
4+
author: alexwolfmsft
5+
ms.author: alexwolf
6+
ms.date: 08/04/2025
7+
ms.service: azure-dev-cli
8+
ms.topic: how-to
9+
ms.custom: devx-track-azdevcli, build-2023
10+
---
11+
12+
# Azure Developer CLI environments overview
13+
14+
The Azure Developer CLI (`azd`) lets you manage multiple deployment environments for your projects, to keep configurations separate for development, testing, and production. This article explains essential concepts about how you can use environments to manage your development and deployment process.
15+
16+
## What are environments?
17+
18+
An environment in the Azure Developer CLI (`azd`) is a named set of configurations for a deployment of your app, such as dev, test, or prod. Different environments can be configured with different values. Environments serve several important purposes:
19+
20+
- **Isolation**: Keep development, testing, and production deployments separate.
21+
- **Configuration management**: Maintain different settings for each environment.
22+
- **Collaboration**: Enable team members to work with their own environments.
23+
- **Resource organization**: Group and provision Azure resources by environment.
24+
- **Reproducibility**: Ensure consistent deployments across different stages.
25+
26+
Each environment has its own Azure resource group and configuration settings. This environment isolation helps prevent changes in one environment from affecting others.
27+
28+
## Environment structure and configuration
29+
30+
Azure Developer CLI (`azd`) environments live in a directory structure within your project:
31+
32+
```txt
33+
├── .azure [Created when you run azd init or azd up]
34+
│ ├── <environment-name-1> [Directory for environment-specific configurations]
35+
│ │ ├── .env [Environment variables for this environment]
36+
│ │ └── config.json [Additional configuration parameters for this environment]
37+
│ ├── <environment-name-2> [Another environment]
38+
│ │ ├── .env
39+
│ │ └── config.json
40+
│ └── config.json [Global azd configuration]
41+
```
42+
43+
The key components of this structure are:
44+
45+
- **`.azure` directory**: The root directory for all environment configurations. Excluded from source control by the `.gitignore` file by default.
46+
- **Environment-specific directories**: Directories named after your environments, such as `dev`, `test`, `prod`.
47+
- **`.env` file**: Contains environment-specific variables used by your application and during deployment.
48+
- **`config.json`**: Used to drive settings that influence `azd` command behavior and features. This file isn't intended to be used directly by end users.
49+
50+
### Environment names
51+
52+
Environment naming typically follows these patterns:
53+
54+
- Team projects: `<project-name-[dev/int/prod]>`
55+
- Personal projects: `<personal-unique-alias-[dev/int/prod]>`
56+
57+
These naming conventions aren't enforced by `azd` and are configurable by the user.
58+
59+
## Environment variables
60+
61+
Azure Developer CLI [Environment variables](manage-environment-variables.md) provide a way to store configuration settings that influence and might vary between environments. When you run Azure Developer CLI commands, these variables are used to:
62+
63+
- Configure your application settings, such as endpoints for Azure services.
64+
- Define infrastructure parameters to influence the provisioning process.
65+
66+
The `.env` file contains these variables in a standard format:
67+
68+
```output
69+
AZURE_ENV_NAME=dev
70+
AZURE_LOCATION=eastus
71+
AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
72+
AZURE_RESOURCE_GROUP=rg-dev-12345
73+
SERVICE_WEB_HOSTNAME=web-dev-12345.azurewebsites.net
74+
SERVICE_API_HOSTNAME=api-dev-12345.azurewebsites.net
75+
```
76+
77+
Common environment variables include:
78+
79+
| Variable | Description |
80+
|----------|-------------|
81+
| `AZURE_ENV_NAME` | Name of the current environment |
82+
| `AZURE_LOCATION` | Azure region where resources are deployed |
83+
| `AZURE_SUBSCRIPTION_ID` | ID of the Azure subscription used for this environment |
84+
| `AZURE_RESOURCE_GROUP` | Name of the resource group for this environment |
85+
86+
> [!TIP]
87+
> For other common environment variables and service-specific examples, visit the [Environment variables](manage-environment-variables.md) documentation.
88+
89+
When working with environment variables:
90+
91+
- Avoid committing `.env` files to source control. If environment configuration needs to be persisted or shared, users should use [Remote environments](remote-environments-support.md).
92+
- Use consistent naming across environments.
93+
- Use the `azd env set` command to update variables safely.
94+
95+
> [!WARNING]
96+
> Never store secrets in an Azure Developer CLI `.env` file. These files can easily be shared or copied into unauthorized locations, or checked into source control. Use services such as Azure Key Vault or Azure Role Based Access Control (RBAC) for protected or secretless solutions.
97+
98+
## Compare other framework environments
99+
100+
Many programming frameworks and tools such as Node.js, Django, or React use `.env` files for configuration. While Azure Developer CLI (`azd`) also uses `.env` files, there are important differences:
101+
102+
| Concept | Azure Developer CLI `.env` | Framework `.env` Files |
103+
|--------|---------------------------|------------------------|
104+
| **Location** | Stored in `.azure/<environment-name>/.env` | Typically stored in project root directory |
105+
| **Environment Support** | Support for multiple user-defined environments (dev, test, prod) | Often require manual file switching or naming conventions (`.env.development`, `.env.production`) |
106+
| **Loading Mechanism** | Automatically loaded by `azd` commands | Usually require explicit loading in application code or build scripts |
107+
| **Integration** | Deeply integrated with Azure services and resource provisioning | General purpose configuration, not Azure-specific |
108+
| **Variable Management** | Managed via `azd env` commands | Typically edited manually or via custom scripts |
109+
110+
While both serve similar purposes, Azure Developer CLI's `.env` approach adds structure and tooling designed for managing multiple deployment environments and Azure resources.
111+
112+
> [!NOTE]
113+
> If your project already uses framework-specific `.env` files, you can keep both configuration systems without conflicts. `azd` environment variables override system environment variables of the same name for some operations.
114+
115+
## Next steps
116+
117+
> [!div class="nextstepaction"]
118+
> [Work with environments](work-with-environments.md)
119+
120+
> [!div class="nextstepaction"]
121+
> [Manage environment variables in Azure Developer CLI](manage-environment-variables.md)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ ms.custom: devx-track-azdevcli
1313

1414
Environment variables influence how resources are provisioned and deployed to Azure. This is especially useful when running azd in CI/CD workflow scenarios.
1515

16+
> [!TIP]
17+
> For a comprehensive guide on creating and managing environments in Azure Developer CLI, see [Work with environments in Azure Developer CLI](work-with-environments.md).
18+
1619
## Input Parameters Substitution
1720

1821
Environment variables can be referenced in parameter files (`*.parameters.json` for Bicep, `*.tfvars.json` for Terraform) as part of provisioning. When an environment variable substitution syntax is encountered, `azd` automatically substitutes the reference with the actual environment variable value set. Substitution also occurs for certain configuration settings in `azure.yaml` (properties documented with 'Supports environment variable substitution'), and in deployment configuration files, such as deployment manifests for `aks`.

articles/azure-developer-cli/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Azure Developer CLI reference
33
description: This article explains the syntax and parameters for the various Azure Developer CLI commands.
44
author: alexwolfmsft
55
ms.author: alexwolf
6-
ms.date: 07/17/2025
6+
ms.date: 08/07/2025
77
ms.service: azure-dev-cli
88
ms.topic: conceptual
99
ms.custom: devx-track-azdevcli
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
---
2+
title: Work with Environments in Azure Developer CLI
3+
description: Learn how to create, manage, and switch between different environments using Azure Developer CLI (azd).
4+
author: alexwolfmsft
5+
ms.author: alexwolf
6+
ms.date: 08/04/2025
7+
ms.service: azure-dev-cli
8+
ms.topic: how-to
9+
ms.custom: devx-track-azdevcli, build-2023
10+
---
11+
12+
# Work with Azure Developer CLI environments
13+
14+
The Azure Developer CLI (`azd`) helps you create and manage [Environments](environments-overview.md) with their own configurations, such as dev, test, and prod. This article shows how to create and manage environments, and how to use them with your Bicep infrastructure files.
15+
16+
## Create environments
17+
18+
Create a new environment using the `azd env new` command:
19+
20+
```azdeveloper
21+
azd env new <environment-name>
22+
```
23+
24+
For example, to create a development environment:
25+
26+
```azdeveloper
27+
azd env new dev
28+
```
29+
30+
When you run a command such as `azd up` or `azd deploy`, `azd` prompts you to select an Azure subscription and location for the new environment. Prompt settings are stored in the new environment `.env` or `config.json` files.
31+
32+
You can also specify subscription and location directly in the command:
33+
34+
```azdeveloper
35+
azd env new prod --subscription "My Production Subscription" --location eastus2
36+
```
37+
38+
## List environments
39+
40+
To see all available environments for your project, use:
41+
42+
```azdeveloper
43+
azd env list
44+
```
45+
46+
This command displays all the environments you created, highlighting the current active environment:
47+
48+
```output
49+
NAME DEFAULT LOCAL REMOTE
50+
dev true true false
51+
test false true false
52+
prod false true false
53+
```
54+
55+
## Switch between environments
56+
57+
To switch to a different environment, use the `azd env select` command:
58+
59+
```azdeveloper
60+
azd env select <environment-name>
61+
```
62+
63+
For example, to switch to a production environment:
64+
65+
```azdeveloper
66+
azd env select prod
67+
```
68+
69+
> [!NOTE]
70+
> This command changes your active environment, which affects subsequent `azd` commands like `provision` or `deploy`.
71+
72+
## Understand the default environment
73+
74+
The global configuration file `.azure/config.json` keeps track of your currently selected environment. When you run `azd init` and no environments exist yet, `azd` automatically creates your first environment and sets it as the default. If you already have one or more environments and run `azd env new <name>`, `azd` prompts you to choose whether to make the new environment the default. If you decline, the new environment is created but your current selection remains unchanged.
75+
76+
You can temporarily override the default environment for a single command by using the `--environment` flag. Using this flag doesn't change the default for future commands.
77+
78+
## Refresh environment settings
79+
80+
You can refresh your local environment variables using the `azd env refresh` command. This command locates the most recent Azure deployment for your app, retrieves the environment variable values by name, and then updates your local `.env` file with those latest values for the select environment. For example, if you provisioned both a `dev` and `prod` version, and you currently have the `dev` environment selected, it retrieves the latest output from that deployment to populate the .env file.
81+
82+
```azdeveloper
83+
azd env refresh
84+
```
85+
86+
> [!NOTE]
87+
> The `azd env refresh` command doesn't redeploy resources. It only updates your local environment configuration to match the current state in Azure.
88+
89+
Refreshing your environment is useful when:
90+
91+
- You want to ensure your local `.env` file reflects the latest outputs from your infrastructure (like connection strings, endpoints, etc.).
92+
- You need to sync environment variables after a teammate updated the environment.
93+
94+
If other team members made changes to environment configurations, or if you made changes through the Azure portal, you can refresh your local environment settings with:
95+
96+
## Run commands in specific environments
97+
98+
You can run many `azd` commands in a specific environment without changing your active environment by using the `--environment` or `-e` flag:
99+
100+
```azdeveloper
101+
azd up --environment dev
102+
```
103+
104+
This command runs the `up` workflow (provision and deploy) in the `dev` environment without changing your active environment.
105+
106+
Alternatively, you can first switch to your intended environment:
107+
108+
```azdeveloper
109+
azd env select test
110+
azd up
111+
```
112+
113+
> [!NOTE]
114+
> Teams should consider using CICD pipelines via the `azd pipeline config` command, rather than direct deployments using commands such as `azd up` or `azd provision`.
115+
116+
## Delete environment resources
117+
118+
To delete the Azure resources for a specific environment, using the `azd down` command:
119+
120+
```azdeveloper
121+
azd down <environment-name>
122+
```
123+
124+
> [!NOTE]
125+
> It's currently not possible to delete or rename `azd` environments directly using commands. If you need to rename an environment:
126+
>
127+
> - Use `azd down` to delete the environment resources.
128+
> - Run `azd env new <new-name>` to create the new environment.
129+
> - Manually delete the old `.env` folder from `.azure`.
130+
131+
## Use the environment name in infrastructure files
132+
133+
You can use the `AZURE_ENV_NAME` variable from your environment's `.env` file to customize your infrastructure deployments in Bicep. This is useful for naming, tagging, or configuring resources based on the current environment.
134+
135+
> [!NOTE]
136+
> Visit the [Work with environment variables](manage-environment-variables.md) to learn more about how to use environment variables to configure your Azure Developer CLI projects.
137+
138+
1. `azd` sets the `AZURE_ENV_NAME` environment variable when you initialize a project.
139+
140+
```output
141+
AZURE_ENV_NAME=dev
142+
```
143+
144+
1. In your `main.parameters.json` file, reference the environment variable so `azd` substitutes its value:
145+
146+
```json
147+
{
148+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
149+
"contentVersion": "1.0.0.0",
150+
"parameters": {
151+
"environmentName": {
152+
"value": "${AZURE_ENV_NAME}"
153+
}
154+
}
155+
}
156+
```
157+
158+
When you deploy with `azd`, the value from `.env` is passed to your Bicep file from `main.parameters.json`.
159+
160+
1. In your Bicep template, define a parameter for the environment name:
161+
162+
```bicep
163+
param environmentName string
164+
```
165+
166+
1. You can use the `environmentName` parameter to tag resources, making it easy to identify which environment a resource belongs to:
167+
168+
```bicep
169+
param environmentName string
170+
171+
resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
172+
name: 'mystorage${uniqueString(resourceGroup().id)}'
173+
location: resourceGroup().location
174+
sku: {
175+
name: 'Standard_LRS'
176+
}
177+
kind: 'StorageV2'
178+
tags: {
179+
Environment: environmentName
180+
Project: 'myproject'
181+
}
182+
}
183+
```
184+
185+
This approach helps with resource management, cost tracking, and automation by associating each resource with its deployment environment.
186+
187+
## Next steps
188+
189+
> [!div class="nextstepaction"]
190+
> [Manage environment variables in Azure Developer CLI](manage-environment-variables.md)
191+
192+
> [!div class="nextstepaction"]
193+
> [Customize your Azure Developer CLI workflows using hooks](azd-extensibility.md)

0 commit comments

Comments
 (0)