A step-by-step guide to automate infrastructure and scale a Flask application with Terraform, Docker, and AWS ECS.
š§ Introduction
Welcome Devs to the world ofĀ AIĀ andĀ AutomationĀ š
Today, weāre diving into an exciting hands-on project whereĀ infrastructure meets intelligence. Weāll exploreĀ Cognee AIā a memory layer for LLMs that lets applications remember, retrieve, and build on prior context ā and see how it works in action through theĀ Cognee Starter App, built with Flask.
But weāre not stopping there. Once the app is ready, weāllĀ deploy it to AWS ECS (Fargate)Ā usingĀ Terraform, bringing the power ofĀ Infrastructure as Code (IaC)Ā to streamline and automate the entire deployment process.
By the end of this guide, youāll:
š§ Get familiar with Cognee AI and its role as a memory layer for LLMs.
š³ Containerise and prepare a Flask application for production.
āļø Provision AWS infrastructure using Terraform.
š Deploy the app seamlessly on AWS ECS with Fargate.
So without further ado, letās get started and build something awesome!
š½ļø Youtube Demonstration
š§° Pre-Requisites
Before we roll up our sleeves and dive into the deployment, letās make sure your local environment is ready. Having the right tools installed will make the process smooth and error-free.
Hereās what youāll need on your system:
šŖĀ AWS CLI configured with an IAM userĀ that hasĀ ECS,Ā VPC, andĀ IAM Full AccessĀ permissions.
š If youāre not familiar with this setup, check out my detailed step-by-step guide here:Ā Learn How to Deploy a Three-Tier Application on AWS EKS Using Terraform.š³Ā DockerĀ ā to containerize our Flask application before deploying it to ECS.
šĀ PythonĀ ā since Cognee Starter runs on Flask.
š§±Ā Terraform CLIĀ ā the star of this blog, which weāll use to provision and manage our AWS infrastructure.
Once youāve checked these boxes ā , youāre all set to move to the fun part ā building and deploying our application!
š§ Understanding Cognee AI
Before jumping into infrastructure, letās take a moment to understand whatĀ Cognee AIĀ actually does ā and why itās important.
In simple terms:Ā āCognee organizes your data into AI memory.ā
When you make a call to a Large Language Model (LLM), the interaction isĀ statelessĀ ā meaning it doesnāt remember what happened during previous calls or have access to your broader document context. This makes it difficult to build real-world applications that require context retention, document linking, or knowledge continuity.
Thatās whereĀ Cognee AIĀ comes in. It acts as aĀ memory layer for LLMs, allowing you to:
Link documentsĀ and data sources together.
Maintain contextĀ across multiple LLM calls.
Create richer, more intelligent applicationsĀ that can reason over previous interactions.
In this project, weāll be using theĀ Cognee Starter App, which gives a hands-on introduction to this powerful memory layer ā and then weāll deploy it onĀ AWS ECS (Fargate)Ā to make it production-ready.
š§ How Cognee Works
Cognee isnāt just about storing data ā itās aboutĀ understandingĀ andĀ structuringĀ it so LLMs can use it intelligently. When it comes to your data, Cognee knows what matters.
There areĀ four key operationsĀ that power the Cognee memory layer:
Ā
.addĀ ā Prepare for Cognification
This is the starting point. You send your data asynchronously, and Cognee cleans, processes, and prepares it for the memory layer..cognifyĀ ā Build a Knowledge Graph with Embeddings
Cognee splits your documents into chunks, extracts entities and relations, and links everything into aĀ queryable knowledge graphĀ ā the core of its memory layer..searchĀ ā Query with Context
When you search, Cognee combinesĀ vector similarityĀ withĀ graph traversal. Depending on the mode, it can fetch raw nodes, explore relationships, or even generate natural language answers using RAG (Retrieval-Augmented Generation). It ensures theĀ right contextĀ is always delivered to the LLM..memifyĀ ā Semantic Enrichment of the GraphĀ (coming soon)
This will enhance the knowledge graph withĀ deeper semantic understanding, adding richer contextual relationships.
In ourĀ hands-on demonstration, weāll use the first three methods āĀ .add,Ā .cognify, andĀ .searchĀ ā to see how Cognee works in action before deploying the app on AWS ECS.
š§Ŗ A Small Demo to Understand Cognee Functions
Before we jump into deploying our Flask application on AWS, letās take a few minutes to understand howĀ Cognee AI works practically.
Iāve already hosted the project code on GitHub š
šĀ GitHub Repository ā terra-projects
Head inside theĀ cognee-flaskĀ directory, and youāll find the entire project structure there.
š§° Step 1: Set up Environment Variables
Inside the project folder, create aĀ .envĀ file. You can refer to theĀ .env.exampleĀ file for the format.
Youāll need aĀ Gemini API Key, which isĀ freeĀ to get. Paste it in yourĀ .envĀ file like this:
LLM_PROVIDER="gemini"
LLM_MODEL="gemini/gemini-2.5-flash"
LLM_API_KEY="<your-gemini-key>"
# Embeddings
EMBEDDING_PROVIDER="gemini"
EMBEDDING_MODEL="gemini/text-embedding-004"
EMBEDDING_DIMENSIONS="768"
EMBEDDING_API_KEY="<your-gemini-key>"
š If youāre using a different LLM provider, follow this guide to configure it properly:Ā Cognee Docs ā Installation & Setup
ā” Step 2: Install Dependencies
To install all the required dependencies, run:
uv sync
This will set up everything you need to run the demo locally.
š§ Step 3: UnderstandingĀ testing_cognee.py
Now, open theĀ testing_cognee.pyĀ file. Hereās what it looks like:
from cognee import SearchType, visualize_graph
import cognee
import asyncio
import os, pathlib
async def main():
# Create a clean slate for Cognee -- reset data and system state
await cognee.prune.prune_data()
await cognee.prune.prune_system(metadata=True)
# Add sample content
text = "Cognee turns documents into AI memory."
await cognee.add(text)
# Process with LLMs to build the knowledge graph
await cognee.cognify()
graph_file_path = str(
pathlib.Path(
os.path.join(pathlib.Path(__file__).parent, ".artifacts/graph_visualization.html")
).resolve()
)
await visualize_graph(graph_file_path)
# Search the knowledge graph
graph_result = await cognee.search(
query_text="What does Cognee do?", query_type=SearchType.GRAPH_COMPLETION
)
print("Graph Result: ")
print(graph_result)
rag_result = await cognee.search(
query_text="What does Cognee do?", query_type=SearchType.RAG_COMPLETION
)
print("RAG Result: ")
print(rag_result)
basic_result = await cognee.search(
query_text="What are the main themes in my data?"
)
print("Basic Result: ")
print(basic_result)
if __name__ == '__main__':
asyncio.run(main())
šĀ Whatās happening here:
First, weĀ purge any existing dataĀ usingĀ
prune_dataĀ andĀprune_system.Then, weĀ add new text dataĀ to Cognee usingĀ
.add.WeĀ process and build a knowledge graphĀ withĀ
.cognify.The graph is stored inĀ
.artifacts/graph_visualization.html.-
Finally, weĀ run three types of searches:
-
GRAPH_COMPLETIONĀ ā explores relationships. -
RAG_COMPLETIONĀ ā generates natural language answers. - Basic Search ā retrieves core themes.
-
ā¶ļø Step 4: Run the Demo
Run the script with:
uv run testing_cognee.py
You should see outputs forĀ Graph,Ā RAG, andĀ BasicĀ results in your terminal. You can also open theĀ .artifacts/graph_visualization.htmlĀ file in a browser to view theĀ knowledge graphĀ Cognee has generated.
š§Ŗ Step 5: Run the Flask App Locally
Before deploying to the cloud, letās test the Flask app locally:
uv run app.py
Once the server starts, open šĀ http://localhost:5000
Youāll see theĀ Cognee AI Starter AppĀ UI. Hereās what you can do:
Add your own data.
Ask a query.
Wait 1ā2 minutes.
-
Youāll get:
- š§ Ā Graph Completion Result
- š¬Ā RAG Completion Result
- šĀ Basic Theme of the text
- š AĀ vector graphĀ visualization generated by Cognee.
Click onĀ āView Knowledge GraphāĀ to explore the graph and see how Cognee structured your data.
ā
With this, weāve understood Cogneeās core functionality locally.
Next up ā weāll take this application to the cloud byĀ deploying it on AWS ECS using TerraformĀ š
āļø Deploying the Cognee App on AWS ECS Using Terraform
Weāve successfully tested theĀ Cognee AI Starter AppĀ locally ā now itās time to take things to theĀ cloudĀ š„ļø
Weāll useĀ TerraformĀ to provision all the required AWS infrastructure andĀ deploy our Flask application on ECS (Fargate).
Inside your project directory, navigate to theĀ terra-configĀ folder. This is where all of our Terraform configuration files live.
š§± Step 1: Understanding the Terraform Files
provider.tf
Defines AWS as ourĀ cloud providerĀ and sets the region and provider configuration.-
default_config.tf- Fetches theĀ default VPCĀ andĀ subnets.
- Creates aĀ security groupĀ for ECS tasks with portĀ
5000Ā open to allow inbound traffic.
-
main.tfĀ (the heart of the project)- Creates anĀ ECS cluster.
-
Defines theĀ ECS task definitionĀ with:
- Docker image
- Container port
- CPU & memory configurations
- CPU architecture
Creates anĀ IAM roleĀ for ECS.
Finally, provisions anĀ ECS serviceĀ using the task definition inside the cluster.
get_ip.sh
A simple shell script that uses AWS CLI to fetch theĀ public URLĀ where your application is running.
ā” Step 2: Initialize and Deploy the Infrastructure
Run the following commands inside theĀ terra-configĀ directory:
terraform init
terraform plan
terraform apply --auto-approve
ā³ This will take aroundĀ 2 minutesĀ to provision the complete infrastructure on AWS ā including the ECS cluster, service, task definition, and networking setup.
š Step 3: Get the Application URL
Once the deployment finishes, make the script executable and run it:
chmod u+x get_ip.sh
./get_ip.sh
This will output theĀ URLĀ where your application is hosted.
š Open the URL in your browser, and youāll see yourĀ Flask Cognee applicationĀ live on AWS ECS š
From here, you can:
Add your data.
Ask queries.
See the graph and AI-generated answers exactly like in the local demo.
The only difference is ā itās now running inside aĀ scalable, production-grade ECS cluster.
š§¹ Step 4: Clean Up Resources
Once youāre done testing, itās good practice to destroy the infrastructure to avoid unwanted AWS charges:
terraform destroy --auto-approve
This will tear down all ECS services, roles, and networking resources you created for this project.
ā
Ā And thatās it!
Youāve successfully deployed yourĀ Flask Cognee AI Starter AppĀ on AWS ECS usingĀ Terraform. With just a few commands, we automated the entire infrastructure provisioning and deployment pipeline.
šÆ Conclusion
Congratulations! š Youāve successfully taken aĀ Flask-based Cognee AI Starter AppĀ from your local machine all the way to theĀ cloud using AWS ECS and Terraform.
In this blog, we learned how to:
UnderstandĀ Cognee AIĀ and its memory layer for LLMs.
Explore and test its key operations āĀ
.add,Ā.cognify, andĀ.search.Run the app locally to see how Cognee organizes and queries your data.
Provision AWS infrastructure usingĀ Terraform.
Deploy the Flask application onĀ ECS (Fargate)Ā and access it via a public URL.
This hands-on project demonstrates the power of combiningĀ AI, containerization, and infrastructure automationĀ to deploy intelligent applications in aĀ scalable, production-ready environment.
š Explore More
Check outĀ Cognee AIĀ here:Ā https://cognee.ai
-
Follow me on socials for more DevOps, cloud, and AI tutorials:
- GitHub:Ā https://github.com/Pravesh-Sudha
- Blog:Ā https://blog.praveshsudha.com
- LinkedIn:Ā https://www.linkedin.com/in/pravesh-sudha
Keep exploring, keep building, and stay tuned for moreĀ hands-on projects combining AI and DevOps! š












Top comments (0)