Επεξεργασία

Κοινή χρήση μέσω


Tutorial: Create and deploy your first ARM template

This tutorial introduces you to Azure Resource Manager templates (ARM templates). It shows you how to create a starter template and deploy it to Azure. It teaches you about the template structure and the tools you need to work with templates. This instruction takes 12 minutes to complete, but the actual finish time varies based on how many tools you need to install.

This tutorial is the first of a series. As you progress through the series, you modify the starting template, step by step, until you explore all of the core parts of an ARM template. These elements are the building blocks for more complex templates. We hope by the end of the series you're confident in creating your own templates and ready to automate your deployments with templates.

If you want to learn about the benefits of using templates and why you should automate deployments with templates, see ARM template overview. To learn about ARM templates through a guided set of Learn modules, see Deploy and manage resources in Azure by using JSON ARM templates.

If you don't have a Microsoft Azure subscription, create a free account before you begin.

Tip

If you're new to ARM templates, you might consider learning Bicep instead. Bicep is a new language that offers the same capabilities as ARM templates but with an easier-to-use syntax. To start learning Bicep, see Quickstart: Create Bicep files with Visual Studio Code.

Get tools

Let's start by making sure you have the tools you need to create and deploy templates. Install these tools on your local machine.

Editor

Templates are JavaScript Object Notation (JSON) files. To create templates, you need a good JSON editor. We recommend Visual Studio Code with the Azure Resource Manager Tools extension. If you need to install these tools, see Quickstart: Create ARM templates with Visual Studio Code.

Command-line deployment

You also need either Azure PowerShell or Azure Command-Line Interface (CLI) to deploy the template. If you use Azure CLI, you need to have version 2.37.0 or later. For the installation instructions, see:

After installing either Azure PowerShell or Azure CLI, make sure you sign in for the first time. For help, see Sign in - PowerShell or Sign in - Azure CLI.

Important

If you're using Azure CLI, make sure you have version 2.37.0 or later. If you're using Azure PowerShell, make sure you have version 7.2.4 or later. The commands shown in this tutorial don't work if you're using earlier versions. To check your installed version, use: az --version.

Okay, you're ready to start learning about templates.

Create your first template

  1. Open Visual Studio Code with the installed ARM processor Tools extension.

  2. From the File menu, select New File to create a new file.

  3. From the File menu, select Save As.

  4. Name the file azuredeploy and select the json file extension. The complete name of the file is azuredeploy.json.

  5. Save the file to your workstation. Select a path that's easy to remember because you need to provide that path later when deploying the template.

  6. Copy and paste the following JSON into the file:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "resources": []
    }
    

    Here's what your Visual Studio Code environment looks like:

    Screenshot of Visual Studio Code displaying an empty ARM template with JSON structure in the editor.

    This template doesn't deploy any resources. We're starting with a blank template so you can get familiar with the steps to deploy a template while minimizing the chance of something going wrong.

    The JSON file has these elements:

    • $schema: Specifies the location of the JSON schema file. The schema file describes the properties that are available within a template. The schema, for example, defines resources as one of the valid properties for a template. Don't worry that the date for the schema is 2019-04-01. This schema version is up to date and includes all of the latest features. The schema date hasn't been changed because there have been no breaking changes since its introduction.
    • contentVersion: Specifies the version of the template, such as 1.0.0.0. You can provide any value for this element. Use this value to document significant changes in your template. When you deploy resources using the template, you can use this value to make sure you're using the right template.
    • resources: Contains the resources you want to deploy or update. Currently, it's empty, but you can add resources later.
  7. Save the file.

Congratulations, you've created your first template.

Sign in to Azure

To start working with Azure PowerShell or Azure CLI, sign in with your Azure credentials.

Select the tabs in the following code sections to pick between Azure PowerShell and Azure CLI. The CLI examples in this article are written for the Bash shell.

Connect-AzAccount

If you have multiple Azure subscriptions, choose the subscription you want to use. Replace SubscriptionName with your subscription name. You can also use your subscription ID instead of your subscription name.

Set-AzContext SubscriptionName

Create resource group

When you deploy a template, you can specify a resource group to contain the resources. Before running the deployment command, create the resource group with either the Bash Azure CLI or Azure PowerShell.

Note

Samples for the Azure CLI are written for the bash shell. To run this sample in Windows PowerShell or the Command Prompt, you may need to remove the back slashes and write the command as one line such as:

New-AzResourceGroup `
  -Name myResourceGroup `
  -Location "Central US"

Deploy template

To deploy the template, use either Azure CLI or Azure PowerShell. Use the resource group you created. Give a name to the deployment so you can easily identify it in the deployment history. For convenience, also create a variable that stores the path to the template file. This variable makes it easier for you to run the deployment commands because you don't have to retype the path every time you deploy. Replace {provide-the-path-to-the-template-file} and the curly braces {} with the path to your template file.

$templateFile = "{provide-the-path-to-the-template-file}"
New-AzResourceGroupDeployment `
  -Name blanktemplate `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile

The deployment command returns results. Look for ProvisioningState to see whether the deployment succeeded.

Note

If the deployment fails, use the verbose switch to get information about the resources being created. Use the debug switch to get more information for debugging.

Verify deployment

You can verify the deployment by exploring the resource group from the Azure portal.

  1. Sign in to the Azure portal.

  2. From the left menu, select Resource groups.

  3. Check the box to the left of myResourceGroup and select myResourceGroup.

  4. Select the resource group you created in the earlier procedure. The default name is myResourceGroup. The resource group doesn't have any resources yet because you deployed a blank template.

  5. Notice in the middle of the overview, in the Essentials section, the page displays the deployment status next to Deployments. Select 1 Succeeded.

    Screenshot of Azure portal showing the deployment status in the Essentials section of the resource group.

  6. You see a history of deployment for the resource group. Check the box to the left of blanktemplate and select blanktemplate.

    Screenshot of Azure portal displaying the deployment history with the blanktemplate deployment selected.

  7. You see a summary of the deployment. In this case, there's not a lot to see because no resources are deployed. Later in this series you might find it helpful to review the summary in the deployment history. Notice on the left you can see inputs, outputs, and the template that the deployment used.

    Screenshot of Azure portal showing the deployment summary for the blanktemplate deployment.

Clean up resources

If you're moving on to the next tutorial, you don't need to delete the resource group.

If you're stopping now, you might want to delete the resource group.

  1. From the Azure portal, select Resource groups from the left menu.

  2. Type the resource group name in the Filter for any field... text field.

  3. Check the box next to myResourceGroup and select myResourceGroup or your resource group name.

  4. Select Delete resource group from the top menu.

    Screenshot of Azure portal with the Delete resource group option highlighted in the resource group view.

Next steps

You created a simple template to deploy to Azure. In the next tutorial, you can learn how to add a storage account to the template and deploy it to your resource group.