Python 3.x/Flask – Using Cloudwatch handler throws CORS error when login in

Question:

I’m working on a project that uses flask w/ python 3.10 and react.
Everything works fine, but the logs were a local file and we decided to move it to Cloudwatch.
Wrote the code, changed the loguru sink, everything seemed ok, It works locally!
When we deploy the application, we get a CORS error on the react application, can’t even login anymore. Removing the cloudwatch handler from the loguru sink makes everything works smoothly again.
The frontend doesn’t log anything to cloudwatch or the backend and I really can’t understand what’s going on here.

The exact error on console is:

Access to XMLHttpRequest at 'my.ip:port/login' from origin 'my.ip' has been blocker by CORS policy
: Response to preflight request didn`t pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

My code is the following:

### Loguru config ###
from cloudwatch import cloudwatch
from loguru import logger
...
handler = cloudwatch.CloudWatchHandler( log_group = 'my_log_group', access_id='my_access_id', access_key='my_access_key')
logger.add(sink=handler, level='INFO')
### App config ###
from flask import Flask
from flask_cors import CORS
...
app: Flask = create_app()
api = Api(app)

#original config
cors = CORS(app, resources='*', headers='Content-Type')

# Tried like this, since resources by default should be *
# cors = CORS(app, supports_credentials=True)
# app.config['CORS-HEADERS'] = 'Content-Type'

# And like this
# cors = CORS(app, resources='*', headers='Content-Type', supports_credentials=True, send_wildcard=True)
# app.config['CORS-HEADERS'] = 'Content-Type'

Any help is much appreciated

Asked By: Felipe Bandeira

||

Answers:

For anyone having same or similar issue: The problem was never CORS.

At the UWSGI config we were referencing the plugin to be used, python3 in our case.
With that config, it was looking for the python plugin and couldn’t locate it.

The container was running, the UWSGI started BUT the Flask application couldn’t be started, and when there’s some error in the application, UWSGI blames CORS intead of throwing a decent hint.

Why this only happened when I used the cloudwatch handler? Have no idea.
Why the app was working smoothly before? Not a clue, but now it’s working and I’m happy with that. Nginx redirect is not working but that’s another problem.

Answered By: Felipe Bandeira