AWS Logo
Menu
Q-Bits: Building Safe and Efficient Web APIs with Rust, Actix Web and Amazon Q Developer

Q-Bits: Building Safe and Efficient Web APIs with Rust, Actix Web and Amazon Q Developer

Learn how to create high-performance web APIs in Rust using the Actix framework. Amazon Q Developer will demonstrate how to set up routes, handle request parsing and validation, and implement async handlers.

Matt Laver
Amazon Employee
Published Jan 14, 2025
Last Modified Jan 15, 2025
Welcome to another installment of Q-Bits, our regular series showcasing cool ways Amazon employees are leveraging Amazon Q Developer. Today, we're looking at how Amazon Q Developer benefits Rust developers when building safe and efficient Web APIs with Actix Web.

Introduction

This article will explore some of the steps I take to plan and implement a Web API with Rust and Actix Web. I'll be using Amazon Q Developer as my companion along the way.

Amazon Q Developer and JetBrains RustRover

JetBrains RustRover is my IDE of choice when working on Rust projects. Amazon Q Developer and JetBrains RustRover make an excellent combination for Rust developers, enhancing productivity and code quality. Amazon Q can be integrated directly into the RustRover IDE, allowing developers to access AI assistance without leaving their coding environment.
Amazon Q Developer integrated with JetBrains RustRover
Amazon Q Developer integrated with JetBrains RustRover
While RustRover offers deep Rust language support, Amazon Q can provide additional context, best practices, and explanations tailored to Rust development. By combining Amazon Q Developer's AI capabilities with RustRover's specialized Rust development environment, developers can create a powerful, efficient workflow for Rust programming.
Planning
It's often tempting to dive straight into writing code, with Amazon Q Developer as my coding companion, I can start to ask questions that are in my head or apply a bit of rubber duck debugging, except this time the duck talks back!
From the Amazon Q Developer Chat window within RustRover, I can start bouncing ideas around, for example:
"How can I implement rate limiting in my Actix-web API?"
Amazon Q Developer Chat
Amazon Q Developer Chat
What I love about this is that Amazon Q Developer introduces me to a new package, actix-governor, provides a code example, explains what the sample implementation does in plain English, and even includes advice for production usage.
Production tips from Amazon Q Developer Chat
Production tips from Amazon Q Developer Chat
Other typical questions I might ask :
"Describe the best practices for error handling and response formatting in an Actix-web API."
"How can I structure my Actix project to ensure scalability and maintainability?"
"What are some strategies for API versioning in an Actix-web application?"
"How can I implement request validation and sanitization in my Actix-web API?"
Implementing Our API with Q Developer Agent for Software Engineering
Now I have some good ideas, and I'm ready to begin implementing my API; this is where the Q Developer Agent for Software Engineering comes in. Using the /dev command within the chat window, I can ask Amazon Q Developer to build my API.
Create a RESTful Web API using Rust and the Actix web framework. The API should:
  • Set up a basic project structure with proper cargo dependencies for Actix and any necessary database libraries.
  • Implement CRUD (Create, Read, Update, Delete) operations for a 'User' resource with fields: id, name, email, and created_at.
  • Use a database (e.g., PostgreSQL) for data persistence. Include connection pool setup.
  • Implement proper error handling and return appropriate HTTP status codes.
  • Include input validation for the API endpoints.
  • Implement basic authentication using JWT (JSON Web Tokens).
The agent immediately gets to work:
Developing features with Amazon Q Developer
Developing features with Amazon Q Developer
I can review the proposed updates and iterate with additional prompts and then accept the changes. What I love about this is that Q Developer has done much of the heavy lifting, including creating database migration scripts:
Amazon Q Developer generated database migration scripts
Amazon Q Developer generated database migration scripts
And CRUD implementation for my data persistence layer:
Amazon Q Developer generated data layer code
Amazon Q Developer generated data layer code
Browsing my new code, I can see the logging middleware has been added to the Actix-web server configuration code. I can use the Amazon Q -> Explain Code feature to help me understand the implementation:
Amazon Q Developer explains code
Amazon Q Developer explains code

Conclusion

In this post I've shown how Amazon Q Developer has been used to plan, implement and review a Web API built with Rust and Actix Web. The design was driven via prompts and Amazon Q Developer took care of the implementation for me.
My work is not finished, I will be continuing to use Amazon Q Developer to help me take this project through the rest of the SDLC, including:
  • Setup examples of unit and integration tests
  • Add Swagger/OpenAPI documentation for the API
  • Define a Dockerfile to create a container image
  • Define CloudFormation to provision and deploy my API to AWS
  • Setting up a CI/CD pipeline
     

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments