Doing More With (Server)less

"P2, Recapping the Speedrun of Serverless,
With Google Cloud Console (UI)"

By Weiyuan Liu | Rakuten Viki

This is P2 of the speedrun

For the slides in P1, go here

Overview

  • Created new Google Cloud Platform project
  • Deployed CICD pipeline with Cloud Build
  • Assigned roles to service account for Cloud Build to perform deployment for App Engine and Cloud Run
  • Deployed Web application(s) to App Engine & Cloud Run

Let's examine the speedrun,
in 10 distinct steps

#1 - Create the Project
#2a - Enable App Engine
  • Assess the above from the Navgation Menu - "App Engine > Dashboard"
  • Click the Create Application button
#2b - Select App Engine Region
#2c - Set App Engine Core Configuration

See more on choosing the appropriate
App Engine environment here

#3 - Enable App Engine Admin API
  • Assess the above from the Navgation Menu - "APIs & Services > Dashboard"
  • Search for "App Engine Admin API" and click the Enable button
  • See here on why this API is required
#4 - Enable Cloud Run
  • Assess the above from the Navgation Menu - "Cloud Run"
  • Click the Start Using Cloud Run button
#5 - Enable Cloud Build (for CI/CD)
  • Assess the above from the Navgation Menu - "Cloud Build"
  • Click the Enable Cloud Build API button
#6a - Add Cloud Build configuration
  • Assess the above from the Navgation Menu - "Cloud Build > Triggers"
  • Click Connect Repository
#6b - Connect Repository
  • Select the correct code repository product that you are using
  • Click Continue once you're ready
#6c - Proceed to add build trigger
  • Click Add Trigger
#6d - Connect Repository
  • Select the "Build configuration" setting as seen above
  • Add branch regex (optional) if you only want to target a certain group of branches
  • Click Create Trigger
#7 - Add IAM roles to Cloud Build Service Account
  • Assess the above from the Navgation Menu - "IAM & admin > IAM"
  • Add the above IAM roles to ${id}@cloudbuild.gserviceaccount.com. This is a service account that is the underlying identity when running Cloud Build.
  • View the privileges offered under the above roles from Navigation Menu - "IAM & admin > Roles"
#8 - Add App Engine Configuration to codebase
runtime: nodejs10
instance_class: B1
manual_scaling:
  instances: 1

handlers:
- url: /static
  static_dir: static
- url: /public
  static_dir: public
- url: /client-assets
  static_dir: client-assets                

Add the above in the root dir of project as app.yaml

App Engine configuration differs between environment (Standard VS Flexible). Do check out the standard configuration here and flexible configuration here .

See code base that I used here .

#9 - Add Cloud Build Configuration to codebase
timeout: '600s'
steps:
- id: init
  waitFor: ['-']
  name: 'gcr.io/cloud-builders/npm'
  args: ['install']

- id: frontendTest
  waitFor: ['init']
  name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'f-test']

- id: backendTest
  waitFor: ['init']
  name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'b-test']

- id: buildAssets
  waitFor: ['frontendTest', 'backendTest']
  name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'build']

- id: appEngineDeploy
  waitFor: ['buildAssets']
  name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy', '-q', '--project', '$PROJECT_ID']

- id: cloudRunDeploy
  waitFor: ['frontendTest', 'backendTest']
  name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'sh'
  args:
    - -c
    - |
      gcloud builds submit --tag gcr.io/$PROJECT_ID/webapp:1 -q
      gcloud beta run deploy --image gcr.io/$PROJECT_ID/webapp:1 -q --allow-unauthenticated --platform="managed" --region="us-central1" webapp
                

Add the above in the root dir of project as cloudbuild.yaml

The above runs the application's unit tests, before building the assets and deploying the applications to Cloud Run and App Engine. See here for more details on Cloud Build's configuration.

#10 - Commit the configurations

Committing the code above will trigger Cloud Build to starting building based on the trigger from #6

Viola! Wait for a few minutes before your web applications are deployed


Note that you might want to omit the usage of Cloud Run if you're using App Engine, or vice versa, as you only need to serve a web application from one of them. Amend from the Cloud Build steps to select the tool to use for your web application.

Deploying using both tools during the speedrun was part of the challenge to show how easy it was to get started with "serverless" XD

Thanks for listening!


Linkedin / Github : bit.ly/weiyuan