Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 318
MTZ4 Avatar asked Feb 14 '26 12:02

MTZ4


1 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.

like image 121
Aprian Diaz Novandi Avatar answered Feb 16 '26 03:02

Aprian Diaz Novandi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!