DEV Community

Cover image for šŸš€ Deploying Cognee AI Starter App on AWS ECS Using Terraform

šŸš€ Deploying Cognee AI Starter App on AWS ECS Using Terraform

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>"
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

šŸ“Ā 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

ā³ 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Keep exploring, keep building, and stay tuned for moreĀ hands-on projects combining AI and DevOps! šŸš€

Top comments (0)