How to create a JSON ARM template with Visual Studio Code
Tags: shecodeafrica, tutorial, azure, beginners
Published: 2023-05-19 07:25:14

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vxexg3mb5v16v1u2vwp.png)

_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vkn3uz4anznj0qdbemk.png)

## 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 `arm `into 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aslhe0ch78nijufnowtl.png)

_The result should be the template below:_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlc4tdjrgr6qrl5gwcnx.PNG)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0aa88axq3vpvkncp4a8r.png)

_You should see the following storage template below:_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h63eb9l7z655l69pc7jd.PNG)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw6nwwv9sz29whxmvjvp.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfqflilkkbc7cuv4fpea.PNG)

**Step 3:** Add min and max storage account name 

length 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kju8aql0l6dpicg23j98.PNG)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmnipf6w8uj2zcjgio0h.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnnomge0l2osvvn373lo.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20ugmnyd31xdphsr8gfx.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qiiw0fempcwv2bsq5b3h.PNG)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdssdp9b0qzrl1m08fv2.PNG)
_The first code was executed successfully now it will deploy and create an ARM resource group_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phwijmco5o98kkvbswyf.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tf8by6ivx1cq513aye4.PNG)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y69jq5t3mhm1x4vtl5i7.PNG)

So far so good!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wouptrfwi6ti1rwhqxgd.PNG)

_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17x1wguyife0s9co164c.PNG)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cf1jx9zor22iyjfqtys.PNG)


Congratulations, on completing this tutorial.