1. Introduction
Overview
Cloud Run functions is a new way to deploy workloads using the familiar GCF eventing paradigms and function signature. Instead of using our opinionated build process and deployment configurations, Cloud Run functions gives you direct control over the underlying Service created on Cloud Run.
With Cloud Run functions, we provide the simple UX of Cloud Run source deploy, giving developers complete control over their workloads using Cloud Run configurations.
This codelab uses nodejs samples in the examples below. However, you can use the Cloud Functions 2nd gen code samples in the language of your choice:
- Python function samples
- Nodejs function samples
- Go function samples
- Java function samples
- PHP function samples
- Ruby function samples
- .NET function samples
What you'll learn
- How to deploy a Cloud Run function that is triggered by a HTTP request
2. Set Environment Variables and Enable APIs
Upgrade gcloud CLI
First, you will need to have a recent version of the gcloud CLI installed. You can update the CLI by running the following command:
gcloud components update
Setup environment variables
You can set environment variables that will be used throughout this codelab.
PROJECT_ID=<YOUR_PROJECT_ID> REGION=<YOUR_REGION, e.g. us-central1> gcloud config set project $PROJECT_ID SERVICE_NAME=crf-http-codelab
Enable APIs
Before you can start using this codelab, there are several APIs you will need to enable. This codelab requires using the following APIs. You can enable those APIs by running the following command:
gcloud services enable run.googleapis.com \ cloudbuild.googleapis.com \ storage.googleapis.com \ artifactregistry.googleapis.com
3. Create a HTTP function
First, create a directory for the source code and cd into that directory.
mkdir -p cloud-run-functions/$SERVICE_NAME && cd $_
Then, create a package.json
file with the following content:
{ "dependencies": { "@google-cloud/functions-framework": "^3.0.0" } }
Next, create an index.js
file with the following content:
const functions = require("@google-cloud/functions-framework"); functions.http("helloHttp", (req, res) => { res.send(`Hello ${req.query.name || req.body.name || "World"}!`); });
4. Deploy the function
Now you can deploy the Cloud Run function by running the following command:
gcloud beta run deploy $SERVICE_NAME \ --source . \ --function helloHttp \ --region us-central1 \ --no-allow-unauthenticated
This command uses buildpacks to transform your function source code into a production-ready container image.
Please note the following:
- the –source flag is used to tell Cloud Run to build the function into a runnable container based service
- the –function flag (new) is used to set the entrypoint of the new service to be the function signature you want to be invoked
- (optional) the –no-allow-unauthenticated to prevent your function from being publicly invokable
5. Test the function
When the deployment is complete, you will see the service URL. To invoke the function, you need to send an authenticated request with your identity token or the identity token of a principle that has the Cloud Run Invoker role, as shown below:
# get the Service URL SERVICE_URL="$(gcloud run services describe $SERVICE_NAME --region us-central1 --format 'value(status.url)')" # invoke the service curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL
6. Congratulations!
Congratulations for completing the codelab!
We recommend reviewing the documentation on Cloud Run functions
What we've covered
- How to deploy a Cloud Run function that is triggered by a HTTP request
7. Clean up
To avoid inadvertent charges, (for example, if the Cloud Run services are inadvertently invoked more times than your monthly Cloud Run invokement allocation in the free tier), you can either delete the Cloud Run or delete the project you created in Step 2.
To delete the Cloud Run function, go to the Cloud Run Cloud Console at https://console.cloud.google.com/run and delete the crf-http-codelab
service.
If you choose to delete the entire project, you can go to https://console.cloud.google.com/cloud-resource-manager, select the project you created in Step 2, and choose Delete. If you delete the project, you'll need to change projects in your Cloud SDK. You can view the list of all available projects by running gcloud projects list
.