How to set endpoint path to my google cloud function (using serverless framework)

Viewed 453

I am using serverless framework to deploy a cloud function in the GCP with the following configuration:

service: myname

provider:
  name: google
  stage: prod
  runtime: nodejs10
  region: us-central1
  project: myname
  credentials: keyfile.json
  environment:
    IS_PROD: 'true'

plugins:
  - serverless-google-cloudfunctions

package:
  exclude:
    - node_modules/**
    - .gitignore
    - .git/**

functions:
  clear:
    handler: clearCommand
    events:
      - http: clear

I am using serverless-google-cloudfunctions plug-in version 3.1.0. After deployment, the address/path becomes following:

https://us-central1-myname.cloudfunctions.net/myname-prod-clear

I am wondering if there is a way to set the path by myself to be like the following:

https://us-central1-myname.cloudfunctions.net/clear

Is there a way to set this? The serverless framework way is preferred.

3 Answers

You can specify the name in the function definition in the serverless.yml, as mentioned in the Gareth's comment in Guillaume's answer.

So, in your serverless.yml, it will be like following,

functions:
  clear:
    name: clear
    handler: clearCommand
    events:
      - http: clear

However, please note that in the serverless-google-cloudfunctions version 3.1.0, the function name assignment still has an issue. The deployment works fine and the deployed function name in the GCP is also correct. But it is not properly reflected in the deployment status at the end of the serverless deploy execution.

EDIT: The deployment status issue is fixed in the version 3.1.1.

In the earlier version, we still need to do the following hacky workaround to manually update the serverless-googlecloudfunctions in the node_modules.

In the info/lib/displayServiceInfo.js, replace the current getFunctionNameInService function implementation,

const getFunctionNameInService = (funcName, service, stage) => {
  let funcNameInService = funcName;
  funcNameInService = funcNameInService.replace(service, '');
  funcNameInService = funcNameInService.replace(stage, '');
  funcNameInService = funcNameInService.slice(2, funcNameInService.length);
  return funcNameInService;
};

with

const getFunctionNameInService = (funcName, service, stage) => {
  let funcNameInService = funcName;
  funcNameInService = funcNameInService.replace(`${service}-`, '');
  funcNameInService = funcNameInService.replace(`${stage}-, '');
  return funcNameInService;
};

I hope this will help.

With the serverless framework, it seems to not be possible. Look at this line in the source code

The deploymentName is generated automatically, no params to override this. I didn't look deeper to find where the clear value is set, but, obviously, it's a static value.

In fact the name of your Cloud Functions that you can find in the console. If you deploy yourselves the function, you can choose the name that you want and thus achieve what you want to

The last part of the link is the Cloud Functions name. It is done at time to create the Cloud Functions, if you are creating the Cloud function using the Cloud Console, you just have to introduce the name.

On other hand, if you are using gcloud to deploy your Cloud Functions, you have to add the parameter name, for example:

gcloud functions deploy my_function --runtime=python37 --trigger-event=providers/cloud.firestore/eventTypes/document.write --trigger-resource=projects/project_id/databases/(default)/documents/messages/{pushId}

This will result in something like this:

https://us-central1-myname.cloudfunctions.net/[Your_CFName]

Take into account that once you name the Cloud Functions it couldn't be changed

Related