|
| 1 | +# Building Azure DevTest Labs Environments using Azure CLI |
| 2 | + |
| 3 | +Make sure the DevTest Lab resource is created and the git repo is [added as a template repository](https://docs.microsoft.com/en-us/azure/devtest-labs/devtest-lab-create-environment-from-arm). |
| 4 | + |
| 5 | +Azure CLI is required to create environments as PowerShell AZ only has a small subset of the DevTest Labs commands. |
| 6 | + |
| 7 | +1) Login to azure |
| 8 | +``` |
| 9 | +az login |
| 10 | +``` |
| 11 | + |
| 12 | +2) List artifact sources |
| 13 | +``` |
| 14 | +az lab artifact-source list --resource-group '{DevTest Labs Resorce Group Name}' ` |
| 15 | + --lab-name '{DevTest Labs Name}' |
| 16 | +``` |
| 17 | + |
| 18 | +3) In the returned JSON look for the item in the array where the display name matches the name of the template repository you set up, or the uri matches the git url of that repo. Find the name of the node, it should be something like *privaterepoXXX* where *XXX* is a random number. We use this to list the available ARM templates. If an ARM template or the parameter file is not valid, it will not show up in the list, but also does not generate errors when the template repository is configured. |
| 19 | +``` |
| 20 | +az lab arm-template list --resource-group '{DevTest Labs Resorce Group Name}' ` |
| 21 | + --lab-name '{DevTest Labs Name}' ` |
| 22 | + --artifact-source-name '{privaterepoXXX}' |
| 23 | +``` |
| 24 | + |
| 25 | +4) The name one each item in the returned JSON should match the folders in the folder setup as the *Azure Resource Manager template folder path* when the template repository was configured. The ARM template and parameters are shown. We can check the ARM template is as we expect, and then create a parameters file to pass to create an environment. An example parameters file is shown below. Save it as a JSON file. |
| 26 | +***Note:** the paramters file for the AZ CLI tools is a different format to the paramters file stored in github as part of the template definition.* |
| 27 | +``` |
1 | 28 | [
|
2 | 29 | {
|
3 |
| - "name": "branch", |
4 |
| - "value": "test" |
| 30 | + "name": "Parameter_1", |
| 31 | + "value": "ABC" |
5 | 32 | },
|
6 | 33 | {
|
7 |
| - "name": "commit", |
8 |
| - "value": "995102f73b8c64a4c412bcf99558ffdaec5edb19" |
| 34 | + "name": "Parameter_2", |
| 35 | + "value": "DEF" |
9 | 36 | }
|
10 |
| -] |
| 37 | +] |
| 38 | +``` |
| 39 | + |
| 40 | +5) Now we can create the environment. |
| 41 | +***Note:** Make sure the path for the parameters file is prefixed with @.* |
| 42 | +``` |
| 43 | +az lab environment create --resource-group '{DevTest Labs Resorce Group Name}' ` |
| 44 | + --lab-name '{DevTest Labs Name}' ` |
| 45 | + --artifact-source-name '{privaterepoXXX}' ` |
| 46 | + --arm-template '{ARM Template name}' ` |
| 47 | + --name '{Environement Name}' ` |
| 48 | + --parameter '@{Full Path to Parameters file}' |
| 49 | +``` |
| 50 | + |
| 51 | +6) Once the environment is created we can use the ```az resource list``` command to list the resources created in the environment. If the devtest lab is configured to create environments in their own resource group, we can find that in the JSON output from creating the environment in the ```resourceGroupId``` property. If we create all resources in the same resource group as the DevTest lab, then we can use tags in the ARM template to give us an easy way to find those resources. |
| 52 | +***Note:** Using tags to find resources will find all resources you have reader access to, regardless of what resource group they are in (you can't mix the ```--tag``` and ```--resource-group``` parameters ). Becuase of this use tags that are unique to your labs* |
| 53 | +An example of each is below: |
| 54 | +``` |
| 55 | +az resource list --resource-group {Environment Specific Resource Group Name} |
| 56 | +``` |
| 57 | +``` |
| 58 | +az resource list --tag tag1=abc --tag tag2=def |
| 59 | +``` |
| 60 | + |
| 61 | +7) Removing environments is simple. We delete the environment, and Azure removes all of the associated resources for us automatically. |
| 62 | +``` |
| 63 | +az lab environment delete --resource-group '{DevTest Labs Resorce Group Name}' ` |
| 64 | + --lab-name '{DevTest Labs Name}' ` |
| 65 | + --name '{Environement Name}' |
| 66 | +``` |
0 commit comments