Deploying Go with GAE
A simple tutorial of deployment of Go application using GAE
I’ve introduced how to deploy the Go application using GKE and Spinnaker in another post. This post will show you how to deploy it with GAE on Standard.
Example repo
You can view the example code at:
Set up Go app
First off, we prepare a very simple Go app just responding to Hello Go App!
Create a main.go
:
Run it:
go run main.go
See it at http://localhost:8080
:
Set up Cloud SDK
To quickly start off GCP, we use SDK provided in Google.
Go to an install page and follow the instruction to set it up:
After installation, let’s initialize gcloud account:
gcloud init
After that, you can see the configuration list:
$ gcloud config list [core]
account = xxx@gmail.com
disable_usage_reporting = TrueYour active configuration is: [default]
Make sure your account is activated:
$ gcloud auth list
Credentialed Accounts
ACTIVE ACCOUNT
* xxx@gmail.comTo set the active account, run:
$ gcloud config set account `ACCOUNT`
Create a project
Create a project with an id which should be unique. In this post we use golang-deploy-gae
but you can name it whatever you want.
gcloud projects create golang-deploy-gae
Initialize App Engine
Initialize GAE:
gcloud app create
Select your regions where you want App Engine application located.
Please choose the region where you want your App Engine applicationlocated:[1] asia-east2 (supports standard and flexible)[2] asia-northeast1 (supports standard and flexible)[3] asia-northeast2 (supports standard and flexible)[4] asia-south1 (supports standard and flexible)[5] australia-southeast1 (supports standard and flexible)[6] europe-west (supports standard and flexible)[7] europe-west2 (supports standard and flexible)[8] europe-west3 (supports standard and flexible)[9] europe-west6 (supports standard and flexible)[10] northamerica-northeast1 (supports standard and flexible)[11] southamerica-east1 (supports standard and flexible)[12] us-central (supports standard and flexible)[13] us-east1 (supports standard and flexible)[14] us-east4 (supports standard and flexible)[15] us-west2 (supports standard and flexible)
And you cannot change an app’s region once it has been set.
Enable a billing account
To use some Google API, we need to enable billing settings.
If you haven’t had billing account, set it up first:
Make sure of your billing account:
$ gcloud beta billing accounts list
ACCOUNT_ID NAME OPEN MASTER_ACCOUNT_ID
xxxxx-xxxxx-xxxxx xxxx True
Next, hook your project up to your billing account:
gcloud beta billing projects link golang-deploy-gae --billing-account {ACCOUNT_ID}
ACCOUNT_ID
is supposed to be the billing account that you’ve got above.
You’ll get like this:
billingAccountName: billingAccounts/xxxxx-xxxxx-xxxxx
billingEnabled: true
name: projects/golang-deploy-gae/billingInfo
projectId: golang-deploy-gae
Install App Engine Extension for Go
Install the gcloud command that includes the GAE extension for Go:
gcloud components install app-engine-go
Set up a configuration file
Configure the App engine app’s settings by creating the app.yaml
file. The file should contain at least runtime: go
. You can see the details on docs.
Create a app.yaml
in the root directory:
# app.yamlruntime: go113
It indicates that we’re using the Go 1.3 runtime. If you would like to use Go 1.12, replace it with go112
.
Deploying an app to GAE
Let’s deploy the app on GAE by running the command:
gcloud app deploy
After that, view the page by running the command:
gcloud app browse
Versioning app
Now that we’ve deployed the app on GAE and it can be accessed at https://[YOUR_PROJECT_ID].appspot.com
. In an actual case, you probably need several environments such as staging
environment, testing environment (QA) and production environment.
Let’s assume that we need staging
environment on GAE.
Create staging
version by running the command:
gcloud app deploy --version staging --no-promote -q
—-version
option will create a specific versioning environment at https://[VERSION]-dot-[YOUR_PROJECT_ID].appspot.com
and it will receive the all traffic immediately at https://[YOUR_PROJECT_ID].appspot.com
as staging environment. We don’t want it to be switched immediately so add --no-promote
which will disable the behavior. More options can be seen in the docs.
You can view the staging
at GCP console:
And you also see the progress bar at Traffic Allocation
column. Now that another version 20191206t092042
has 100%
which means that it receives all traffic as https://[YOUR_PROJECT_ID].appspot.com
.
Let’s access https://staging-dot-[YOUR_PROJECT_ID].appspot.com
and you’ll see Hello Go App!
.
If you want to change the traffic, you can switch it by running the command:
gcloud app services set-traffic default --splits staging=1 -q
And you’ll see that it switched:
Conclusion
We’ve done with deploying the Go app on GAE in this post. GAE provides two environments, Standard
and Flexible
and you can choose simultaneously to use both environments as microservices architecture. The differences are in docs. I think you might want to start off Standard environment and eventually when you need it to be more flexible as no vendor lock-in, you can set it up as Flexible or split it into Standard and Flexible as microservices.
Hope you’ll find it useful.
A full final codebase is at: