DEV Community

Cover image for Deploy cloud function using CLI
Alan
Alan

Posted on

Deploy cloud function using CLI

Sometimes, deploying a FaaS (Function As a Service) becomes necessary to simplify our main monolith or when dealing with a microservices infrastructure.

In this blog, I will demonstrate how to deploy certain cases of FaaS in Google Cloud.

Http function

The most common scenario involves an HTTP invocation, and for a Node.js function, the syntax follows an Express-style.

exports.entry = (req,res) => {
  const {body,params,query} = req;
  const message = body?.message || params?.message || query?.message || 'Hello World';
  res.status(200).send(message);
}
Enter fullscreen mode Exit fullscreen mode

We need to export a function with the same name as our declared entry point. This function is the one that GCP (Google Cloud Platform) should call on each run.

First, navigate to the root path of your project, and then run the following command:

gcloud functions deploy my-function --runtime nodejs20 --region us-west1 --source . --entry-point=entry --trigger-http --allow-unauthenticated --project=my-dev-project-412620
Enter fullscreen mode Exit fullscreen mode

We are instructing the CLI to deploy a function named 'my-function,' utilizing Node.js v20 as its runtime. The function has an entry point named 'entry' and is accessible via HTTP in the us-west1 region (Oregon). It can be invoked without a GCP IAM token (use --no-allow-unauthenticated to disable this). This resource will be deployed in 'my-dev-project-412620'.

After running this command, if the CLI displays a message such as 'API [cloudfunctions.googleapis.com] not enabled on project [my-dev-project-412620]. Would you like to enable and retry (this will take a few minutes)? (y/N)?' simply type 'y' to proceed.

Now, navigate to gcloud -> functions, and you should be able to view our newly deployed function.

Image description

Now, you can make calls using the Cloud Function URL

Image description

Tips

SQL connections

Cloud functions are serverless. If you need to run a function with a connection to a database, you may encounter a pool connection timeout error. To prevent this error, it is a good practice to set the connection pool limit to 1 and the pool timeout to 0.

Fast function

Cloud functions reuse existing containers to handle new calls. Following this logic, a good practice is to separate one-use code from the request-logic code. Define the one-use code outside of the main function and keep only the request-handling logic inside the specified entry-point function.

const {connect_to_db} = require('./db');

connect_to_db()
// connections and another code here
exports.entry = (req,res) => {
  // request handler logic here
  const {body,params,query} = req;
  const message = body?.message || params?.message || query?.message || 'Hello World';
  res.status(200).send(message);
}
Enter fullscreen mode Exit fullscreen mode

Flags

--gen2

By using this flag, you can enable the second-generation function and deploy a Cloud Run as a function. Additionally, you can define the CPU and memory usage using the --cpu and --memory flags.

--env-vars-file

With this flag, you can define an environment file in YAML format to pass variables to the Cloud Function.

DATABASE_URL: <MY_URL>
PORT: 3000
Enter fullscreen mode Exit fullscreen mode

--vpc-connector

This flag is used to designate the VPC Connector for a Cloud Function. It allows you to specify the VPC network that your function will be connected to, providing control over networking configurations

Resource

CLI documentation

You know some tricks about cloud functions and serverless structure?

Top comments (0)