DEV Community

Cover image for How to create a JSON ARM template with Visual Studio Code
sipsdaoracle
sipsdaoracle

Posted on

How to create a JSON ARM template with Visual Studio Code

In this exercise, we will use the extension to create an ARM template from scratch. While doing so you experience the extensions capabilities such as ARM template snippets , validation , completions, and parameter file support.

What is JSON ARM and what is it useful for?

Image description

JSON, or JavaScript Object Notation, is a data interchange format that is commonly used for configuration and data exchange in modern applications.

In Azure Resource Manager, JSON templates are used to define and deploy resources in a declarative way.

This means that instead of writing imperative scripts to specify the steps required to create and configure resources, you can define the desired state of your resources using JSON templates.

Image description

Prerequisite:

  • Visual Studio Code
  • Azure Resource Manager Tools extension
  • Azure CLI or Azure Powershell
  • Azure subscription

Task 1: Create an ARM template

Step 1: Create and open with VSCode new file azuredeploy.json

  • Enter arminto the code editor, which initiates Azure Resource Manager snippets for scaffolding out an ARM template.

Select arm! to create a template scoped for an Azure resource group deployment

Image description

The result should be the template below:

Image description

Task 2: Add an Azure resource

Step 1: Scroll down to line 7 and place the cursor in the template resources block and then select arm-storage

Image description

You should see the following storage template below:

Image description

Task 3: Add template parameters

Parameters will be used to specify the storage account name.

Step 1: On line 4 place the cursor in the parameters block, type " and select the new-parameter snippet.

This should add a parameter to your template.

Image description

Step 2: Set the name of the parameter to storageAccountName and the description to Storage Account Name.

Image description

Step 3: Add min and max storage account name

length

Image description

Step 4: Update name property to use it

  • Navigate to line 14 on the resource storage
  • Remove current name
  • Enter a double quote " and an opening square bracket[, select parameters

Image description

Step 5: Enter round brackets and a single quote ' inside the brackets

A list of defined parameters will be listed, select the parameter

Image description

Task 4: Create a parameter file

A parameter file allows you to store environment-specific parameter values and pass these values in as a group during deployment.

Step 1: Right-click on the template (code editor) and select Select/Create Parameter File

Image description

Step 2: Select New > All Parameters
Enter a name and choose a location for the parameter file

Image description

The extension between the parameter file and the template validates both the files together.

Task 5 Deploy the template

Step 1:
Open Visual studio code terminal or Azure Powershell

N.B: Make sure that on your Visual Studio Code you have singed in to your Microsoft account. To do this you need select the accounts icon and Turn On Cloud

Now...Deploy by pasting the following code on the CLI/Powershell:

az group create --name arm-vscode --location eastus

az deployment group create --resource-group arm-vscode --template-file azuredeploy.json --parameters azuredeploy.parameters.json
Enter fullscreen mode Exit fullscreen mode

Image description
The first code was executed successfully now it will deploy and create an ARM resource group

Image description

Image description

Now we can head on to our Azure portal and check if indeed it has been created.

Image description

So far so good!

Image description

There you go you have successfully deployed.

If you prefer you can push your code to your Github repository as I have done:

Image description

Clean up Resources

Now that you have successfully deployed ensure you clean by deleting all the resources to avoid unnecessary incurred cost.

Log back to your Azure portal and search for resources that have just been created and ensure that you have deleted them.

Image description

Congratulations, on completing this tutorial.

Top comments (0)