Create a 2nd gen Cloud Function by using the Google Cloud console
Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired, or by an HTTP request.
This page shows how to create and deploy a 2nd gen HTTP function using the Google Cloud console. This page is based on Node.js, but the process is similar for all of the runtimes.
This example uses the following function, which returns a message when triggered by an HTTP request:
const functions = require('@google-cloud/functions-framework'); functions.http('helloHttp', (req, res) => { res.send(`Hello ${req.query.name || req.body.name || 'World'}!`); });
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.
Create a function
Open the Functions Overview page in the Google Cloud console:
Go to the Cloud Functions Overview page
Make sure that the project for which you enabled Cloud Functions is selected.
Click Create function.
For the Environment, select 2nd gen.
Name your function, for example,
function-1
.In the Authentication field, select Allow unauthenticated invocations.
Allow unauthenticated invocations lets you reach the function without authentication. To require authentication, choose Require authentication.
Click Next.
In the Source code field, select Inline editor. In this exercise, you will use the default function provided in the editor.
Use the Runtime dropdown to select the desired runtime. This example uses Node.js.
Deploy the function
At the bottom of the page, click Deploy.
After clicking Deploy, the Google Cloud console redirects to the Function details page.
While the function is being deployed, the icon next to it is a small spinner. After the function finishes deploying, the spinner turns to a green check mark.
Test the function
To test the function, open the URL at the top of the Function details page to see a web browser displaying the message "Hello World!"