Prerequisites:
- A GCP account for Google Cloud functions.
- Install Google Cloud SDK.
- An Upstash account for Serverless Redis.
Step 1: Init the Project
- Create a folder, then run
npm init
inside the folder.
Step 2: Install a Redis Client
Our only dependency is redis client. Install go-redis vianpm install ioredis
Step 3: Create a Redis Database
Create a Redis database from Upstash console. Select the GCP US-Central-1 as the region. Free tier should be enough. It is pretty straight forward but if you need help, check getting started guide. In the database details page, click the Connect button. You will need the endpoint and password in the next step.Step 4: The function Code
Create index.js as below:This example uses ioredis, you can copy the connection string from the
Node tab in the console.
Step 5: Deployment
Now we are ready to deploy our API. Deploy via: