0

I am having a problem running a Cloud function I created using Python on the Google Cloud Platform. I am attempting to trigger a DAG using the cloud function and I follow all the rules specified in the official documentation to do it. I keep getting this error code.

 EDT

gcs-dag-trigger-function

68hsnlvog0g6
Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/__init__.py", line 171, in view_func
    function(data, context)
  File "/workspace/main.py", line 48, in trigger_dag
    make_iap_request(
  File "/workspace/main.py", line 87, in make_iap_request
    raise Exception(
Exception: Bad response from application: 404 / {'Date': 'Fri, 15 Jul 2022 16:25:22 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Vary': 'Accept-Encoding', 'Server': 'gunicorn', 'X-Robots-Tag': 'noindex, nofollow', 'Set-Cookie': 'session=d8fb37f6-bd2d-4a2c-8abc-e528eadc9ac3.hUuadQnCRiJzbuufKObZIMtsTL8; Expires=Sun, 14-Aug-2022 16:25:22 GMT; HttpOnly; Path=/; SameSite=Lax', 'Content-Encoding': 'gzip', 'Via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"', 'Transfer-Encoding': 'chunked'} / '\n\n<!DOCTYPE html>\n<html lang="en">\n  <head>\n    <title>Airflow 404</title>\n    <link rel="icon" type="image/png" href="/static/pin_32.png">\n  </head>\n  <body>\n    <div style="font-family: verdana; text-align: center; margin-top: 200px;">\n      <img src="/static/pin_100.png" width="50px" alt="pin-logo" />\n      <h1>Airflow 404</h1>\n      <p>Page cannot be found.</p>\n      <a href="/">Return to the main page</a>\n      <p>3405c1ef5ce7</p>\n    </div>\n  </body>\n</html>'

Can anyone help me with this?

DamianO
  • 53
  • 6
  • Please include a minimally reproducible example of the code. – DazWilkin Jul 15 '22 at 20:12
  • ```def trigger_dag(data, context=None): client_id = 'CLIENTID' webserver_id = 'TP' dag_name = 'Sdag' if USE_EXPERIMENTAL_API: endpoint = f'api/experimental/dags/{dag_name}/dag_runs' json_data = {'conf': data, 'replace_microseconds': 'false'} else: endpoint = f'api/v1/dags/{dag_name}/dagRuns' json_data = {'conf': data} webserver_url = ( 'https://' + webserver_id + '.appspot.com/' + endpoint ) make_iap_request( webserver_url, client_id, method='POST', json=json_data)``` – DamianO Jul 16 '22 at 23:41
  • Forgive the formatting. But every time I trigger the cloud function, the make_iap_request function returns an error stating it cannot find the web url. – DamianO Jul 16 '22 at 23:42
  • 2
    @DamianO, It's always best to put the code snippet along with the question, you can add your code snippet in the question rather than commenting, it gives better visibility and increases the chances of getting the answers :) – Abhishek Jul 17 '22 at 08:42
  • 2
    Absent more information from you, the only conclusion is that you need to focus on correcting the URL that your code's generating. You didn't include the reference and I assume you're using [Trigger DAGs with Cloud Functions](https://cloud.google.com/composer/docs/how-to/using/triggering-with-gcf). So `webserver_id` and/or `dag_name` are incorrect. – DazWilkin Jul 17 '22 at 15:24
  • @DazWilkin I shortened the code snippet provided by abbreviating the dag_name and webserver_id provided in the Trigger DAGs with Cloud Functions tutorial provided by Google. I replicated the exact code and added the required modules in the requirements.txt file. I can't figure out what the problem is. I will have to review my deployment and see if there is anything I can do to fix it. – DamianO Jul 18 '22 at 12:33
  • 1
    Are you able to replicate the code's `POST` command using something like `curl`? That may help you diagnose the issue. – DazWilkin Jul 18 '22 at 15:43
  • @DazWilkin I'm not sure how to do that to be honest. I re-posted my question making sure to include the full error code and the function I was attempting to upload. Here is the link to that [link](https://stackoverflow.com/questions/73024654/trigger-cloud-composer-using-google-cloud-function) – DamianO Jul 18 '22 at 15:51
  • It's better practice to edit existing questions with updated info rather than duplicate them. – DazWilkin Jul 18 '22 at 15:55
  • 1
    If you're using Linux, you could try `curl --request POST --header "Authorization: Bearer $(gcloud auth print-identity-token)" --data "{'conf': ${DATA}}" "https://${WEBSERVER_ID}.appsspot.com/api/v1/dags/${DAG_NAME}/dagRuns"`. You'll need to replace `${WEBSERVER_ID}`, `${DATA}` and `${DAG_NAME}` with the values from your code. – DazWilkin Jul 18 '22 at 16:03
  • @DazWilkin I will attempt this and see if I am able to obtain additional information on this problem. – DamianO Jul 18 '22 at 16:51
  • @DazWilkin I attempted the curl command but received a Connection timed-out response. It was still not able to return the url. – DamianO Jul 19 '22 at 00:04
  • Interesting. It timed out but you didn't get a 404, correct? You can try increasing the timeout used by curl with `--connection-timeout`. You can set it to something high like 10 seconds (`--connection-timeout 10`). – DazWilkin Jul 19 '22 at 00:45
  • @DazWilkin I still received the same timeout error. The timeout is already set to a default of 90 seconds which is substantially longer than 10 seconds – DamianO Jul 20 '22 at 14:29
  • `curl` does not appear to return the 404 (not found) whereas the code does. It would be helpful to understand why. Other than that, I'm unable to help further. – DazWilkin Jul 20 '22 at 15:04
  • @DazWilkin I understand. Thank you so much for your assistance thus far. If I am able to resolve the issue I will post it to this thread. – DamianO Jul 20 '22 at 15:49
  • @DazWilkin I attempted the curl command again and now get this error message: ```Invalid IAP credentials: JWT audience doesn't match this application ('aud' claim (618104708054-9r9s1c4alg36erliucho9t52n32n6dgq.apps.googleusercontent.com) doesn't match expected value (480044398815-fs0g5tdafcg4v8q0srm9li8p1i073ugi.apps.googleusercontent.com))``` Can this help me fix my problem? – DamianO Jul 20 '22 at 17:20
  • For authentication problems, you may check this [documentation](https://cloud.google.com/iap/docs/concepts-overview) and let me know if it helps – Anjela B Jul 21 '22 at 05:33
  • 1
    @DazWilkin I had help in the other thread in resolving my issue. I had to set the USE_EXPERIMENTAL_API to False and manually register an airflow user using my existing service account. Thank you so much for all your help. – DamianO Jul 21 '22 at 23:40
  • @AnjelaB I had the problem resolved. Thank you. It wasn't an authentication problem. – DamianO Jul 21 '22 at 23:41
  • Can you post what helped you in the answer section so that the community can benefit as well. – Anjela B Jul 22 '22 at 04:34

1 Answers1

2

@AnjelaB it turns out because I am using Airflow 2 (this is the version which launches in Cloud Composer), you have to use the code, USE_EXPERIMENTAL_API=False in your cloud function since you are now using the stable API. Secondly, your regular service account won't work because its characters are too long (i.e. >64) & you can only use a SA with a limited number of characters. The work around is to manually add a SA as an Airflow user using the documentation found at this link

DamianO
  • 53
  • 6
  • 1
    If anyone would like a detailed explanation on how I executed this code, I published a walkthrough on Medium.com [link](https://dohmhen.medium.com/advanced-google-cloud-composer-pipeline-cloud-function-trigger-bb6d2a913f2c) – DamianO Jul 22 '22 at 18:11