AWS Elastic BeansTalk Django cronjob post request return return 403 error

I am working on a software function in which I have to periodically delete files using Django + cron + AWS . The problem is that I cannot get it to work. What is the best way to make it work? Is any AWS configuration missing? I set up one web server and one worker , deployed the same version of the application on them. A task is a view displayed in url (access to the url that the function is executing). In the working environment, a confirmation message appears:

Successfully uploaded 1 scheduled task from cron.yaml.

But also error 403 for working access_log :

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

cron.yaml

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

Url in urls.py :

 urlpatterns = [ url(r'^delete_expired_files', views.delete_expired_files, name='delete_expired_files'), ] 

to delete files from views.py :

 def delete_expired_files(request): 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() 

My IAM roles:

AmazonSQSFullAccess

AmazonS3FullAccess

AWSElasticBeanstalkFullAccess

AmazonDynamoDBFullAccess

If I access the URL through a browser, the task is executed (expired files are deleted). However, the work environment had to access the URL and complete the task automatically, and not just when accessing the URL through a browser. How can I make it work?

+5
source share
1 answer

I had a similar problem. In my case, I needed to change 2 things to make them work:

  • Ensure that the view is configured to take a POST action from AWS. I used to have my setup only as GET, and AWS doesn't seem to support GET cron requests.

  • After it supports POST, make it CSRF-free, so Django is not afraid that there is a CSRF threat when AWS makes POST requests without a CSRF token . You can use the @csrf_exempt decorator described in this SO answer ; in my case, it was a bit more complicated if I used a cool view and I found this other SO answer on how to enable the class-based @csrf_exempt decorator.

+2
source

Source: https://habr.com/ru/post/1242322/


All Articles