Coding IoT with Amazon CodeWhisperer AI Coding Companion
Tags: ai, iot, codewhisperer, cdk
Published: 2023-05-12 13:51:19

Amazon CodeWhisperer AI Coding Companion has been [generally available](https://aws.amazon.com/about-aws/whats-new/2023/04/amazon-codewhisperer-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](https://docs.aws.amazon.com/cdk/v2/guide/home.html) 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](https://github.com/mqttjs) library, and the [AWS SDK v3](https://github.com/mqttjs) 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](https://www.youtube.com/@iotbuilders). 

**_Part 1 - CDK for IoT_**

{% embed https://youtu.be/C5OkYqcxcAQ %}

**_Part 1 - Building the IoT App_**

{% embed https://youtu.be/S9zaFJwzCBs %}

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

This application was built in 2 steps:

1. The AWS Resources were created using CDK: an IoT Thing policy based on [least privilege](https://docs.aws.amazon.com/iot/latest/developerguide/example-iot-policies.html) 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](https://www.jetbrains.com/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](https://docs.aws.amazon.com/signin/latest/userguide/sign-in-aws_builder_id.html).


![CodeWhisperer SetUp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdsoh4nvs7pipzfvcg05.gif)

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

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

![AI create IoT Thing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujc6wedrjo94q9gl9t18.gif)

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


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

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](https://github.com/aws-iot-builder-tools/cdk-iot-sample/blob/main/lib/cdk_io_t-stack.ts), 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3096agsrm902hk4y3az3.gif)

## 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: 
- [Part 1-CDK](https://youtu.be/C5OkYqcxcAQ ), 
- [Part 2 - IoT App](https://youtu.be/S9zaFJwzCBs).

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](https://aws.amazon.com/codewhisperer/). There is also a guide on how to get started with [CodeWhisperer and JetBrains](https://docs.aws.amazon.com/codewhisperer/latest/userguide/language-ide-support.html) IDEs. 

To watch more such content, subscribe to [IoT Builders on YouTube](https://www.youtube.com/@iotbuilders) or follow [IoT Builders](https://dev.to/iotbuilders) on dev.to.

## Author<a name="Author"></a>
{% user fay_ette %}