Write Code with Machine Learning using Amazon CodeWhisperer

Utilizing machine learning suggestions for code development.

Michael Sambol
5 min readAug 25, 2023

Introduction

In this blog I’ll show you how to use Amazon CodeWhisperer to aid your code development with machine learning. CodeWhisperer is trained on billions of lines of code and generates single-line, full function, and block completions. It also uses your existing code and comments to match your development style and naming conventions.

To demonstrate, I’ll use Visual Studio Code (VS Code) with Python and TypeScript code from my previous blog, Process Millions of Amazon S3 Objects. It feels a little like I’m writing about my own demise as a software developer, but I’d rather embrace change than get left in the dust.

Set Up

The first step is enabling CodeWhisper in your IDE, and there are two options for using it: CodeWhisperer Professional and CodeWhisperer Individual. We’ll use the individual option, as it’s free and quick to set up.

Authentication

To use CodeWhisperer with your IDE, an authenticated connection to AWS is required (this is different than an AWS account). We’ll use AWS Builder ID, a personal profile that’s independent of your company or school. To sign up, use this link and follow the prompts.

Install AWS Toolkit

The next step is to install AWS Toolkit for Visual Studio Code, which can be done by clicking on this link. From the webpage, click Install and you’ll see the following in VS Code:

On the left-hand side of VS Code, click on the AWS icon and then Start under CodeWhisperer:

VS Code will open a new window. Follow the prompts to sign in with your AWS Builder ID. VS Code will open a webpage. Paste the access code and authorize the connection. If all went well, you’ll see the following in VS Code:

Application Code

Time to write some code. I’ll start with the application code (AWS Lambda functions) from my previous blog, Process Millions of Amazon S3 Objects:

1. loader.py: list and load Amazon S3 (S3) object pointers into an Amazon Simple Queue Service (SQS) queue

2. worker.py: poll the SQS queue and process S3 objects

I’ll demonstrate with loader.py (worker.py will be similar) and begin by typing my previous code, documenting the suggestions offered by CodeWhisperer. I typed the following:

import boto3

s3 =

CodeWhisperer knew I wanted to create a boto3 S3 client (gray is the suggestion):

Nice! Next I started defining my handler function, and CodeWhisperer suggested the following code:

This isn’t what I wanted, but it provides a template for the Lambda function to read records from an SQS queue. You can press the left and right arrow to see other CodeWhisperer recommendations (the full list of CodeWhisperer commands is here). Below is another recommendation offered:

This is still not what I wanted as loader.py is a fairly custom Lambda function, but CodeWhisperer provided great boilerplate code for standard use cases.

I pasted in the rest of the handler function verbatim, and started writing helper functions. I began defining the function to send messages to SQS, and CodeWhisperer filled in the rest — impressive!

CodeWhisperer also has the ability to suggest comments. Below is a suggestion for adding a docstring comment to the send_sqs_batch function:

Infrastructure Code

In my previous blog I deployed the solution with AWS Cloud Development Kit (CDK). Let’s experiment with CDK Typescript code and see how CodeWhisperer performs. I started creating an SQS queue, and CodeWhisperer suggested queueName as a construct prop:

Here’s a similar situation when creating a Lambda function. CodeWhisperer suggests a functionName as a construct prop:

Security Scan

CodeWhisperer also has the ability to perform security scans of your code. To run a scan, click on Run Security Scan on the left-hand side of VS Code:

No security flaws were found for the blog code, but running it on another repo, I found SQL injection vulnerabilities:

Takeaways

I’m not ready to close my laptop and hit the unemployment line, but CodeWhisperer provides decent autocomplete recommendations for basic use cases. For beginner developers, this will certainly come in handy. For more experienced developers, it will take some getting used to, as it may be more inefficient to read and digest suggestions rather than writing the code from scratch. CodeWhisperer improves recommendations as you write more code and comments, so using it on a mature repo will have better results. I think CodeWhisperer has a place in my development workflow, in conjunction with other VS Code extensions.

Conclusion

In this blog I demonstrated how to use CodeWhisperer and machine learning to aid code development. Drop me a note if you have questions or comments.

--

--

Michael Sambol

Software engineer and sporadic YouTuber. I write about software development and make videos on data structures and algorithms.