iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔥

How to directly execute Google Cloud Functions

に公開

Conclusion

There are two main options:

  • Execute "Test function" in the GCP console
  • Run the gcloud command

*Note: This method is also applicable to functions deployed using Cloud Functions for Firebase.

Use Cases / Challenges

  • You want to deploy a Cloud Functions function and execute it directly
  • You want to call a one-off throwaway function
  • You want to directly execute scheduled or HTTP call functions

Method via the GCP Console

  1. Log in with the desired account in the GCP console
  2. Enter the target project in the GCP console
  3. Switch to the desired environment in the GCP console
  4. Navigate to the Cloud Functions page
  5. Go to "Function details" by clicking the function name link
  6. Move to the "Testing" tab
  7. Execute "Test function"

Using the gcloud command

For more details, see the official documentation at the link.

gcloud functions call YOUR_FUNCTION_NAME --data '{"name":"Keyboard Cat"}'

https://cloud.google.com/functions/docs/calling/direct?hl=ja#using_the_gcloud_command-line_interface

Troubleshooting Errors

{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
How to handle the above error when executing an http onCall function:

  1. Include a data element in JSON format
  2. Execute

*Note: Due to Cloud Functions specifications, this is required even if no data is actually needed.

{
  "data": {
    "hoge": 'Enter arbitrary data here',
    "fuga": 'You must pass data with a "data" element in JSON format'
  }
}

References

https://cloud.google.com/functions/docs/calling/direct?hl=ja
https://stackoverflow.com/questions/49476231/how-to-call-firebase-callable-functions-with-http

Discussion