Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django POST listener in Elastic Beanstalk to receive AWS Worker Tier requests

I'm trying to set up a Worker Environment to run a background task. I have the same application version running on two Environments, one is the web server and the other one is the Worker.

I need to delete files periodically according to expiration date. I've mapped a view to be the URL on localhost where messages will be forwarded as HTTP POST requests. The task is being scheduled and seems like SQS is running but the messages are all at the WorkerDeadLetterQueue.

At the log file I got the requests being made but a 403 error:

/var/log/httpd/access_log:

"POST /networks_app/delete_expired_files HTTP/1.1" 403 2629 "-" "aws-sqsd/2.0"

and this at /var/log/aws-sqsd/default.log:

message: sent to %[http://localhost:80/networks_app/delete_expired_files] 2016-01-23T14:58:05Z http-err: d5f645cf-ce15-40bc-8ee3-34acb79e797b (4) 403 - 0.007

Here is my views.py code:

def delete_expired_files(request):
    if request.method == 'POST':
        users = DemoUser.objects.all()
        for user in users:
            documents = Document.objects.filter(owner=user.id)
            if documents:
                for doc in documents:
                    now = timezone.now()
                    if now >= doc.date_published + timedelta(days = doc.owner.group.valid_time):
                        doc.delete()

The cron.yaml file:

version: 1
cron:
 - name: "delete_expired_files"
   url: "/networks_app/delete_expired_files"   
   schedule: "* * * * *" 

If I access the URL via browser it works, It shows a GET request at the log_file of my web application server.

What should I do to make the Worker Environment execute the task?
Why when the Worker tries to send a message, it returns a 403 error?
Is it related to the role permissions?
Should I code a specific listener in Django?
Is using celery the best way to solve this issue?

like image 345
Jaqueline Passos Avatar asked Jan 23 '16 17:01

Jaqueline Passos


People also ask

Is Elastic Beanstalk in free tier?

There is no additional charge for AWS Elastic Beanstalk. You pay for AWS resources (e.g. EC2 instances or S3 buckets) you create to store and run your application. You only pay for what you use, as you use it; there are no minimum fees and no upfront commitments.

What is the worker environment used for in Elastic Beanstalk?

Worker environments run a daemon process provided by Elastic Beanstalk. This daemon is updated regularly to add features and fix bugs. To get the latest version of the daemon, update to the latest platform version.

Which environment tier should be used for batch applications in AWS Elastic Beanstalk?

AWS resources created for a worker environment tier include an Auto Scaling group, one or more Amazon EC2 instances, and an IAM role. For the worker environment tier, Elastic Beanstalk also creates and provisions an Amazon SQS queue if you don't already have one.


1 Answers

The internal SQS daemon that creates the POST request does not include a CSRF token, which can lead to '403 Forbidden' errors.

A potential workaround for this is to mark the method as csrf_exempt:

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):
    return HttpResponse("hello, world")
like image 185
Stephen M. Avatar answered Oct 18 '22 21:10

Stephen M.