Primary image for Serverless Tasks in Django with AWS Lambda

Serverless Tasks in Django with AWS Lambda

Periodic tasks are the heartbeat of many Django apps—sending emails, clearing out stale data, or crunching reports. Sure, tools like Celery can do the job, but they come with baggage: managing message brokers, worrying about infrastructure, and scaling costs as tasks pile up.

Enter AWS Lambda: your serverless sidekick. No servers to babysit. No unnecessary bills. Just clean, efficient execution when—and only when—you need it. Pair it with EventBridge for dead-simple scheduling, and you’ve got a setup that’s scalable, reliable, and almost too easy.

Ready to ditch the overhead and let AWS handle the heavy lifting? Let’s break it down.

Real-world Example

Imagine you have a task to delete inactive users who haven’t logged in for over 30 days. Instead of running this task in your Django app or setting up Celery, you can offload it to AWS Lambda.

Here’s the logic to create a task that cleans up inactive users:

# tasks.py

from datetime import timedelta
from django.utils.timezone import now
from myapp.models import User

def cleanup_inactive_users():
    threshold = now() - timedelta(days=30)
    User.objects.filter(last_login__lte=threshold).delete()

Simple, right? Next, let’s wrap this in a Lambda function.

Writing the Lambda Function

AWS Lambda can run Python code, so you can reuse most of your Django logic. Here’s how:

# lambda_function.py

import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
django.setup()

from myapp.tasks import cleanup_inactive_users

def lambda_handler(event, context):
    cleanup_inactive_users()
    return {"statusCode": 200, "body": "Inactive users cleaned up"}

Packaging Your Function for Lambda

AWS Lambda needs everything packaged into a single .zip file, including dependencies. Use pip to install dependencies into a folder, and then zip everything together:

pip install -r requirements.txt -t ./lambda_package
cd lambda_package
zip -r ../lambda_function.zip .
cd ..
zip -g lambda_function.zip lambda_function.py

Deploying to Lambda

Deploying to AWS is straightforward if you have the AWS CLI set up:

aws lambda create-function \
    --function-name cleanupInactiveUsers \
    --runtime python3.9 \
    --role <execution-role-arn> \
    --handler lambda_function.lambda_handler \
    --zip-file fileb://lambda_function.zip

Ensure the IAM role associated with the function has the necessary permissions to access your database.

Scheduling the Task with EventBridge

EventBridge lets you run your Lambda function on a schedule. Here’s how you can set it up:

  • Open the EventBridge Console.
  • Create a new rule.
  • Choose “Schedule” and define a cron expression, e.g., 0 0 * * * for midnight UTC.
  • Attach your Lambda function as the target.

That’s it. Your task now runs at the interval you specified.

Integrating Lambda with Django

A few things to keep in mind when integrating Lambda with Django:

Shared Database: Make sure your Lambda function connects to the same database as your Django project.

Environment Variables: Store sensitive credentials like database URLs in Lambda environment variables or AWS Secrets Manager.

Automation: If you’re deploying with Terraform, consider automating the entire setup, from the Lambda function to EventBridge rules.

Debugging and Monitoring

Monitoring is key for serverless tasks:

  • CloudWatch Logs: Lambda logs everything to CloudWatch. You can tail logs in real time using the AWS CLI:

    aws logs tail /aws/lambda/cleanupInactiveUsers --follow
    
  • Alerts: Use AWS CloudWatch Alarms to get notified if something breaks.

  • Retry Logic: Configure retry policies in EventBridge to handle occasional failures.

Conclusion

AWS Lambda is a game-changer for running scheduled tasks, especially when simplicity, scalability, and cost efficiency are your top priorities. It eliminates the need for managing extra infrastructure, automatically scales with your workload, and lets you pay only for what you use. For Django projects, it offers a lightweight and powerful alternative for periodic tasks like data cleanup, email notifications, or generating reports.

So, when should you use Lambda? If your tasks are lightweight, time-bound, and don’t require complex orchestration, Lambda shines. It’s perfect for setups where infrastructure costs feel overkill or when you need something that works without constant babysitting. On the other hand, if you’re dealing with tasks with high concurrency, long execution times, or intricate dependencies, tools like Celery might still be a better fit.

Think of AWS Lambda as your go-to for cutting the clutter and running tasks on autopilot. It’s not just about saving money—it’s about saving time and focusing on what truly matters: building features, not babysitting servers. The next time you’re setting up periodic tasks, ask yourself, “Do I really need the complexity of Celery, or can Lambda get it done?” You might find that serverless is exactly what your project needs to stay agile and efficient.

Sanyam Khurana

About the author

Sanyam Khurana

Sanyam Khurana earned a Master's degree in Computer Science from Georgia Tech in Atlanta, US, and is widely known on the web as "CuriousLearner." He has spoken at numerous international conferences and is a staunch advocate …

View Sanyam's profile