Search code examples
pythongoogle-cloud-platformgoogle-cloud-functionsgcloud

Gcloud functions deploy with specific python file and specific function name


I'm trying to deploy several Cloud Functions which are all in the same repository but in different python files.

Basically, I have those two functions

my_repo
- function_1_folder
  - function_1.py
  - requirements.txt
  - .gcloudignore
- function_2_folder  
  - function_2.py
  - requirements.txt
  - .gcloudignore

Inside function_1.py I have function_1() which I want to deploy to my cloud function called function1 (note there is no underscore here), and same for function_2.

I go to function_1_folder and I have specified an entry point (--entry-point function_1) but I get a "missing main.py" error.

How can I specify both python filename and function name (if possible) ? Will gcloud also deploy the requirements.txt which is needed to install the packages my function depends on ?

Thank you


Solution

  • You can't name the source file arbitrarily. It's part of the structuring requirements that your Python source file should always be named main.py. This is how your directory should look like:

    my_repo
     - function_1_folder
       - main.py
       - requirements.txt
       - .gcloudignore
     - function_2_folder  
       - main.py
       - requirements.txt
       - .gcloudignore
    

    The flag --entry-point is used to specify the name of the function on your source file. For example (as HTTP):

    main.py

    def function_1(request):
        return 'Hello World'
    

    Run this command inside function_1_folder:

    gcloud functions deploy function1 --entry-point function_1 --runtime python37 --trigger-http 
    

    To answer your final question, requirements.txt is included along with your main.py and is a valid configuration. Those dependencies will be installed during build time.

    As additional reference, see: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/functions