Create an environment by using the Azure Developer CLI
Članak
In this article, you install the Azure Developer CLI (AZD), create a new deployment environment by provisioning your app infrastructure to Azure Deployment Environments (ADE), 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).
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.
In Visual Studio 2022 17.3 Preview 2 or later, you can enable integration with azd as a preview feature.
To enable the azd feature, go to Tools > Options > Environment > Preview Features and select Integration with azd, the Azure Developer CLI.
When the feature is enabled, you can use the Azure Developer CLI from your terminal of choice on Windows, Linux, or macOS.
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.
The output of commands issued from the command palette is displayed in an azd dev terminal like the following example:
Sign in to Azure at the CLI using the following command:
azd auth login
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.
To open the Developer Command prompt:
From the Tools menu, select Terminal.
In the Terminal window, select Developer Command Prompt.
Sign in to AZD using the Developer Command Terminal:
azd auth login
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.
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.
In Visual Studio Code, open the folder that contains your application code.
Open the command palette, and enter Azure Developer CLI init, then select Azure Developer CLI (azd): init.
In the list of templates, to continue without selecting a template, press ENTER twice.
In the AZD terminal, select Use code in the current directory.
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.
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.
Enter a name for your local AZD environment.
azd init displays a list of the projects you have access to. Select the project for your environment
azd init displays a list of environment definitions in the project. Select an environment definition.
AZD creates the project resources, including an azure.yaml file in the root of your project.
At the CLI, navigate to the folder that contains your application code.
Run the following command to initialize your application and supply information when prompted:
azd init
In the AZD terminal, select Use code in the current directory.
AZD scans the current directory and gathers more information depending on the type of app you're building. Follow the prompts to configure your AZD environment.
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.
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.
Enter a name for your local AZD environment.
azd init displays a list of the projects you have access to. Select the project for your environment.
azd init displays a list of environment definitions in the project. Select an environment definition.
AZD creates the project resources, including an azure.yaml file in the root of your project.
At the CLI, navigate to the folder that contains your application code.
Run the following command to initialize your application and supply information when prompted:
azd init
In the AZD terminal, select Use code in the current directory.
AZD scans the current directory and gathers more information depending on the type of app you're building. Follow the prompts to configure your AZD environment.
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.
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.
Enter a name for your local AZD environment.
azd init displays a list of the projects you have access to. Select the project for your environment.
azd init displays a list of environment definitions in the project. Select an environment definition.
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.
In Explorer, right-click azure.yaml, and then select Azure Developer CLI (azd) > Provision Azure Resources (provision).
AZD scans Azure Deployment Environments for projects that you have access to. In the AZD terminal, select or enter the following information:
Project
Environment definition
Environment type
Location
AZD instructs ADE to create a new environment based on the information you gave in the previous step.
You can view the resources created in the Azure portal or in the developer portal.
Provision your application to Azure using the following command:
azd provision
'azd provision' provides a list of projects that you have access to. Select the project that you want to provision your application to.
'azd provision' provides a list of environment definitions in the selected project. Select the environment definition that you want to use to provision your application.
'azd provision' provides a list of environment types in the selected project. Select the environment type that you want to use to provision your application.
AZD instructs ADE to create a new environment based on the information you gave in the previous step.
You can view the resources created in the Azure portal or in the developer portal.
Provision your application to Azure using the following command:
azd provision
'azd provision' provides a list of projects that you have access to. Select the project that you want to provision your application to.
'azd provision' provides a list of environment definitions in the selected project. Select the environment definition that you want to use to provision your application.
'azd provision' provides a list of environment types in the selected project. Select the environment type that you want to use to provision your application.
AZD instructs ADE to create a new environment based on the information you gave in the previous step.
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.
In Explorer, right-click azure.yaml, and then select Azure Developer CLI (azd) > View Local and Remote Environments (env list).
You're prompted to select a project and an environment definition.
Use the following command to view the environments that you have access to: the local AZD environment and the remote Azure Deployment Environments environment.
azd env list
azd env list prompts you to select a project and an environment definition.
Use the following command to view the environments that you have access to: the local AZD environment and the remote Azure Deployment Environments environment.
azd env list
azd env list prompts you 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.