0
votes

I have a main Google cloud function in nodeJS. Inside this function I want programmatically create another cloud function inside the same project. The code of this new cloud function can be stored someone in a zip file in Google cloud storage. I tried to follow the guides over here: https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/create

I was able to run the query in the test emulator but when I run it inside a google cloud function I don't know the exact code! preferably I want to do this in NodeJS. Otherwise Python.

Can someone help me please?

1
Its not clear what you are asking. I sense that there is an existing cloud function ... what can you tell us about it? Do you own it? Do you have source of it? What of the new Cloud Function ... how does that relate to the existing cloud function? - Kolban
Is your fonction to deploy already exists or do you want to create it on the fly? If exist, where it is stored? - guillaume blaquiere
@guillaumeblaquiere I updated the description. What do you mean with create it on the fly? The actual code? The code can be stored somewhere in a zip file. I would like to deploy this code in a new function. - Siem Peters
@Kolban I updated the description. I own the functions. I would like to deploy a new function within the same project. The code of this functions I very small. I can store the code somewhere in a zip file. - Siem Peters

1 Answers

2
votes

I did this in python:

  1. Create the first cloud function hello_world.

  2. Zip the function and move it to Google Cloud Storage.

  3. Create the second cloud function (by default is created with App Engine default service account, which has Editor Role. In case you will assign a different service account as identity, make sure your service account has the permissions to query metadata server and to create cloud functions)


import requests
import json


def make_func(request):


    # Get the access token from the metadata server
    metadata_server_token_url = 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token?scopes=https://www.googleapis.com/auth/cloud-platform'
    token_request_headers = {'Metadata-Flavor': 'Google'}
    token_response = requests.get(metadata_server_token_url, headers=token_request_headers)
    token_response_decoded = token_response.content.decode("utf-8")
    jwt = json.loads(token_response_decoded)['access_token']

    # Use the api you mentioned to create the function
    response = requests.post('https://cloudfunctions.googleapis.com/v1/projects/your-project/locations/us-central1/functions',
                               json={"name":"projects/your-project/locations/us-central1/functions/funct","runtime":"python37","sourceArchiveUrl":"gs://bucket/main.zip","entryPoint":"hello_world","httpsTrigger": {} },
                               headers={'Accept': 'application/json', 
                                        'Content-Type': 'application/json',
                                        'Authorization': 'Bearer {}'.format(jwt)} )   
    if response:
         return 'Success! Function Created'
    else:
         return str(response.json())  


Let me know if you have any questions