Vendure eCommerce on Google Cloud Run

Martijn van de Brug
3 min readOct 27, 2020

For my company Pinelab.studio I decided to build a SaaS eCommerce platform, to hold multiple webshops. After doing some research I stumbled upon Vendure.io, a headless eCommerce framework written in NodeJS with a GraphQL API. What more could you want?

For scalability and cost reduction I am running Vendure on Google Cloud Run, here is how to do it!

⚠️There is an updated version of this tutorial here: https://www.vendure.io/blog/2021/11/serverless-e-commerce-vendure-on-google-cloud-run/

Minimal effort

This blog describes the minimal steps to deploy Vendure to Cloud Run. More improvements are possible. I have described them at the end of this blog.

What needs to be done

  • Vendure uses local file system to store assets. Cloud Run doesn’t have persistent local file system
  • Listen to the correct Cloud Run port
  • A MySQL database for Vendure
  • Dockerize the application
  • Deploy to Google Cloud Run

Google Storage

We need to make Vendure use Google Storage instead of local file system. Luckily it’s very easy to implement your own AssetStorageStrategy.

This file shows you how to implement Google Storage.

Configure port

Make sure the Vendure app listens to the correct port on Cloud Run. Add this to you vendure-config.ts :

apiOptions: {
port: process.env.PORT || 3000
}

This will listen to the PORT variable if defined by Google, or port 3000 when running locally.

MySQL database

For the MySQL database I use CloudSQL, which is Google’s managed MySQL solution.

  • Create a database and user. Save the host, username, password and database name
  • Set the host, username, password and databasename in vendure-config.ts :
dbConnectionOptions: {
type: ‘mysql’,
synchronize: true,
logging: false,
username: DATABASE_USER,
password: DATABASE_PASSWORD,
host: DATABASE_HOST,
database: DATABASE_NAME,
}

Remember to set synchronize: false after the initial startup, to prevent possible data loss later.

Dockerize the application

We need to Dockerize our Vendure app to deploy it to Cloud Run.

First, create a Dockerfile :

# https://github.com/vendure-ecommerce/vendure/issues/458
FROM
node:12

WORKDIR /
usr/src/app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY
package*.json ./

RUN
npm ci --only=production

# Bundle app source
COPY
. .

# Run the web service on container startup.
CMD
[ "npm", "start"]

Run the following commands in the root folder of your project to build the Docker container and upload it to Google’s container registry (GCR):

docker build -t eu.gcr.io/your-gcloud-project/your-app .
# Configure docker to use Google authentication
gcloud auth configure-docker -q
docker
push eu.gcr.io/your-gcloud-project/your-app

Deploy

Deploy the created container to Cloud Run:

gcloud run deploy shops-test \
--quiet \
--image "eu.gcr.io/your-gcloud-project/your-app:latest" \
--region "europe-west1" \
--platform "managed" \
--allow-unauthenticated \
--memory=1G

Improvements

To make you Vendure setup even more production worthy:

For the complete project: https://github.com/martijnvdbrug/shops

Am I missing something? let me know!

Pinelab specializes in static websites, webshops, and backend development using NodeJS and GraphQL. Visit Pinelab.studio or leave a comment here.

--

--