Step-by-Step: How to setup and use Azure CLI Interactive mode
Tags: terminal, azure, cli, ai
Published: 2023-07-13 11:55:47

The Azure Command Line Interface (CLI) is a developer tool that allows users to control Azure services using command line commands. It is a cross-platform tool that can be installed in a number of ways (Windows, Mac, Linux, Docker and Azure Cloud Shell).

It also has the ability to have a number of extensions installed to add additional functionality, assisting with your development and deployments to the cloud. One of these extensions is the newly released AZ Interactive extension that is **now in preview**.

## Useful links:
- [Install Azure CLI](https://aka.ms/azi-blog-install-az-cli)
- [Azure CLI Interactive documentation](https://aka.ms/azi-blog-documentation)
- [Azure Interactive GitHub Guide](https://aka.ms/azi-blog-github-guide)

## What is AZ Interactive?

Using any CLI can sometimes be cumbersome, especially if you are new to the tool. AZ Interactive is a new AI-based extension for the Azure CLI and somewhat of a cheat code for developers. It allows you to interact with Azure resources in a more interactive and intuitive way than usual. It is a great tool for those who are new to the Azure CLI and want to learn how to use it, or for those who are more experienced and want to be more efficient.

It sets out to solve a few problems that developers face when using the Azure CLI:

- Discoverability of services, commands and parameters
- Reducing the time taken to learn the Azure CLI and its commands
- Speed up the process of writing commands and deploying resources

## Prerequisites

To use AZ Interactive, you will need to have the Azure CLI installed. You can find instructions on how to install the Azure CLI [here](https://aka.ms/azi-blog-install-az-cli).

## Installing AZ Interactive

To install the AZ Interactive extension, you will need to run the following command from your terminal (once you have the Azure CLI installed):

```bash
az extension add --name interactive --upgrade
```

This command will install the extension and any dependencies that are required. Once the installation is complete, you can run the following command to start the interactive mode:

```bash
az interactive
```

Those are the only commands you need to install and start using AZ Interactive. It is that simple!

Once you have it running, your terminal will look something like this:

![azure interactive default view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohgfnvgdj9klkti5au57.png)

## Using AZ Interactive

Now you have this awesome tool installed, what sort of super power does it provide? Let's take a look at some of the features.

### Command Suggestions

One of the great features of AZ Interactive is the command suggestions. As you type, it will suggest commands that match what you have typed. It is somewhat similar to tab completion you may find in other tools, but it is a lot more intuitive and it will match commands based on what you have typed so far and allow you to chose from a list.

![azure interactive command suggestions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ld0nqsvkrhuev7krk01.png)

### Command Scenarios

Another great feature of AZ Interactive is the command scenarios. This is where AZ Interactive allows you to select a scenario and then it will guide you through the process of creating the resources required for that scenario and provide you with the commands needed to do so.

![azure interactive command scenarios](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnw3z8vvx3vdfnlezjx5.png)

### Search for commands and scenarios

You also have a search capability that allows you to search for commands, parameters and resources. This is great if you know what you are looking for and want to have the CLI generate the command(s) for you. For instance, if you search with the prompt "connect an app service to mongodb", it will generate a number of commands within a scenario that you can run.

![azure interactive searching for commands and scenarios](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gz996jtszuv6iobov61x.png)

### Running commands outside of interactive mode

You may notice that you are abstracted away from your normal terminal shell at this point and tucked into an "az interactive" shell. This does not mean that you cannot run commands outside of interactive mode. You can run any command you like by prefixing it with "#". This will run the command as normal and return the output to the terminal for you. For example, if I want to know my current working directory, I would typically run the command "pwd". In interactive mode, I would run "#pwd" and it would return the output to the terminal in the same manner.

![azure interactive running native terminal commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa9tz2daqf2n4swtvv4g.png)

### JMESPath query support

This is a powerful feature and probably one of my favourites. JMESPath is a query language for JSON that allows you to extract and transform elements of JSON. AZ Interactive supports JMESPath queries and allows you to run them against the output of commands. This is great if you want to filter the output of a command to only return the data you are interested in. For example, if I want to get a the name of my resource group from the JSON object returned in a previous command I can do just that by using `??name` as seen in the example below.

![azure interactive jmespath query support](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjkbja37xsr3qvy30df1.png)

This is not all either. You can also use this to inject jmespath query into future commands allowing you to use this data throughout your session. For example, if I want to get the name of my resource group and use it in a command to delete the resource group, I can do that by using `??name` in the command as seen below.

![azure interactive jmespath query support in a new command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oa6u63trhtqvd411gjpc.png)

### Status codes

When running terminal commands you may be used to seeing a status code returned. This is a number that indicates whether the command was successful or not. This is also supported within this extension and you can find the previous status code of each command by simply using the `$` symbol. This will return the status code of the previous command.

I previously ran a command to delete a resource group and it returned a status code of 0, indicating that the command was successful.

![azure interactive command status codes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybk5qf1ypzhw7q6mehop.png)

### Setting Scope

When you are working within a certain command group you can set the scope of the commands. This means that you can run commands without having to specify the command group every time. For example, if I want to run a number of commands to work within resource groups, I can set the scope to be groups using the command `%% group`. From there, I can run any command within that group without having to specify the prefix every time. This saves time and acts as a shorthand for you.

![azure interactive setting terminal scope](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6el0p22zv7qt02wcngvv.png)

### Fun colour themes

This is a fun way to spice up your terminal. You have the ability to change the colours you want to use with a number of different styles. This changes the text colour, drop down lists and more!

![azure interactive setting theme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yryth5sa4w7ic84aybgm.png)

![azure interactive new theme example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3i9l0jp78o7inme4ik2o.png)

By default, all of the AI capabilities are turned on but if you are not a fan of having all the noise in your terminal and you want to get back to the basics, you can turn it off by running the command `az config set interactive.enable_recommender=False`.

Overall this is a great tool to use and I would highly recommend it to anyone who is new to the Azure CLI or wants to speed up their development and deployment processes from within the terminal. It is a great tool to have in your arsenal and I am sure it will only get better as it is developed further.

For more examples of how to use the Azure CLI Interactive mode, you can checkout the documentation [here](https://aka.ms/azi-blog-documentation).

Happy terminal'ing!

If you have any questions or some feedback, I'd love to hear so feel free to drop me a line 📝

[Twitter](https://twitter.com/liamchampton)
[LinkedIn](https://www.linkedin.com/in/liam-conroy-hampton/)