|
| 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