<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Martijn</title>
    <description>The latest articles on DEV Community by Martijn (@martijnvdbrug).</description>
    <link>https://dev.to/martijnvdbrug</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F765278%2F725bf227-5849-4091-b7e9-a2d9b8d18c18.jpeg</url>
      <title>DEV Community: Martijn</title>
      <link>https://dev.to/martijnvdbrug</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/martijnvdbrug"/>
    <language>en</language>
    <item>
      <title>Serverless e-commerce: Vendure on Google Cloud Run</title>
      <dc:creator>Martijn</dc:creator>
      <pubDate>Tue, 30 Nov 2021 17:20:38 +0000</pubDate>
      <link>https://dev.to/martijnvdbrug/serverless-e-commerce-vendure-on-google-cloud-run-397d</link>
      <guid>https://dev.to/martijnvdbrug/serverless-e-commerce-vendure-on-google-cloud-run-397d</guid>
      <description>&lt;p&gt;Google’s Cloud Run is a scalable, containerized, fully managed serverless platform. It’s cheap and handles infrastructure and scaling for us. Sounds perfect to run a headless Vendure instance on, right?&lt;/p&gt;

&lt;p&gt;Here is what we need to do. Don’t worry, for most of these you can use a plugin:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a database&lt;/li&gt;
&lt;li&gt;Use Cloud Storage for assets&lt;/li&gt;
&lt;li&gt;Use Cloud Tasks to process worker jobs&lt;/li&gt;
&lt;li&gt;Dockerize Vendure&lt;/li&gt;
&lt;li&gt;Build image and deploy to Google Cloud Run&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We assume you already have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Vendure project set up locally. If not, checkout &lt;a href="https://www.vendure.io/docs/getting-started/"&gt;these steps&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;gcloud&lt;/code&gt; cli &lt;a href="https://cloud.google.com/sdk/docs/install"&gt;installed&lt;/a&gt; and &lt;a href="https://cloud.google.com/sdk/docs/initializing"&gt;authorized&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Create a database for Vendure
&lt;/h2&gt;

&lt;p&gt;Vendure requires a database to store its products, orders, customers etc. In this tutorial, we will use Google’s Cloud SQL: a managed database platform.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://cloud.google.com/sql/docs/mysql/quickstart"&gt;Enable Cloud SQL&lt;/a&gt; in your dashboard and create a database.&lt;/li&gt;
&lt;li&gt;For this example, 1vcpu and 10 GB SSD is sufficient. PostgreSQL or MySQL is up to you. Make sure to write down the password you entered.&lt;/li&gt;
&lt;li&gt;Create a database named &lt;code&gt;vendure&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Click on your instance, and go to connection &amp;gt; add network. Create a new network with IP range &lt;code&gt;0.0.0.0/0&lt;/code&gt;. Careful with production environments, this will make your SQL instance publicly available!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Configure Vendure to use the new database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vendure-config.ts&lt;/span&gt;
&lt;span class="nx"&gt;dbConnectionOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mysql&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;synchronize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Disable this after first startup&lt;/span&gt;
    &lt;span class="nx"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Don't use this in production&lt;/span&gt;
    &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-password&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;192.56.298.3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// The public IP of your SQL instance&lt;/span&gt;
    &lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vendure&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setup Cloud Storage
&lt;/h2&gt;

&lt;p&gt;Cloud run instances are stateless, which also means you shouldn’t use its local file system. Instead, we will use Google Cloud Storage.&lt;/p&gt;

&lt;p&gt;Create a storage bucket and &lt;a href="https://cloud.google.com/storage/docs/access-control/making-data-public"&gt;make it publicly accessible&lt;/a&gt;, &lt;br&gt;
so that the images can be used in a storefront. You can use &lt;a href="https://www.npmjs.com/package/vendure-plugin-google-storage-assets"&gt;this plugin&lt;/a&gt;&lt;br&gt;
to connect Vendure to the bucket.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vendure-config.ts&lt;/span&gt;
&lt;span class="nx"&gt;AssetServerPlugin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;storageStrategyFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;GoogleStorageStrategy&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;bucketName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-bucket-name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="na"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;assets&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;assetUploadDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/tmp/vendure/assets&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can implement the &lt;a href="https://www.vendure.io/docs/typescript-api/assets/asset-storage-strategy/"&gt;AssetStorageStrategy&lt;/a&gt; yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup Cloud Tasks
&lt;/h2&gt;

