AWS CLI Made Easy with Amazon Q
Amazon Q is an AI coding assistant that has all the features you'd expect, but also includes one you might not: a command line assistant. Using Q for the Command Line can make it easier to work with the AWS CLI.
The AWS CLI is massive! By my count, AWS CLI v2 includes 392 base level commands. That number seemed so astronomical to me that I double and triple checked it and yet it doesn’t even include subcommands and option flags.
While you are unlikely to ever need to work with every service in AWS, even a subset of commands for the services you do interact would still likely encompass a large list of commands, subcommands and available options. On the upside, LocalStack’s AWS CLI wrapper doesn’t require that you learn any new commands or options since it just a wrapper that redirects existing commands to the locally hosted services – meaning everything you know about the AWS CLI applies to awslocal
.
If you, like me, have not memorized all the relevant commands and options that you need to work with, I have some good news. Amazon Q, Amazon’s generative AI assistant, has a CLI integration that can be a huge help in generating the commands you need without requiring any memorization. Let’s look at how to get it set up, how to use it and where you can adapt the generated commands for LocalStack.
What Is Amazon Q for the Command Line?
Amazon Q Developer (formerly CodeWhisperer) is an AI assistant for coding similar to GitHub Copilot. It can do things like code suggestions within your editor, offer chat-based coding help, or even write/modify your code for you. If you’ve used an AI coding assistant before, these are all features you’d expect.
However, it also includes a full set of command line functionality including hinting, chat and a translate feature that can take natural language and turn it into command line commands. As we’ll see, these features can be incredibly useful for speeding up your command line usage (especially if, like me, you aren’t necessarily a command line expert).
At this time, Amazon Q for the Command Line is only available on MacOS.
Getting Q for the Command Line Set Up
There are a number of different ways to install Amazon Q, but the command line assistance requires a separate installer and application that you need to run. To get started, you can download Amazon Q Developer for the command line and install it.
Once installed, open the application and there are a number of settings, though most of what you’ll need is enabled by default (or was for me, anyway). For example, if you want to ensure that you can use Q on the terminal within VS Code, head to the integrations:
There are also a lot of configuration settings that you can toggle on/off regarding how Q handles command line completions. For the purposes of this article, I should note that I am only using all the default settings.
Using Q Command Line Hinting
The most common way that you’ll use Q is likely for command line hinting. This is similar to how code suggestions work in your code editor.
As you type, Q will offer suggestions. These suggestions are context specific. If you see the gif below, you’ll see that it suggests folders as I attempt to change directory but then switch to suggesting npm tasks based upon the package.json
in the folder when I use the npm
command. (If you’re paying attention, you’ll also notice that it suggested an autocomplete for the full path for the folder I wanted because it remembers commands that I’ve entered before).
If you use the CDK, Q will suggest CDK commands for you.
Unfortunately, Q is not aware of the LocalStack CDK wrapper (cdklocal
) so you’ll either need to manually change the command it suggests once you are done or manually append --endpoint-url http://localhost:4566
, which is the default local hostname and port for LocalStack.
As you might expect, Q pretty much knows everything about AWS CLI commands. This makes it really easy to remember the syntax and options for pretty much anything you might want to do with the CLI. Below you can see it hinting me both the commands and options for the specific command I am executing against DynamoDB (as well as the type of value the option expects).
That one might be a little simple, so let’s look at an example where I am loading a local file with my JSON payload for a Lambda. Notice that it not only gives me context-based suggestions but even allows me to browse through local files to use as my payload.
Using Q Natural Language Translation
Context-based suggestions are probably how you’d typically use Amazon Q, but that’s also most helpful when you know the specifics already of what you need to do. Scrolling through a long list of commands and options isn’t going to help if you need more complete guidance. This is where Q’s natural language translation can help.
Natural language translation using q translate
can take a description of what you need to do and turn it into a command line command. Obviously, this works for more than just the AWS CLI but it can be a big help if you are unsure of how to compose the proper command and options. Even better for LocalStack users, you can force it to convert that command to work with LocalStack.
Let’s try to recreate a Lambda invocation equivalent to the one above but using just a description of what we want. Keeping in mind that I am not an experience “prompt engineer”, here’s the text I am entering:
Invoke a lambda with the function name IdempotencyStack-idempotencyFunction1D2EE063-20UoNvd7admR and a JSON payload from the local file payload.json and export it to output.txt
Entering that gives me exactly what I intended, so I can execute it directly. If I needed to make a change I could choose the option to edit the command, which would place the text of the command on the command line but not execute it.
As I mentioned, we can get q translate
to work with LocalStack, all I need to do is add a slight tweak to my command.
Invoke a lambda with the function name IdempotencyStack-idempotencyFunction1D2EE-d5cd275f and a JSON payload from the local file payload.json and export it to output.txt. Avoid base64 errors. Use awslocal instead of aws
Note the Use awslocal instead of aws
, which is all that is needed to force Q to convert the command – and since awslocal
is just a lightweight wrapper around the AWS CLI that redirects commands to LocalStack, we know it’ll translate equivalently.
Attentive readers may notice another difference in the above text. I added the phrase Avoid base64 errors.
As is typical with AI tools, the results can be slightly unpredictable. The initial prompt didn’t include the necessary flags --cli-binary-format raw-in-base64-out
to avoid an Invalid base64
error with AWS CLI version 2 (note the earlier example added it automatically without additional prompting). This unpredictability can be a weakness of AI-based tools. However, when I received the error, all I needed to do was tweak the prompt and it was fixed. I didn’t need to search for the answer and dig through search results to find it – something I’ve done before for this exact problem. So, this can also be seen a strength of AI based tools, allowing you to tell it to ask it to solve a problem you might encounter for you.
In one final test, I wanted to see if it could do something even more complex and get the name of the function for me before invoking it. Here’s the prompt:
Get a list of available lambdas and then choose the one where the name of the lambda the contains idempotencyFunction and then invoke that lambda with the JSON payload from the local file payload.json and export it to output.txt. Avoid base64 errors. Use awslocal instead of aws
This almost worked out-of-the-box. Here’s the command it provided:
awslocal lambda list-functions --query "Functions[?FunctionName.contains(@, 'idempotencyFunction')].FunctionArn" --output text | xargs -I{} awslocal lambda invoke --function-name {} --payload file://payload.json --cli-binary-format raw output.txt
Executing that command as is returns:
--cli-binary-format: Invalid choice, valid choices are:
base64 | raw-in-base64-out
Again, generative AI tends to be imperfect. The fix in this case is easy (change raw
to raw-in-base64-out
), but it’s worth noting as there is a chance for more potentially destructive mistakes. Always proceed with caution. Testing against LocalStack first can help prevent any damage.
Anything else?
Alternatively, I could have used Q to help me debug the problem via chat if I was unable to tweak my prompt to get the correct solution. I’d start a chat session with q chat
and then I could ask it how to fix an Invalid base64
error when invoking a Lambda. You can even pass in the shell history, among other data, to inform your chat. This will, of course, give me a much more verbose set of solutions and explanations, which could be helpful context in some situations.
While we’ve covered the main usage of Q for the Command Line there are a lot of additional options for you to explore in the Q dashboard. Also, while I have focused on using Q for AWS CLI and LocalStack’s awslocal
, it is not limited to assisting with AWS-specific CLI commands and can be an incredibly helpful assistant to all of your command line work.