12

I have a Google Cloud Function on Python 3.7 that does not take any input arguments. When I try to run it, it gives the following error:

TypeError: google_cloud_function() takes 0 positional arguments but 2 were given

The actual function code looks something like this (with different bash functions called):

import subprocess

def google_cloud_function():
    subprocess.call(["ls"], shell=True)
    subprocess.call(["pwd"], shell=True)

Why would this be the case?

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
Balkan
  • 691
  • 1
  • 8
  • 22
  • Can you provide a description of the definition of your function? How are you attempting to run it? – Kolban Oct 05 '18 at 02:22
  • I've inserted code sample into the question. It is triggered by GCS bucket upload. The requirements.txt is fine. – Balkan Oct 05 '18 at 02:46

1 Answers1

20

A background Cloud Function triggered by something like Cloud Storage should have the following function signature:

def function(data, context):
    ...

The arguments need to be included even if you aren't using them.

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
  • So all cloud functions will have data and context passed into them it seems. Good to know. Cheers, – Balkan Oct 05 '18 at 03:29
  • 1
    If they're background triggers, yes. If the trigger is HTTP requests, they'll get a `request` argument instead. – Dustin Ingram Oct 05 '18 at 03:36
  • @DustinIngram thanks for the help, but could you please provide a full example? My function was like this : def deploy2():... my code... , and was getting the same errror Balkan was. Then tried your solution, but don't know if doing right, changed to this def test2(data, context):, but started getting error: `TypeError: test2() missing 1 required positional argument: 'context'`. Any tips? Cheers for the help – Rafael Paz Mar 01 '19 at 04:01
  • 1
    @RafaelPaz It sounds like you might be trying to deploy a function triggered by HTTP and not by a GCS event, in that case you'd want a function signature like `def test2(request)` instead. – Dustin Ingram Mar 01 '19 at 17:14
  • Thank you so much @DustinIngram for your reply. I tried that, but having other issues, don't wanna ask much, but if you could help me out with this: https://stackoverflow.com/questions/54938099/how-to-run-a-bash-script-inside-google-cloud-function-in-python?noredirect=1#comment96640261_54938099 and this https://serverfault.com/questions/956272/is-there-a-way-to-execute-a-bash-script-inside-google-cloud-functions-in-python would be much appreciated. Saw that you are very active about GCP topics and I'm pretty new. Cheers mate – Rafael Paz Mar 01 '19 at 19:53