Develop a LAMBDA function in Python using Amazon CodeWhisperer
Tags: aws, codewhisperer, lambda, machinelearning
Published: 2023-01-28 17:55:12

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjr978aguh0nf9pouu8f.png)


![Start CodeWhisperer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tni1e6lyud3iei6rm41t.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9dbb7q71nnh2hb7noil.png)

![confirmation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uro7p689w5w6op67s3hs.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvjlcgq5nhw8pgdg167x.png)

![Accept the terms of service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60hvc9okonffkvctp0hf.png)

## II. Working with CodeWhisperer in VS Code

Create a simple main.py 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kouq504k2zduojw1papy.png)
Write a function by typing def and see if you will get suggestions, if you  like then click Tab to accept.

![Code Suggestions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyucjzqy7bi0vis0wzp2.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2bn8y5vusfzhb1dxxcb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ja4hbr5ipywk6himhwuk.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tuuof3f5ziijtd45mw7h.gif)

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yuez6wxwixp75omanqc7.png)

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

![Code Suggestion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oymrlnvyfwdv8g7ehu69.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx5faxr7lk6g0vedtnl5.gif)
###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmktzhufjlyfg7dt34nw.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nhk2h4owe794d5vg9po.png)
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.

![LAMBDA code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87r2gk8m500jcjug7alk.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep6kfpn8h5ajbj6dm37h.png)

![LAMBDA code recommendatation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wepg648oztulnhgts39i.png)

![LAMBDA code recommendatation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66y5mgjxxf0rymijxlfl.png)


![LAMBDA code recommendatation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m38n0kmgtukp9g7g4zd2.png)


![LAMBDA code recommendatation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yw6sic0lirh6fczmp26.png)

###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 test_case.py for writing test cases and add comments based on your test cases.


![UT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbo07stcolyeuvfm8xaf.png)


![UT Recommendatations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z49d7wvx2hpzodwbnpn.gif)

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

![UT mock](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75f1mp8aw9kuzhrcjw2y.png)

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbv0px1t0vzr63ezqhdy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luf4rtrzmqbq890r73c3.png)
If the developer accepts (Tab) the recommendation, CodeWhisperer reference log tracks the corresponding licensing information

![Reference Log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6dasjmhnu44pdl2c8qd.png)
##Conclusion
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.
##References
https://aws.amazon.com/blogs/devops/develop-a-serverless-application-in-python-using-amazon-codewhisperer/
https://aws.amazon.com/codewhisperer/#How_to_get_started
https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/codewhisperer.html