DEV Community

Pavan Gorpuni
Pavan Gorpuni

Posted on


Develop a LAMBDA function in Python using Amazon CodeWhisperer

Amazon CodeWhisperer, a machine learning (ML)-powered service that helps to improve the developer's productivity by providing code recommendations based on the developer's natural comments and prior code. It builds the specific code on the fly and recommends the generated code snippets directly in the IDE. Developers can find the code snippets in StackOverflow or any public forum but it's always better to get automatically generated code by the AWS CodeWhisperer's tool as it follows the correct programming syntax and best coding practices.

Moreover, CodeWhisperer seamlessly integrates with your Visual Studio Code and JetBrains IDEs, and Cloud9. So, you can stay focused and never leave the development environment. CodeWhisperer supports Java, Python, JavaScript, C#, and TypeScript. CodeWhisperer code suggestions feature is also available on the AWS Lambda console as a tool.

In this post, I will share my initial thoughts on using AWS CodeWhisperer (Preview) tool specific to the below points.
a. Set up CodeWhisperer in Visual Studio code locally. 
b. Develop the LAMBDA code in IDE and in the LAMBDA console for a simple serverless event-driven application (Ex: S3 event, Put Item to DynamoDB, and Kinesis events to LAMBDA).
c. Unit test case preparation capability.
d. Security scan and Reference Log features.

I. Setting up CodeWhisperer:

CodeWhisperer is available as part of the AWS Toolkit extensions for major IDEs, including JetBrains, Visual Studio Code, and AWS Cloud9.
In the Toolkit for Visual Studio under Developer tools, under CodeWhisperer, click Start to connect. A drop down menu will appear at the top of VS Code.

Install AWS Toolkit

Start CodeWhisperer

From the dropdown menu, select Use a personal email to sign up and sign in with AWS Builder ID
A browser tab will open to the Create AWS Builder ID page. Register with your email address and complete the setup by validating the email address.
A browser tab will open with a message asking you to allow AWS Toolkit for Visual Studio Code to access your data.

Allow CodeWhisperer


You will be prompted to review and accept the terms of service. Agree on the terms and conditions and now you are logged on to CodeWhisperer with Builder ID

Terms of Service

Accept the terms of service

II. Working with CodeWhisperer in VS Code

Create a simple file and start writing import boto3 and when you enter the next line import automatic suggestions by the tool appears
Use Tab to accept the suggestions and Arrow keys to get the next and previous suggestions (ALT+C for manual suggestions).

VS Code
Write a function by typing def and see if you will get suggestions, if you like then click Tab to accept.

Code Suggestions

III. Use cases

S3 event notification to LAMBDA function:

When you type comment S3 bucket from event then automatically recommendation showing up on how to read bucket name from the event.

code suggestions

Write a comment for getting the key name from event then code suggestions appear, just click on Tab or use the arrow keys for more suggestions.

code suggestions

If we just give some hints on what are you planning to implement then automatically CodeWhisperer suggests the code needed without looking for boto3 documentation or other developer platforms.

Code Suggestions based on comments

Write Json data to DynamoDB table:

Write comments you need a new function which will insert Json data to DynamoDB.

Write a function to load DynamoDB

Just type def then automatically function definition appears for write_dynamo defined above

Code Suggestion

Now we need DynamoDB client, table name, and command to put items and all can be done effortlessly without looking into any documentation by AWS CodeWhisperer.

DynamoDB client and function

Code suggestions from LAMBDA Console

We can also get code suggestions from LAMBDA console while making code changes as shown below
Go to Tools and select Amazon CodeWhisperer Code Suggestions

LAMBDA Console
A new pop-up shows up which expects to read the terms and conditions and accept/Reject. Accept the terms and then you are ready for getting suggestions in the LAMBDA console itself.

Accept the terms
In LAMBDA console we need to press ALT+C to get the code suggestions from the CodeWhisperer tool and Enter to accept the suggestion (For each line we have to press ALT+C and Enter which is tedious.)
Add line import boto3 to the code, So CodeWhisperer can understand and give suggestions related to AWS SDK easily.

Ex: This LAMBDA will be triggered by a Kinesis Data streams, So first we need to connect to Kinesis Client and read the event from the LAMBDA event. (Sometimes it's very hard to give proper comments to get what we wanted)

LAMBDA code recommendatation

LAMBDA code recommendatation

LAMBDA code recommendatation

LAMBDA code recommendatation

LAMBDA code recommendatation

Unit test cases preparation (need to explore more):

CodeWhisperer can be used to prepare your unit test cases, where most of the developers takes time.
Create a new for writing test cases and add comments based on your test cases.


UT Recommendatations

You can also run test cases using mock resources by importing it

UT mock

IV. Security

CodeWhisperer provides the ability to run scans on your code (generated by CodeWhisperer as well as written by you) to detect security vulnerabilities. When you choose Run Security Scan, CodeWhisperer detects this vulnerability and displays the issue.

Security scan

CodeWhisperer tells you in real-time that the current code recommendation you're seeing may be similar to a reference code by showing a notification in the recommendations pop-up.

Reference tracker
If the developer accepts (Tab) the recommendation, CodeWhisperer reference log tracks the corresponding licensing information

Reference Log


In this post, we've seen how to improve developer's productivity by using machine learning (ML)-powered service AWS CodeWhisperer(Preview). With this feature, developers stay focused on IDE or LAMBDA console which will reduce the time that they spend searching online for code snippets that are relevant and never have to leave their editor. CodeWhisperer also provides the ability to run scans on your code to detect security vulnerabilities and a reference log tracker to track if the code suggestion chosen is from any license or matches with some open-source code. It's up to the Developers who can accept the top recommendations, view more recommendations or continue writing their own code.


Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git