logo
Menu
Refactoring your Python App with 12-Factor Methodology

Refactoring your Python App with 12-Factor Methodology

Streamlined refactoring with Amazon Q Developer and /dev agent

Mohammed Fazalullah
Amazon Employee
Published Sep 30, 2024

Introduction

In this article, we’ll explore how to refactor a Python task management app using Amazon Q Developer and the /dev agent to better align with the 12-factor methodology. The 12-factor methodology, developed by Heroku, is a set of best practices for building scalable and maintainable software-as-a-service (SaaS) apps.
Amazon Q Developer is an AI-powered tool designed to assist with code analysis, refactoring suggestions, and adherence to best practices. The /dev agent is an AI assistant capable of performing actions in a code workspace based on natural language instructions. By combining these tools, we can streamline the process of enhancing our app to follow the 12-factor principles more closely.

Step 1: Analyzing the Existing Codebase

The first step was to use Amazon Q Developer to review the existing codebase. Our goal was to identify areas where the app could be improved to align more closely with the 12-factor methodology. The 12-factor methodology advocates treating backing services (e.g., databases, message brokers) as attached resources, configured via environment variables, and various other recommendations.
Single file app
Single file app
Since the app is a single file currently, we ask Amazon Q Developer in the chat on it's thoughts about the code with the following prompt:
Explain what the application coded in app.py does and mention briefly on how it can be refactored with the 12 factor app methodology
Chatting with Amazon Q Developer in VS Code
Chatting with Amazon Q Developer in VS Code
The analysis from Amazon Q Developer highlighted the following:
Now just with based on these insights we get an idea of where to focus our efforts on improving the app’s ability to seamlessly integrate backing services and improving the parity between development and production environments.
This can be done either by:
  1. Iterating through each step in the 12 factor app roach manually updating the code or asking Amazon Q Developer to suggest code changes and us taking it into consideration.
  2. Or, we leverage the /dev agent and let it do the work for us!

Step 2: Refactoring with the /dev Agent

By choosing the /dev agent approach to refactor the code, we leverage the agent to implement changes by providing natural language instructions, streamlining the process. And before implementing the changes Amazon Q Developer provided us the recommendations and asks us to verify and approve the changes. Let's see how that went below!

2.1 First pass of implementing the 12 factor app methodology

Opening a new chat window in the Amazon Q Developer extension in VS Code, and typing the /dev command activates the Amazon Q Developer agent.
Agent activate
Agent activate
Using the below prompt we get the /dev agent to start analyzing and providing us with concrete steps of what can be changed:
Transform the following project using the 12 factor app approach and also generate a step-by-step instruction in a README.md file after refactoring is complete.
First pass 12 factor with /dev
First pass 12 factor with /dev
The agent begins by diving into the codebase, analyzing and providing real-time updates on its findings, the modifications it's making, and the specific files involved. It starts by mapping out the codebase, quickly honing in on the critical sections that require changes. With each iteration, the agent refines its modifications, testing and adjusting until it achieves the desired outcome. Once complete, it presents a summary of the changes made, detailing the files that have been updated and the improvements implemented.
How cool is that?!
Modifications in each file can be reviewed in the IDE and decided whether to accept them. If the generated code could be improved, feedback can be provided to the agent and request that it regenerates it.
The other thing to notice is that it also generated a test case to test the functionality which is a great way to enforce verifying functional correctness while refactoring.
Functional test
Functional test

2.2 Iteration to improve the deployment piece

One thing that was not mentioned explicitly before hand was the target deployment approach. Since it was asked to refactor using the 12 factor methodology, Amazon Q Developer created a Proc file that can be used as part of deployment to a Heroku account.
But what if we want to containerize this and deploy it elsewhere? Asking Amazon Q Developer to do it for us with the following prompt is the next step:
I'm not using Heroku so can you make an alternative implementation of the Procfile with Docker and Docker Swarm? Update the README.md accordingly.
Containerize it!
Containerize it!
Apart from the findings and modifications, we see the README.md file is updated. Clicking on the suggestion opens a diff view showing what changes will be applied if we accept the code.

2.3 Running the app

Now with any iterative approach to refactoring we have to test out the code and configurations that was generated. The README.md file generated earlier provided the steps needed to leverage the Docker compose command to build and deploy locally.
And the images are built and run by Docker!
Docker Compose Up!
Docker Compose Up!
How do we test this though? Maybe asking Amazon Q Developer to suggest sample payloads should work. Heading over to the IDE and tapping on the + sign in Amazon Q Developer to start a new chat, while having the app.py file open in the IDE, the following prompt is entered:
Give me some sample payloads for the flask API I can test with on http://127.0.0.1:5000
Amazon Q Developer delivers
Amazon Q Developer delivers
Taking the first example and executing from the command line, and... something is not right. Looking at the container logs it shows the following:
Error ahoy
Error ahoy
Ah, yes! This app initially was using an in-memory store and now we moved to a Postgres database. While verifying the code we did not notice that a table create script or code logic was missing.
No worries, asking Amazon Q Developer the following in the /dev agent chat brings us back on the right track:
The table is not created in the database. add the required scripts to do this after checking if the table exists or not.
Code changes
Code changes
Table created
Table created
And on testing the requests through the VS Code extension Thunder Client, we have success!
Success
Success
Success
Success
Using Amazon Q Developer, we quickly generated this update and ensured that it accurately described the refactoring process.

Conclusion

By utilizing Amazon Q Developer and the /dev agent, we were able to refactor our task management app to better adhere to the 12-factor methodology. These AI-powered tools provided actionable insights, reduced the manual effort required for refactoring, and ensured that best practices were followed throughout the process. The benefits of this approach include:
  1. Faster identification of issues: Amazon Q Developer quickly pinpointed areas for improvement.
  2. Efficient refactoring: The /dev agent allowed us to make significant changes with minimal manual intervention.
  3. Improved maintainability and scalability: The refactor enhanced the app's flexibility and stability, making it easier to scale.
  4. Educational insights: The AI tools provided clear explanations of best practices, enhancing our understanding of the principles behind the 12-factor methodology.

Next steps

Check out the Amazon Q on AWS Developer Center and documentation. Engage with AWS Community posts here. Also don’t forget to follow AWS Developers social profiles - YouTube, Twitter/X, LinkedIn, Instagram, and Twitch —to stay updated with the latest news and content!
 

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

2 Comments