DEV Community

Cover image for Build your own Azure CLI Extensions
Michael Crump for Microsoft Azure

Posted on

Build your own Azure CLI Extensions

This is Part 2 of 2. You can check out Part 1 below.

Build your own Azure CLI Extensions

Azure CLI extensions are really helpful. You can read about them in this Azure Tip. You can use extensions from the list here, which you can also get when you enter the az extension list-available --output table command in the Azure CLI.

And you can also build Azure CLI extensions yourself. You do that by creating a Python wheel, which is a package of Python code.

Let me show you how you can create and use your own Azure CLI extension.

Creating an Azure CLI Extension

Azure CLI extensions can currently only been Python wheel packages. So to create a new extension, you need to have the following prerequisites installed on your development machine:

  • Python (version 2.7.9 or 3.4 or up). Download it here
  • Python wheel (once Python is installed, you can get wheel by using the command pip install wheel)

Now that we have Python and wheel installed, we can start to create the extension.

1.) We'll start by creating a new folder that holds all of the files that we need for the extension. Let's call it Tipsextension
2.) In the Tipsextension folder, we'll create some files that make up the extension. These are:

  • (folder) azext_tipsextension
  • setup.cfg

3.) Now, we will fill in the content of the files. We'll start with the file. This file will tell the Azure CLI what is in the extension. We'll put in this code:

from codecs import open
from setuptools import setup, find_packages

VERSION = "0.0.1"

    'Development Status :: 4 - Beta',
    'Intended Audience :: Developers',
    'Intended Audience :: System Administrators',
    'Programming Language :: Python',
    'Programming Language :: Python :: 2',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3',
    'Programming Language :: Python :: 3.4',
    'Programming Language :: Python :: 3.5',
    'Programming Language :: Python :: 3.6',
    'License :: OSI Approved :: MIT License',


    description='My CLI extension',
    long_description='An example Azure CLI Extension.',
    author='MY CORP.',
Enter fullscreen mode Exit fullscreen mode

4.) The next file that we are going to fill, is the setup.cfg file. This file will be used by Python wheel to create the package that the CLI can use. This file is short and will contain only this code:

Enter fullscreen mode Exit fullscreen mode

5.) The last file that we'll fill is the file in the azext_tipsextension folder. This file contains the actual functionality of the extension. It is written in Python. I'm not a Python expert myself, but it's easy enough to pick up. We'll put this code in the file:

from knack.help_files import helps

from azure.cli.core import AzCommandsLoader

helps['gimme tips'] = """
    type: command
    short-summary: Points you to a world of Azure Tips and Tricks.

def showtipsurl():
    print('Azure Tips and Tricks - The Complete List:')

class TipsAndTricksCommandsLoader(AzCommandsLoader):

    def __init__(self, cli_ctx=None):
        from azure.cli.core.commands import CliCommandType
        custom_type = CliCommandType(operations_tmpl='azext_tipsextension#{}')
        super(TipsAndTricksCommandsLoader, self).__init__(cli_ctx=cli_ctx,

    def load_command_table(self, args):
        with self.command_group('gimme') as g:
            g.custom_command('tips', 'showtipsurl')
        return self.command_table

    def load_arguments(self, _):

COMMAND_LOADER_CLS = TipsAndTricksCommandsLoader
Enter fullscreen mode Exit fullscreen mode

6.) Next, we need to build the application and compile it into a wheel package. We can do that with the command below. The directory should match the directory that contains all of the extension files

cd /Source/extension/Tipsextension
python bdist_wheel
Enter fullscreen mode Exit fullscreen mode

This output of the build result looks like this and produces a .whl file.

Alt Text

(Results of building the extension)

7.) Now, we can try the extension out. We can do that by installing it with the following command

az extension add --source C:\Source\extension\tipsextension\dist\tipsextension-0.0.1-py2.py3-none-any.whl
Enter fullscreen mode Exit fullscreen mode

8.) When the extension is installed, you can see the help by using az gimme tips -h or get the results by using az gimme tips

Alt Text

(Trying the extension)

The above is the happy flow of developing an Azure CLI Extension. Usually, you need to debug the extension and have more control when you are developing it. You can read more about that here. And you can also publish the extension so that people can start using it. You can read about how to do that here.

Here are some of the published CLI Extensions that I find very useful:

  • find, which helps you to get contextual information with the CLI
  • webapp, which has some extra commands for managing Web Apps, ike creating a new one from the CLI
  • resource-graph, which enables you to query the Azure Resource Graph


Azure CLI extensions are a very powerful way to make the CLI work for you. The steps to develop an Azure CLI extension are relatively easy. The downside (to me) is that it is currently only possible to develop the extensions in Python. Maybe in the future, other languages will be supported. In any case, it is wonderful that it is possible to extend the CLI. Go and develop your ultimate extension and share it with the community!

Stay connected with me on various social platforms for daily software development news.

Top comments (0)