Post

Integrating your own cli tool into aws cli

We are using AWS Command Line Interface (CLI) in multiple projects and daily operations. The aws cli is a propriatary tool for managing AWS services and resources from the command line. It is maintained as a dedicated application written on Python https://github.com/aws/aws-cli with its own SDLC and build system.

Besides standard functionality aws cli it has the ability to integrate custom commands and CLI tools by registering your own alises to invoke any application or even integrate other cli tools into aws cli thereby enhancing its functionality and adapting it to specific workflows.

In this post we will go through the process of registering any other cli tool in aws cli.

Prerequisites

First, ensure that you have the following:

AWS CLI Installed: You can install AWS CLI via Homebrew with the following command

1
$ brew install awscli

AWS CLI Basics

AWS CLI commands follow a consistent structure to operate:

1
$ aws <command> <subcommand> [options and parameters]
  • Command: Typically a service name (e.g., s3, ec2).
  • Subcommand: Specific operation within the service (e.g., ls, describe-instances).

Target CLI tool to integrate with

Let’s integrate a custom CLI tool called mcdc, which explores AWS storage services and renders them in the draw.io format.

Here is a syntax to invoke it:

1
2
3
4
5
6
7
8
9
10
11
$ mcdc aws dynamodb --help

Usage: mcdc aws dynamodb [OPTIONS] COMMAND [ARGS]...

╭─ Options ────────────────────────────────────────────╮
│ --help          Show this message and exit.          │
╰──────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────╮
│ get                                                  │
│ list                                                 │
╰──────────────────────────────────────────────────────╯

Adding alias

A great feature is the ability to inject custom commands into the section. These commands can be other CLI tools, applications, or even bash scripts:

Register alias of other cli tool

AWS CLI allows custom command aliases to be defined in a file located at ~/.aws/cli/alias. This file specifies the alias and the command it maps to.

1
2
3
4
5
6
[toplevel]

mcdc =
  !f() {
    mcdc aws $@
  }; f

Remember to include double spaces at the end of the next line in your code block, similar to a code section in IDEA. Failing to do so will prevent the AWS CLI parser from correctly parsing your command.

Verify the Integration

Following invocations will be absolutely the same now:

Invoke mcdc using its own cli tool:

1
$ mcdc aws dynamodb --help

To ensure that your custom tool is correctly integrated, you can invoke it via aws cli:

1
2
3
4
5
6
7
8
9
10
11
$ aws mcdc dynamodb --help

Usage: mcdc aws dynamodb [OPTIONS] COMMAND [ARGS]...

╭─ Options ────────────────────────────────────────────╮
│ --help          Show this message and exit.          │
╰──────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────╮
│ get                                                  │
│ list                                                 │
╰──────────────────────────────────────────────────────╯

Registering alias as a shortcut to aws command with subcommand

Let’s continue editing ~/.aws/cli/alias file and add one more section [cloud-based]

As with Unix aliases same way we can shortcut long aws commands with multiple parameters into a single aliased command:

1
2
3
[cloud-based]

whoami = sts get-caller-identity

Usage of subcommand alias:

1
2
3
4
5
6
7
8
$ aws whoaim

{
  "UserId": "xxxx:xxx@xxx.com",
  "Account": xxxxx,
  "Arn": "arn:aws:sts::xxxxxxxxx:assumed-role/AWSSSO/xxx.xxx@xxx.com"
}

Registering alias as any bash cli script

1
2
3
4
5
6
[network]

myip =
  !f() {
    dig +short myip.opendns.com @resolver1.opendns.com
  }; f

Usage of script alias:

1
2
$ aws myip
1xx.1xx.1xx.1xx

Tips for Integration

  • Follow convention in alias blocks: double Spaces when defining aliases in the alias file, ensure to include double spaces at the end of the next line. This is similar to code blocks in some IDEs. Failure to do so may cause parsing errors in AWS CLI.
  • Testing: Always test your aliases to ensure they work as expected before integrating them into your workflow.
  • Documentation: Maintain documentation for your custom integrations to assist team members in understanding and utilizing these enhancements.

Conclusions

AWS CLI offers a convenient feature to extend its commands with user-defined aliases. This capability is easy to use and opens up many possibilities for integrating with cloud-related projects, tools, and CLI scripts. Take advantage of this feature to enhance and streamline your workflows.

This post is licensed under CC BY 4.0 by the author.