I have a simple flask application. And I need to run it on Cloud Run with enabled option "Manage authorized users with Cloud IAM."
app.py
from flask import Flask
api_app = Flask(__name__)
endpoints.py
from app import api_app
@api_app.route("/create", methods=["POST"])
def api_create():
# logic
main.py
from app import api_app
from endpoints import *
if __name__ == "__main__":
api_app.run(host="0.0.0.0", port=8080)
And it works if i run it locally. It also works well if run in docker. When I upload the application image to Cloud Run, there are no deployment errors. But when I try to call the endpoint I get an error even though everything is working fine locally.
Request example:
import urllib
import google.auth.transport.requests
import google.oauth2.id_token
import requests
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "test.json"
audience="https://test-service-*****.a.run.app"
req = urllib.request.Request(audience)
auth_req = google.auth.transport.requests.Request()
token = google.oauth2.id_token.fetch_id_token(auth_req, audience)
response = requests.post(f"{audience}/create", data={
"text": "cool text"
}, headers={
"Authorization": f"Bearer {token}"
})
Response:
<!doctype html>
<html lang=en>
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
In Cloud Run logs I have only one warning log
POST 400 820b 3ms python-requests/2.26.0 https://test-service-*****.a.run.app/create
Dockerfile:
FROM python:3.8-slim-buster
WORKDIR /alpha
ENV PYTHONUNBUFFERED True
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
CMD exec gunicorn --bind :8080 --workers 1 --threads 8 --timeout 0 main:api_app
Why can't I reach the application endpoint in Cloud Run?