DEV Community

Alina Dima for IoT Builders

Posted on

Coding IoT with Amazon CodeWhisperer AI Coding Companion

Amazon CodeWhisperer AI Coding Companion has been generally available since April 13th, 2023. Naturally, curiosity got the best of me, so I decided to see if I could rapidly build an IoT application, and have the AI do most of the coding.

The goal of this experiment was to set up the foundations of a simple IoT application, in about 30 minutes. In terms of tools, I used AWS CDK for infrastructure (TypeScript). I had not used CDK before, even if I had years of experience with AWS CloudFormation, SAM, Amplify. For the IoT app itself, I used JavaScript, the MQTT.js library, and the AWS SDK v3 for the AWS IoT Control Plane calls. The idea behind mixing JavaScript and TypeScript was to evaluate how well CodeWhisper could assist me in both languages.

Curious how good a team CodeWhisperer and me were in this experiment? Keep reading, because I will cover some of the great and no so great experiences of working with CodeWhisperer.

For those interested in the build details, I have recorded the entire experience. It is available in a 2-episode series on the IoT Builders YouTube channel.

Part 1 - CDK for IoT

Part 1 - Building the IoT App

The code is also available on GitHub: http://bit.ly/cdk-iot-sample

What did I build?

The IoT application is shown in the diagram below. We have an AWS IoT Thing, connected to AWS IoT Core, subscribed to an MQTT topic, and publishing data on another MQTT topic. An AWS IoT Rule picks up the data and invokes a Lambda action which prints out the MQTT message.
IoT App Example

This application was built in 2 steps:

  1. The AWS Resources were created using CDK: an IoT Thing policy based on least privilege best practices, an IoT Rule, a Lambda action and of course the Resource policy allowing Lambda invocation from the IoT Rule.
  2. The IoT application code was written to: create an IoT Thing, create the identity, attach the IoT policy to the certificate, and the certificate to the Thing, create and configure the MQTT client, connect the client to IoT Core, subscribe and publish data.

Amazon CodeWhisperer helped in both steps listed above. It was used to speed up development, by making whole-line and full-function code completions, automatically generating functions and code from natural language comments, and accelerating work with third party libraries like MQTT.js.

Getting started with Amazon CodeWhisperer

As a user of intellij IDEA, I have integrated CodeWhisperer into my IDE in 3 easy steps:

  • Step 1: Installed the latest_ AWS Toolkit plugin_ in IDEA.
  • Step 2: In the IDE, open the AWS extension panel and click the “Start” button under Developer Tools → CodeWhisperer
  • Step 3: In the resulting pop-up, select the “Sign in with Builder ID” option. Use your personal email address to sign up and sign in with AWS Builder ID.

CodeWhisperer SetUp

What was the Developer Experience like with CodeWhisperer for IoT?

First off, as we saw above, CodeWhisperer is super easy to set up and start.

Generating Code from Comments

CodeWhisperer was efficient at generating code from comments. Some interesting examples were:

  • When prompted with the comment: // Create AWS Lambda function, with inline code.

the result was:

AI Lambda

  • Here is another example of CodeWhisperer coming up with the code to create an IoT thing when prompted by comment:

AI create IoT Thing

Auto-Completion

CodeWhisperer is a clever tool for auto-completion. Below is an example of how I fixed the initially incorrect IoT policy using the auto-completion assistance of CodeWhisperer:

AI IoT policy fix

A Problematic IoT Policy

While trying to get assistance from CodeWhisperer, I struggled with the creation of an IoT policy, using the CDK SDK for Typescript. It took a few attempts at changing my comment prompts to get to an overly permissive and incorrect IoT policy (see below).

Incorrect IoT Policy

The comment I provided was:

// Create IoT policy for IoT Core. Restrict to account id and region. Allow connect with thing name, subscribe to topic and publish on topic.

Coming up with a correct policy, available here, was eventually more effort than expected, although, as shown in the video above, CodeWhisperer did a very good job with auto-completion.

Coaxing CodeWhisperer into Creating Correct Code

Sometimes, CodeWhisperer generates code that looks just about right, but actually it is not. In fact, sometimes I got tricked into skipping the correct suggestion and went for the incorrect one.

So, as a developer, you need to take care to pick the correct suggestions. Below is an example:

Incorrect SDK calls AI

How to make the most of CodeWhisperer?

If you want to make the most of working with CodeWhisperer, the following helps:

  • In general, the more code you already have, the better CodeWhisperer will do.
  • Do your main imports first - if you want to use the Control Plane calls for IoT Core, like in my example, import the ClientIoT as a first thing. Afterwards, you CodeWhisperer will anticipate better what you are prompting it to do in your comments.
  • Be very explicit in your comments - if you want a JavaScript Lambda function, write that in your comments. Be explicit with the type of resources, the nature of code (if you want a function, say so). The more detailed and explicit your comments are, the better success you will have.
  • Pay attention - sometimes CodeWhisperer provides multiple suggestions, and if you do not pay a lot of attention, you get tricked into choosing the incorrect one (as in the example above). Scroll fast between the proposals, and choose the correct one.
  • Know when to give up and move on - if full code generation from comments does not work out-of-the-box (like in my example with the IoT policy), count your loses on this one, and use the AWS documentation to start it off correctly, and then use CodeWhisperer for auto-completion, once you are on the right track.

Conclusion and Useful Links

To sum up, the experiment of building an IoT application with the assistance of CodeWhisperer was a success. I aimed for 30 minutes, but in reality it probably took about 40, excluding explanations, introduction and conclusions parts of the video, or written this blog.

If you want to watch the entire experience, it is available in 2 episodes on YouTube:

The code for the IoT application is available on GitHub: http://bit.ly/cdk-iot-sample.

Amazon CodeWhisperer is an AI-powered coding assistant that provides real-time recommendations in your IDE based on your existing code and comments. The tool is available for and can integrate well with various IDEs, including JetBrains. This post talks about my developer experience building an IoT application in about 30 minutes, with the assistance of CodeWhisperer, with some examples of what went well and what did not got well.

For more information about CodeWhisperer, have a look at the documentation. There is also a guide on how to get started with CodeWhisperer and JetBrains IDEs.

To watch more such content, subscribe to IoT Builders on YouTube or follow IoT Builders on dev.to.

Author

Top comments (2)

Collapse
 
akramiot profile image
akramIOT

Great work Alina with CodeWhisperer and AWS IOT Core.

Collapse
 
fay_ette profile image
Alina Dima

Thank you! 🙂