Create an environment by using the Azure Developer CLI

In this article, you install the Azure Developer CLI (AZD), create a new deployment environment by provisioning your app infrastructure to Azure Deployment Environments, and deploy your app code onto the provisioned deployment environment.

Azure Developer CLI (AZD) is an open-source tool that accelerates the time it takes for you to get your application from local development environment to Azure. AZD provides best practice, developer-friendly commands that map to key stages in your workflow, whether you're working in the terminal, your editor or integrated development environment (IDE), or CI/CD (continuous integration/continuous deployment).

To learn how to set up AZD to work with Azure Deployment Environments, see Use Azure Developer CLI with Azure Deployment Environments.

Prerequisites

You should:

Prepare to work with AZD

When you work with AZD for the first time, there are some one-time setup tasks you need to complete. These tasks include installing the Azure Developer CLI, signing in to your Azure account, and enabling AZD support for Azure Deployment Environments.

Install the Azure Developer CLI

When you install azd, the azd tools are installed within azd scope rather than globally, and are removed if azd is uninstalled. To use azd tools in Visual Studio Code, install the extension.

To enable Azure Developer CLI features in Visual Studio Code, install the Azure Developer CLI extension, version v0.8.3. Select the Extensions icon in the Activity bar, search for Azure Developer CLI, and then select Install.

Screenshot of Visual Studio Code, showing the Sign in command in the command palette.

Sign in with Azure Developer CLI

Access your Azure resources by logging in. When you initiate a log in, a browser window opens and prompts you to log in to Azure. After you sign in, the terminal displays a message that you're signed in to Azure.

Sign in to AZD using the command palette:

Screenshot of Visual Studio Code, showing the Extensions pane with the Azure Developer CLI and Install highlighted.

The output of commands issued from the command palette is displayed in an azd dev terminal like the following example:

Screenshot of the azd dev terminal, showing the press any key to close message.

Enable AZD support for ADE

You can configure AZD to provision and deploy resources to your deployment environments using standard commands such as azd up or azd provision. When platform.type is set to devcenter, all AZD remote environment state and provisioning uses dev center components. AZD uses one of the infrastructure templates defined in your dev center catalog for resource provisioning. In this configuration, the infra folder in your local templates isn't used.

Screenshot of Visual Studio Code, showing the Enable support command in the command palette.

Create an environment from existing code

Now you're ready to create an environment to work in. You can begin with code in a local folder, or you can clone an existing repository. In this example, you create an environment by using code in a local folder.

Initialize a new application

Initializing a new application creates the files and folders that are required for AZD to work with your application.

AZD uses an azure.yaml file to define the environment. The azure.yaml file defines and describes the apps and types of Azure resources that the application uses. To learn more about azure.yaml, see Azure Developer CLI's azure.yaml schema.

  1. In Visual Studio Code, open the folder that contains your application code.

  2. Open the command palette, and enter Azure Developer CLI init, then from the list, select Azure Developer CLI (azd): init.

    Screenshot of the Visual Studio Code command palette with Azure Developer CLI (azd): init highlighted.

  3. In the list of templates, to continue without selecting a template, press ENTER twice.

  4. In the AZD terminal, select Use code in the current directory.

    Screenshot of the AZD terminal in Visual Studio Code, showing the Use code in current directory prompt.

  5. azd init identifies the services defined in your app code and prompts you to confirm and continue, remove a service, or add a service. Select Confirm and continue initializing my app.

    Screenshot showing the AZD init prompt to confirm and continue, remove a service, or add a service.

  6. azd init continues to gather information to configure your app. For this example application, you're prompted for the name of your MongoDB database instance, and ports that the services listen on.

    Screenshot showing the azd init prompt for a database name.

  7. Enter a name for your local AZD environment.

    Screenshot showing azd init prompt Enter a new environment name.

  8. azd init displays a list of the projects you have access to. Select the project for your environment

    Screenshot showing azd init prompt Select project.

  9. azd init displays a list of environment definitions in the project. Select an environment definition.

    Screenshot showing azd init prompt Select environment definitions.

    AZD creates the project resources, including an azure.yaml file in the root of your project.

Provision infrastructure to Azure Deployment Environment

When you're ready, you can provision your local environment to a remote Azure Deployment Environments environment in Azure. This process provisions the infrastructure and resources defined in the environment definition in your dev center catalog.

  1. In Explorer, right-click azure.yaml, and then select Azure Developer CLI (azd) > Provision Azure Resources (provision).

    Screenshot of Visual Studio Code with azure.yaml highlighted, and the AZD context menu with Azure Developer CLI and Provision environment highlighted.

  2. AZD scans Azure Deployment Environments for projects that you have access to. In the AZD terminal, select or enter the following information:

    1. Project
    2. Environment definition
    3. Environment type
    4. Location
  3. AZD instructs ADE to create a new environment based on the information you gave in the previous step.

  4. You can view the resources created in the Azure portal or in the developer portal.

List existing environments (optional)

Verify that your environment is created by listing the existing environments.

  1. In Explorer, right-click azure.yaml, and then select Azure Developer CLI (azd) > View Local and Remote Environments (env list).

    Screenshot of Visual Studio Code with azure.yaml highlighted, and the AZD context menu with Azure Developer CLI and View Local and Remote environments highlighted.

    You're prompted to select a project and an environment definition.

Deploy code to Azure Deployment Environments

When your environment is provisioned, you can deploy your code to the environment.

  1. In Explorer, right-click azure.yaml, and then select Azure Developer CLI (azd) > Deploy Azure Resources (deploy).

    Screenshot of Visual Studio Code with azure.yaml highlighted, and the AZD context menu with Azure Developer CLI and Deploy to Azure highlighted.

  2. You can verify that your code is deployed by selecting the end point URLs listed in the AZD terminal.

Clean up resources

When you're finished with your environment, you can delete the Azure resources.

In Explorer, right-click azure.yaml, and then select Azure Developer CLI (azd) > Delete Deployment and Resources (down).

Screenshot of Visual Studio Code with azure.yaml highlighted, and the AZD context menu with Azure Developer CLI and Delete Deployment and Resources (down) highlighted.

Confirm that you want to delete the environment by entering y when prompted.