Deploying a Functions (serverless) app on Google Cloud

Campion
Campion
Apr 19, 2018 · 2 min read
Google Cloud

This tutorial will show you how to deploy a Functions app to Google Cloud


Prerequisites

  • You have the gcloud tool installed
  • You have a project in GCP

Note: you must have the gcloud beta version installed

To install gcloud beta use:

gcloud components update && gcloud components install beta

Let’s Get Started

First, let’s make a directory and enter it:

mkdir ~/hello_functions && cd hello_functions

Now, make a index.js file and put this in it:

exports.helloFunctions = (req, res) => {
res.send('Hello Functions!');
};

This makes a function called helloFunctions that responds to a GET request with Hello Functions!

That’s it?

Yep, pretty much. Let Google Cloud do the rest.

Make sure your Project ID (from GCP) is set as an environmental variable and configure gcloud to use it:

PROJECT_ID=<project-id>
gcloud config set project $PROJECT_ID

Now to deploy your Functions app:

gcloud beta functions deploy helloFunctions --trigger-http

Note: it may ask you to enable the functions API, go ahead and say yes

Once it’s deployed, you can find your function’s URL with:

gcloud beta functions describe helloFunctions | grep url

The URL should look like:

https://[GCP-REGION]-[PROJECT_ID].cloudfunctions.net/helloFunctions

Open that URL in your browser and you should see Hello Functions!

Testing Locally

If you want to test your Function locally before deploying, the cloud-functions-emulator makes it super easy!

First install the functions-emulator:

npm install -g @google-cloud/functions-emulator

Then start it up:

functions-emulator start

And now deploy the function locally:

functions-emulator deploy helloFunctions --trigger-http

And test it!

functions-emulator call helloFunctions

You should see something like:

ExecutionId: d02ce9e7-bf2c-44fb-89ab-ab8445277ecd
Result: Hello Functions!

⒡ → 👋 Functions

Thanks for reading! If you have any questions, comment below!

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Campion

Written by

Campion

Student at the University of Washington

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade