Tutorials
Serverless Redis on Google Cloud Functions
GitHub Repository
You can find the project source code on GitHub.
Google Cloud Functions is the second most popular serverless execution platform. Similar to AWS Lambda it is stateless, namely you need to access external resources to read or write your applications state. In this post, we will introduce Redis as a database for your Google Cloud functions.
This tutorial shows how to build a serverless API with Redis on Google Cloud Functions. The API will simply count the views and show it in JSON format.
Prerequisites
- Create a Google Cloud Project.
- Enable billing for your project.
- Enable Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.
Database Setup
Create a Redis database using Upstash Console or Upstash CLI. Copy UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
for the next steps.
Counter Function Setup & Deploy
- Go to Cloud Functions in Google Cloud Console.
- Click Create Function.
- Setup Basics and Trigger Configuration like below:
- Using your
UPSTASH_REDIS_REST_URL
andUPSTASH_REDIS_REST_TOKEN
, setup Runtime environment variables under Runtime, build, connections and privacy settings like below. - Click Next.
- Set Entry point to
counter
. - Update
index.js
index.js
- Update
package.json
to include@upstash/redis
.
package.json
- Click Deploy.
- Visit the given URL.
Was this page helpful?