Dart's potential for backend development has gained a lot of attention. While Dart's official shelf library has been the traditional solution for backend development, there has been other solutions to enable full-stack Dart applications. Projects like Serverpod and Dart Frog are expanding the ecosystem, making it easier than ever to build complete applications using Dart on both frontend and backend.
In the age of AI, you ask many of your questions to AI and deploying backend as a Flutter developer can be a question that you might ask as well. In this blog post, you will see how you can deploy your Dart backends to AWS with the help of Amazon Q Developer on CLI.
You can check out the source-code for the project over GitHub to start with. Besides that you have to have an AWS Account to deploy over AWS and for using Amazon Q Developer on CLI (or IDE) you should have Builder ID and install Q developer CLI by following the official documentation.
Deploying REST APIs
First, we have to start by running the Amazon Q Developer on our CLI. At any point in your project, you can type 'q' and it will show a window for you to interact.
If you want to limit the context, you can navigate to the root folder of your project. Next, you need to tell the Q CLI what you want from it.
I want to deploy the hello.dart file in the backend folder. I don't know anything about AWS. All I know is, hello.dart is an application for a REST API. Show me the simplest, fastest and cheapest way to deploy this dart backend app over AWS.
When you run this prompt, the Q CLI analyzes your project, locates the backend file you want to deploy, and recommends the best deployment method.
The Q CLI also explains why this deployment method works best.
Next, the Q CLI creates the necessary deployment files. You can choose between supervised development or allow automated deployment with minimal intervention.
Deploying to AWS
After the Q CLI generates the necessary files, you will find a Dockerfile among them.
FROM dart:stable AS build
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get
COPY . .
RUN dart pub get --offline
# Build a minimal serving image
FROM dart:stable
COPY --from=build /app /app
WORKDIR /app
# Make sure the application listens on port 8080
ENV PORT=8080
# Install dependencies in the final image
RUN dart pub get
# Start the application
CMD ["dart", "bin/hello.dart"]
# Expose the port
EXPOSE 8080
Docker creates a virtual environment that contains all necessary dependencies.
You can choose between AI-guided automated deployment or manual deployment following step-by-step instructions. For the REST API deployment, you will focus on the manual instructions first.
First, you need to create a repository in Amazon Elastic Container Registry (ECR) to store your Docker container image.
After naming the repository and keeping the default settings, click 'Create.' Amazon ECR will display commands for building and pushing the container.
After pushing the container, proceed to AWS App Runner to run it. Create a service and assign the container as a resource to this service.
Next, enter a name and keep the default settings.
The backend is now ready for deployment.
Troubleshooting
When you deploy your backend, the initial deployment may fail with the following information:
An advantage of working with AI is the ability to provide context for assistance with deployment or troubleshooting potential issues.
In this case, the system shows how to deploy containers to different architectures.
The solution requires rebuilding with multi-architecture support and updating the image in Amazon ECR:
# If you don't have buildx set up:
# Create a new builder instance
docker buildx create --name mybuilder --use
# Build with the correct platform
docker buildx build --platform=linux/amd64 --load -t hello .
# Tag and push
docker tag hello:latest AWS_USER_ID.dkr.ecr.us-east-1.amazonaws.com/full-stack-dart/hello:latest
docker push AWS_USER_ID.dkr.ecr.us-east-1.amazonaws.com/full-stack-dart/hello:latest
After rebuilding the project, the deployment completes successfully.
Also, the endpoint works as expected:
Deploying Dart Backends with Websockets
The shared codebase contains the original implementation: a game with an emoji counter. It uses WebSockets and provides real-time updates to each listener.
Now, let's switch the deployment approach from manual instructions to automated deployment using Q CLI, with human involvement limited to testing the results.
Run the following prompt on Q CLI:
Hello I want you to deploy the dart websocket backend under serverpod folder to the cheapest and fastest AWS option. I am not knowledgable with AWS so I want you to take care of necessary permissions, create necessary files and execute the deployment until it is successful. Make sure to use a service supporting websockets and service can be deployed through CLI. Also, make sure whatever way you deploy is in line with the deployment environment. E.g. if you are deploying to Linux, make sure to cover Linux related deployments. Test and make sure the connection is established before you complete the request. Once all is done and successful, document the steps and results to a markdown file.
Q CLI analyzes the project, examines the implementation, generates a solution, and provides detailed explanations.
Like before, hen prompted, enter 't' to authorize full AI implementation. After deployment, the system displays an execution summary:
As you have seen in the previous example, Dart backends lack direct deployment solutions. While AWS offers multiple services for containerized applications, Q CLI selected Elastic Beanstalk for these reasons:
- It supports websockets
- It's like having a hosting service specifically designed for web applications
- It handles a lot of the complex infrastructure setup automatically
- It's part of the AWS Free Tier, so it's cost-effective for our project
Think of Elastic Beanstalk as a service that takes your code and automatically sets up everything needed to run it online.
Containerizing Our App with Docker
Before deploying, Q CLI needed to package your app in a way that ensures it runs the same way everywhere. It updated your Dockerfile like the following:
FROM dart:stable AS build
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get
COPY . .
RUN dart pub get --offline
# Build a minimal serving image
FROM dart:stable
COPY --from=build /app /app
WORKDIR /app
# Make sure the application listens on port 8080
ENV PORT=8080
# Install dependencies in the final image
RUN dart pub get
# Start the application
CMD ["dart", "bin/server.dart"]
# Expose the port
EXPOSE 8080
This Dockerfile:
- Starts with a base Dart environment
- Copies our app code
- Installs dependencies
- Sets up the command to run our server
- Exposes port 8080 so the outside world can connect
Setting Up Network Security
Next, Q CLI created what AWS calls a Security Group - think of it as a virtual firewall that controls what traffic can reach our server:
✅ HTTP (port 80) - For regular web traffic
✅ HTTPS (port 443) - For secure web traffic
✅ WebSocket (port 8080) - For our real-time connections
This ensures that only the types of connections you expect can reach our server, helping to keep it secure.
Configuring WebSockets for Production
WebSockets are a bit trickier to set up in production than regular HTTP endpoints. They require special configuration to maintain long-lived connections.
Q CLI created a special configuration file for Nginx (the web server that sits in front of our application). This configuration tells Nginx how to handle WebSocket connections properly. The key parts are:
- Setting up the
/ws
path to handle WebSocket protocol upgrades - Configuring proper headers for WebSocket connections
- Setting a long read timeout so connections don't drop prematurely
Deployment Process
With everything configured, Q CLI deployed your app using the Elastic Beanstalk CLI:
- Initialized the application - This creates the basic configuration
- Created the environment - This provisions the actual server
- Deployed the code - This uploads and runs your application
Behind the scenes, AWS:
- Created a virtual machine (EC2 instance)
- Set up Docker on that machine
- Built your Docker container
- Started your Dart server
- Configured the network so it's accessible from the internet
What This Means for You as a Frontend Developer
Now that the backend is deployed, here's what you need to know:
WebSocket Connection: Update your frontend code to connect to
ws://dart-emoji-backend-env.PROJECT-ID.us-east-1.elasticbeanstalk.com/ws
instead of localhost-
API Format: The message format remains the same:
- To send a reaction:
{"type": "reaction", "emoji": "👍"}
- You'll receive:
{"type": "counts", "counts": {"👍": 1, "❤️": 0, ...}}
- To send a reaction:
Real-time Updates: Multiple users can now connect simultaneously from anywhere in the world, and all will see reactions in real-time
The best part? You don't need to worry about managing the server, scaling, or any of the backend infrastructure. It's all handled automatically by AWS Elastic Beanstalk.
Testing the Deployed Endpoint
Go to the websocket_service.dart file and update the getWebSocketUrl
function to return your deployed URL and run the application:
Cleaning Up
If you are doing this as part of an experiment, make sure to delete the deleted resources. You can ask Q CLI to "clean up the deployed resources".
Make sure you also check the deployed services and the deletions happened successfully.
Summing up
AI-guided deployment offers powerful capabilities for Dart backend applications with Amazon Q Developer. While experience with AWS and containers enables custom workflow creation, starting with Q CLI to deploy backends provides a quicker and efficient path to successful Dart backend implementation.
To learn more about it, I challenge you to do more by doing one (or all) of the following:
- Try to deploy the Serverpod backend with the same prompts
- Try to deploy your Frontend with the guidance of Q CLI
- Ask more complex questions about pricing :)
Make sure to check out the Amazon Q CLI docs as well as the Amazon Q on IDE to make the most out of your AWS experience for your full stack experiences.
Top comments (0)