Deploy cloud function using CLI
Tags: googlecloud, node, devops
Published: 2024-01-28 22:02:40

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.

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

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:

```bash
gcloud functions deploy my-function --runtime nodejs20 --region us-west1 --source . --entry-point=entry --trigger-http --allow-unauthenticated --project=my-dev-project-412620
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5w3a2u1317rpzhq1eg61.png)

Now, you can make calls using the Cloud Function URL

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oonqul7oevb5ec15vr2.png)

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

```js
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);
}
```

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

```yaml
DATABASE_URL: <MY_URL>
PORT: 3000
```
#### --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](https://cloud.google.com/sdk/gcloud/reference/functions/deploy#--[no-]allow-unauthenticated)

You know some tricks about cloud functions and serverless structure?