The Ops Community ⚙️

Cover image for How to use AI for AWS CLI commands?
Jatin Mehrotra
Jatin Mehrotra

Posted on

How to use AI for AWS CLI commands?

In this blog, I want to show how I was able to set up and use AWS CodeWhisperer for the command line and enhance my productivity.

Just before AWS Re:Invent 2023 there is an amazing update from AWS which allows to use of AI for the command line. Yes it's true that from today we can use AWS CodeWhisperer with AWS CLI with provides CLI completion, inline documentation and AI natural language -> to CLI command generation.

Here is the link to the official announcement.

https://aws.amazon.com/about-aws/whats-new/2023/11/amazon-codewhisperer-command-line-preview/

AWS annoucement

Note: It's still in preview, so specifications about this blog may be subject to change in future.

CodeWhisperer for command line setup

At this moment codeWhisperer is available only on macOS as per official announcement

CodeWhisperer for command line is available on macOS for all major shells (bash, zsh, and fish) and major terminal emulators such as Terminal, iTerm2, Hyper, and the built-in terminals in Visual Studio Code and JetBrains.

Codewhisperer DMG file installation

  • Download DMG file and then move the file to Applications. (typical drag and drop for MacOS applications)

  • After installation, start the newly installed CodeWhisperer application

CodeWhisperer startup

  • Install the shell integration hooks as the next step in the installation.

Shell Integration

  • As a next step we need to enable accessibility so that CodeWhisperer can write on insert CLI completions and insert commands in the shell. CodeWhisperer will not work if this is not enabled.

Enable accessibility option

  • Toggle the accessibility for CodeWhisperer.

Enable accessibility toggle

  • Next step will ask to sign on. Upon clicking you will redirected to authorise CodeWhisperer for your AWS account.

Sign on option

Authorization Screen

  • You will need an AWS Builder ID. If you already have a Builder ID use your existing credentials otherwise create once as shown in image below. In my case I already have a Builder ID

Builder ID prompt

  • As a last step, if you have setup everything correctly so far, you will see a prompt in which you allow COdewhisperer to access your data so that CodeWhisperer can do the following:

    • Enable CodeWhisperer completions
    • Enable CodeWhisperer analysis
    • Access to IAM Identity centre accounts and permission sets.

Allow code whisperer to access data

Let's see CodeWhisperer in action for AWS CLI commands

  • Once the setup is complete, you will see the code whisperer default window.

Start screen after installation

Generating Commands from text

  • With CodeWhisperer you can write natural language ( many languages are supported) instructions like create s3 bucket or create a lambda function. Codewhisperer will then convert this text to an instantly executable shell command.

  • You can either use this feature in 2 ways:


  cw ai CodeWhisperer completions
  # CodeWhisperer completions // # is not the comment

Enter fullscreen mode Exit fullscreen mode
  • I tried to create cloudfront distribution whose origin is s3 bucket using the following prompt

cw ai create cloudfront distribution named test whose origin is s3 bucket name test
-bucket


Enter fullscreen mode Exit fullscreen mode

create cloudfront distribution

  • Once you have found your command you either execute it, or choose to edit it or regenerate, ask another question or cancel it.

  • You can write the prompt using # your prompt. In the following I tried to create a lambda in tokyo region and it did pretty good job.


# create cloudfront distribution named test whose origin is s3 bucket name test
-bucket

Enter fullscreen mode Exit fullscreen mode

create lambda

CLI completion

  • It also does a great job when it comes to CLI completion in the way it adds *typeahead code completions *

CLI completion

  • Not only AWS CLI commands, code whisperer has support for docker, kubectl (tested, git commands too.

docker command completion

Kubectl command completion

aws command completion

Bonus: Customization of Codewhisperer for CLI

You can always customise the behaviour of Codewhisperer from its dashboard which allows to change its theme, disable code whisperer for certain commands, instant execution of dangerous commands like bucket delete etc.

CodeWhisperer Dashboard

My thoughts

As a DevOps engineer who has to run certain repeated commands every day for daily tasks, I think this update will definitely increase my productivity and make my life way easier with its code completion and text-to-command feature.

As I said before it's in the preview stage, and cannot be used for production but it's going to get even better from this stage.

Connect with me over Linkedin or Twitter and share your thoughts about this blog.

Top comments (0)