&lt;p&gt;Cloud run allows no processing outside request context. As soon as your server returned a response, &lt;br&gt;
there is no guarantee that any leftover processes will be finished. Because of this, we need some way to wrap the Vendure worker Jobs in a request. &lt;br&gt;
We can do that with &lt;a href="https://www.npmjs.com/package/vendure-plugin-google-cloud-tasks"&gt;this plugin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This plugin puts jobs in a queue and Cloud Tasks posts the messages back to a publicly available endpoint in your Vendure application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vendure-config.ts&lt;/span&gt;
&lt;span class="nx"&gt;CloudTasksPlugin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;taskHandlerHost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://your-instance-sds34vbs-ew.a.run.app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// This endpoint needs to be accesible by Google Cloud Tasks&lt;/span&gt;
  &lt;span class="na"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-projectId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;europe-west1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;authSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some-secret&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For simplicity, we will start the worker in the same instance as the application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// index.ts&lt;/span&gt;
&lt;span class="nx"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JobQueueService&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This is not recommended for production!&lt;/strong&gt; &lt;a href="https://www.vendure.io/docs/developer-guide/vendure-worker/"&gt;Read more about the worker here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you could implement the &lt;a href="https://www.vendure.io/docs/typescript-api/job-queue/job-queue-strategy/"&gt;JobQueueStrategy&lt;/a&gt; yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dockerize Vendure
&lt;/h2&gt;

&lt;p&gt;Cloud Run requires Vendure to  be Dockerized. We can simply create a Dockerfile in the root of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:16
WORKDIR /usr/src/app
COPY . .
RUN yarn install --production
RUN yarn build
CMD [ "node", "dist/index.js" ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploy
&lt;/h2&gt;

&lt;p&gt;First we need to build an image and push it to Google's container registry:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.docker.com/engine/install/"&gt;Install docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Execute these commands in the root of your project:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; eu.gcr.io/your-projectId/vendure &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="c"&gt;# Configure docker to use Google authentication&lt;/span&gt;
gcloud auth configure-docker &lt;span class="nt"&gt;-q&lt;/span&gt;
docker push eu.gcr.io/your-projectId/vendure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all that's left is deploying the image to Google Cloud Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# This sets all your secrets from a .env file in a variable, so we can pass it to Cloud Run&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ENV_VARS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;paste&lt;/span&gt; &lt;span class="nt"&gt;-sd&lt;/span&gt;, .env&lt;span class="si"&gt;)&lt;/span&gt;
gcloud run deploy shops-test &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--quiet&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--image&lt;/span&gt; &lt;span class="s2"&gt;"eu.gcr.io/your-projectId/vendure:latest"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--region&lt;/span&gt; &lt;span class="s2"&gt;"europe-west1"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--platform&lt;/span&gt; &lt;span class="s2"&gt;"managed"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--allow-unauthenticated&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--memory&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1G &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--project&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-projectId &lt;span class="se"&gt;\&lt;/span&gt;
            &lt;span class="nt"&gt;--set-env-vars&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$ENV_VARS&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to &lt;a href="https://console.cloud.google.com/run"&gt;https://console.cloud.google.com/run&lt;/a&gt; to view your public URL. It should look something like &lt;code&gt;https://your-instance-sds34vbs-ew.a.run.app&lt;/code&gt;. &lt;br&gt;
Make sure to also set this URL as &lt;code&gt;taskHandlerHost&lt;/code&gt; in the Cloud Tasks plugin if you haven't already.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;code&gt;https://your-instance-sds34vbs-ew.a.run.app/admin&lt;/code&gt; and login with your super admin user&lt;/li&gt;
&lt;li&gt;Go to products and create a new product&lt;/li&gt;
&lt;li&gt;Add an image to the product&lt;/li&gt;
&lt;li&gt;Save the product and go back to the overview&lt;/li&gt;
&lt;li&gt;Your product should appear in the product overview in the admin&lt;/li&gt;
&lt;li&gt;Congratulations, this means everything works as expected!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Make sure you set &lt;code&gt;synchronize: false&lt;/code&gt; after the database has been populated!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some optional improvements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.vendure.io/docs/developer-guide/vendure-worker/"&gt;Set up a separate worker instance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Restrict access to your database to specific IP's.&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://cloud.google.com/sql/docs/mysql/connect-run#configuring"&gt;Unix sockets&lt;/a&gt; for database access&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thats it!&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>serverless</category>
      <category>typescript</category>
      <category>graphql</category>
    </item>
  </channel>
</rss>
