Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Dataprep plan HTTP request to cloud function

I want to trigger a respective cloud function if data prep pipeline pipeline fails or succeeds in plan.
I have created a plan but I'm not able to find proper credentials to invoke cloud function via HTTP request. getting 403 error.
Where I can get the headers and which request post or get to make the request.
Please some guide me.
Let me know if any extra details is required.

0 2 181
2 REPLIES 2

I'm able to make post request via postman but not via HTTP task in data prep plan.
I'm getting error code 500.

The 403 (Forbidden) error typically indicates that the HTTP task in your Dataprep plan lacks the necessary permissions to invoke your Cloud Function. The 500 (Internal Server Error) usually points to issues with the function's code or configuration.

1. Cloud Function Authentication:

  • Service Account:

    • Create a dedicated service account for your Dataprep plan. Grant this service account the "Cloud Functions Invoker" role on the specific Cloud Function you want to trigger.

  • OAuth Token:

    • In your Dataprep HTTP task, use OAuth 2.0 authentication. Generate an access token using the service account’s credentials. Include this token in the Authorization header of the request.

2. HTTP Task Configuration:

  • Method:

    • Use the POST method to invoke Cloud Functions over HTTP.

  • URL:

    • The URL should be the specific invocation URL of your Cloud Function. You can find this URL in the Cloud Functions console and it typically looks like https://<REGION>-<PROJECT_ID>.cloudfunctions.net/<FUNCTION_NAME>.

  • Headers:

    • Authorization: Use Bearer <your_oauth_token> where <your_oauth_token> is the access token you generated.

    • Content-Type: Set to application/json to indicate that the payload is in JSON format.

  • Body (Optional):

    • Include a JSON payload to pass data to your Cloud Function if needed. This is useful for sending information about the Dataprep job status.

3. Error Handling:

  • In the Cloud Function:

    • Implement robust error handling within your Cloud Function to catch and log any issues. This can help diagnose 500 errors by providing more detailed error logs.

  • In Dataprep:

    • Incorporate error handling within your Dataprep plan. If the HTTP task fails, log the error details and consider implementing a retry mechanism.

Code Example:

Here’s an example of how your Dataprep HTTP task configuration might look (using Trifacta's syntax):

post('https://<region>-<project_id>.cloudfunctions.net/<function_name>',
     headers = {
         'Authorization': 'Bearer ' + getAccessToken('path/to/service-account-key.json'),
         'Content-Type': 'application/json'
     },
     body = '{"job_id": "$jobId", "status": "$jobStatus"}'
)

Troubleshooting Tips:

  • Check Logs:

    • Examine the Cloud Function logs in the Cloud Console for error messages to identify issues with the function execution.

  • Test with Postman:

    • Use Postman to test invoking the function with the service account’s credentials. If successful, replicate the settings in your Dataprep HTTP task.

  • Double-Check URLs and Permissions:

    • Ensure the Cloud Function’s URL is correct and that the service account has the necessary invocation permissions.

Additional Considerations:

  • Environment Variables:

    • Consider using environment variables in your Dataprep plan to store sensitive information such as the OAuth token and Cloud Function URL securely.

  • Event-Driven Architecture:

    • For a more scalable approach, you could use Cloud Pub/Sub to trigger Cloud Functions upon Dataprep job completion, instead of relying solely on HTTP tasks. This can improve the reliability and scalability of your architecture.