AWS Database Blog
Faster development with Amazon DynamoDB and Amazon Q Developer
Amazon Q Developer, a generative artificial intelligence (AI) assistant, can help accelerate the development of applications on AWS. When used in an integrated development environment (IDE), Amazon Q provides software development assistance. Amazon Q can chat about code, provide inline code completions, generate new code, scan your code for security vulnerabilities, and make code upgrades and improvements, such as language updates, debugging, and optimizations.
Amazon DynamoDB is a serverless NoSQL database service for single-digit millisecond performance at scale. You can interact with DynamoDB through the AWS Management Console or programmatically. When working programmatically, a common approach is to use infrastructure as code (IaC) tooling like AWS CloudFormation or the AWS Cloud Development Kit (AWS CDK) to manage tables and a programming language SDK to read and write data to tables. Amazon Q can assist with both of these to improve your speed of development.
In this post, we create a DynamoDB table using IaC then perform create, read, update, and delete (CRUD) operations on the table using Python and Boto3 (with additional observations for JavaScript and Java at the end of the post). We demonstrate how Amazon Q can improve your speed of development for these tasks.
Interacting with Amazon Q
For our demonstration, we installed the Amazon Q Developer plugin into our IDE. We interact with Amazon Q in two ways:
- The chat panel in our IDE. We enter a natural language request in the text bar and evaluate the response.
- Inline suggestions in our IDE. We add a natural language comment in the source code file we’re editing and evaluate the suggestions that Amazon Q provides.
We include all the prompts used with Amazon Q for you to expand upon. Generative AI assistants are non-deterministic; if you repeat this exercise yourself, you may not get the same output as these examples, but we’re confident that you’ll still find Amazon Q beneficial.
You own the code that you write, including the Amazon Q code suggestions that you accept. You should always review code suggestions before accepting them, and you might need to make edits to make sure that the code does exactly what you intended.
Create the DynamoDB table
We use AWS CloudFormation to create our solution, but this process would also work with the AWS Serverless Application Model (SAM) CLI or AWS CDK.
We create a DynamoDB table in a YAML CloudFormation template. The table stores temperature values from IoT devices. We ask Amazon Q to generate this in the chat panel in our IDE. The chat request and response are shown in the following screenshot.
The suggested code is ideal, but we forgot to specify a requirement; we want the table to be named IotDynamoDbTable
. The chat panel is conversational and remembers our previous interactions, so we can ask for this additional requirement to be added.
The provided solution is what we want, so we accept it by copying into a text file in our IDE. This has saved us time generating the CloudFormation YAML. We can edit the text file however we want.
If you want to further enhance the table, for example by adding a global secondary index (GSI) or a stream, you can follow one of two approaches:
- Continue the conversation in the Amazon Q chat panel
- Add a comment in the text file to receive an inline response from Amazon Q
In this example, we try extending what we already have in our text file by adding a comment so that Amazon Q provides an inline suggestion. The suggestion is good, so we can accept it and continue. You can see this interaction in the following animation.
Perform CRUD operations
In this section, we provide examples of performing CRUD operations on the DynamoDB table with the help of Amazon Q.
Create
We create a sample of code running in an AWS Lambda function, reading all records from the event and writing them into our DynamoDB table. The records in the event originated from an Amazon Simple Queue Service (Amazon SQS) queue. We return to the chat panel to get us started.
Our request didn’t provide any detail about the records in the event. In the absence of this detail, Amazon Q has generated a suggestion, but it isn’t quite what we wanted. Again, we have two options:
- Continue the conversation in the chat panel and clarify our request
- Copy the code into a text editor and make the modifications manually
Given the simplicity of the modification, we chose the second option. The result is shown in the following screenshot.
Even including this modification, the time to create this function with Amazon Q is quicker than if we did it manually.
Read
Now we want to extend the code to read an item from the table. We know the value of the deviceID
and readingTime
, so we can do a direct lookup using the primary key. We add a comment in the Lambda function code describing what we want and let Amazon Q provide suggested code. You can see this in the following animation.
We accept the Amazon Q suggestion. It contains a placeholder that we need to update—the value for deviceID
. This is a straightforward change, and we didn’t have to leave the IDE to look up how to read an item from the table.
Update
Again, we know the value of the deviceID
and readingTime
for an item and want to update it. We want to add an attribute called badReading
with a value of true, but only if the value of the temperature is greater than 150. We add a comment to the text file and let Amazon Q provide a sample. You can see this in the following animation.
Again, the suggestion contains a placeholder for us to update. This is definitely a time-saver, especially if we need to go look up documentation or examples for this operation. We accept the suggestion and update the value of deviceID
to complete it.
Delete
We know the value of the deviceID
and readingTime
that we want to delete. To add more detail to this request, we want the operation to return how much capacity this operation consumed and what the item looked like before we deleted it. We add a comment to the text file and let Amazon Q provide a sample. This result can be seen in the following screenshot.
Again, we accept the sample and update the placeholder to complete the code.
Perform Query operations
DynamoDB Query operations can be very powerful—in a single operation, you can retrieve a set of items that have the same partition key value, but different sort key values as well as filter on non-key attributes, sort, and limit. They are more complex to implement than the CRUD operations we have seen so far.
For this example, we want to perform a query to return all items with temperature readings between 10–20 degrees that happened yesterday for a given deviceID
. We’re only interested in the five most recent items that meet that condition.
Amazon Q provides multiple samples that we can quickly iterate through. We select the one that we feel is the best as the foundation for continuing our work. This can be seen in the following screenshot.
The structure of the KeyConditionExpression
is correct, but the readingTime
has constant values. We can manually improve this, or improve the comment and try again, or break the problem down and let Amazon Q assist in solving it piece by piece.
We add imports at the top of our file for the time
and datetime
modules as a hint (Amazon Q values import statements as context and will favor providing suggestions that use them). Then we break the problem down and let Amazon Q solve the pieces. Each comment in the following screenshot is a piece of the problem we asked Amazon Q to solve, with the code being the selected suggestion. We let Amazon Q solve a piece before writing the next comment.
Working with JavaScript and Java
Experiments with JavaScript show that the developer experience is largely similar to the Python one already shown in this post.
The Java experience is a bit more variable because there are multiple approaches to working with DynamoDB through the SDKs: v1 (deprecated), v2, and enhanced client. Amazon Q uses existing code in the project as context for which approach to use. If you’re modifying an existing project, you are likely to receive samples using the same approach as the rest of the project. If your project is new, you can strongly influence which approach the initial suggestions use by adding relevant import statements at the top of the file you are currently editing. Both chat panel and inline will use these import statements as context. The following are example imports for two SDK options:
- v2 – import software.amazon.awssdk.services.dynamodb.*
- Enhanced client – import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.*
Generate additional supporting code
To build a fully working solution, we would need additional supporting code. For example, the CloudFormation template needs a Lambda function and associated AWS Identity and Access Management (IAM) role. We can use Amazon Q to generate this code as well. We have focused on how Amazon Q can help you with DynamoDB operations, but from our experience, Amazon Q is also highly effective at saving you time when generating these resources (and more).
Conclusion
In this post, we demonstrated how to create a DynamoDB table using IaC, and perform CRUD operations with the help of Amazon Q. Amazon Q can save you time when working with DynamoDB by providing sample code in response to your needs, expressed in natural language.
With some small modifications to the suggestions, or breaking down larger, more complex requests into smaller pieces, you can produce working code faster than what it would take to write that code without assistance from Amazon Q.
Amazon Q can support the entire software development lifecycle, from planning to maintenance. To see how Amazon Q can support at each stage, see Accelerate your Software Development Lifecycle with Amazon Q.
To learn more about Amazon Q, see the Amazon Q Developer Guide. You can also read about best practices for prompt engineering to learn how to make your prompts (the natural language comments Amazon Q uses to generate suggestions) align to best practices.
To learn more about DynamoDB, see the Amazon DynamoDB Developer Guide.
About the Author
Chris Gillespie is a UK-based Senior Solutions Architect. He spends most of his work with fast-moving “born in the cloud” customers. Outside of work, he fills his time with family and trying to get fit.