ghchinoy
ghchinoy
Mar 31, 2017 · 4 min read

Inspired by Laurent Picard’s Building a serverless Python app in minutes with GCP, this is a similar set of steps, but uses a Go application and App Engine’s Flexible Environment.

The web app lists authors and uses the Google Books APIs to search for the author’s published works.

tl;dr

The few commands that’ll be slightly more elaborated, below.

PROJECT=my-project-name
# create a project
# cloud.google.com/sdk/gcloud/reference/alpha/projects/create
gcloud alpha projects create $PROJECT
# set the project as default
gcloud config set core/project $PROJECT
#
# write your code, test it locally
#
mkdir $GOPATH/src/github.com/you/$PROJECT
cd $GOPATH/src/github.com/you/$PROJECT
# create an app.yaml to define deployment
# ref cloud.google.com/appengine/docs/flexible/go/configuring-your-app-with-app-yaml
vi app.yaml
# deploy!
gcloud app deploy

Prerequisite: Install the Google Cloud SDK cli

Install the Google Cloud SDK command-line tool — this’ll be used throughout.

Create a Google Cloud Project

Using the gcloud alpha create command, create a new project. Note, this is an “alpha release” command, so it may change in the future. The non-alpha way is to use the web-based cloud console: https://console.cloud.google.com.

$ gcloud alpha projects create go-serverless
Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/go-serverless].
Waiting for [operations/pc.7509198304344852511] to finish…done.

List the existing projects

If this isn’t your first Google Cloud project, you may have multiple projects already, so here’s a useful command for checking what projects you have

$ gcloud projects list
PROJECT_ID NAME PROJECT_NUMBER
bespokemirrorapi bespokemirrorapi 811093430365
gdgnoco-fortune gdgnoco-fortune 861018601285
go-serverless go-serverless 49448245715

Set a default project

Subsequent gcloud commands will be associated with a project, so it’s useful to set the default project to our newly created project. Creating a new project doesn’t automatically set it to the default one.

List all the current project configs with gcloud config list and set a config item with gcloud set ….

Set the core/project property to go-serverless:


$ gcloud config set core/project go-serverless
Updated property [core/project].
$ gcloud config list
[compute]
zone = us-central1-c
[core]
account = ghchinoy@gmail.com
disable_usage_reporting = False
project = go-serverless
Your active configuration is: [default]

Create an app locally

  • Create a project directory
  • Create a deployment file app.yaml

Go projects, by convention, go into a folder under $GOPATH\src, typically associated with a repository. Here, I create a project using my github as a path and change to the created directory:


$ mkdir -p $GOPATH/src/github.com/ghchinoy/go-serverless
$ cd $GOPATH/src/github.com/ghchinoy/go-serverless

Add a new file called app.yaml — this defines a deployment on App Engine’s Flexible Environment (ref app.yaml for aeflex)


runtime: go
env: flex
api_version: 1
skip_files:
- README.md

Add code, snippet of main.go (see source for full code)

Test locally

Run the app locally; view at http://localhost:8080


$ go run main.go template.go
2017/03/30 15:55:21 booklist
127.0.0.1 — — [30/Mar/2017:15:55:24 -0600] “GET / HTTP/1.1” 200 1584 “” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3056.0 Safari/537.36”

Deploy

Make sure the project has been configured for billing, visit https://console.developers.google.com/project/go-serverless/settings in a browser, substituting the project name go-serverless with your own.


$ gcloud app deploy
You are creating an app for project [go-serverless].
WARNING: Creating an App Engine application for a project is irreversible and the region
cannot be changed. More information about regions is at
https://cloud.google.com/appengine/docs/locations.
Please choose the region where you want your App Engine application
located:
[1] europe-west (supports standard and flexible)
[2] us-east1 (supports standard and flexible)
[3] us-central (supports standard and flexible)
[4] asia-northeast1 (supports standard and flexible)
[5] cancel
Please enter your numeric choice: 3
Creating App Engine application in project [go-serverless] and region [us-central]….done.
You are about to deploy the following services:
— go-serverless/default/20170330t205543 (from [/Users/ghc/dev/go/src/github.com/ghchinoy/go-serverless/app.yaml])
Deploying to URL: [https://go-serverless.appspot.com]
Do you want to continue (Y/n)? YIf this is your first deployment, this may take a while…done.Beginning deployment of service [default]…
Building and pushing image for service [default]
Some files were skipped. Pass ` — verbosity=info` to see which ones
Updating service [default]…done.
Deployed service [default] to [https://go-serverless.appspot.com]
You can stream logs from the command line by running:
$ gcloud app logs tail -s default
To view your application in the web browser run:
$ gcloud app browse

Deployed!

https://go-serverless.appspot.com/

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

ghchinoy

Written by

ghchinoy

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade