So you want to start using Google Cloud

Mack Davenport
7 min readMar 12, 2019

--

I’m not the first person to write a how-to on this subject, and I won’t be the last. Cloud infrastructure is the new wave and if you’re not currently taking advantage of it, you may be left behind. There’s tons of services out there that will allow you to do what we’re about to go through relatively painlessly, but you may soon find yourself behind a paywall or limited to the basic features offered. Here’s to you, Heroku.

Some may ask, why not AWS? Isn’t it the hottest new thing full of wonder and magic? Because it’s a pain. Basic maneuvers require significant domain knowledge, something about CloudFormation templates, and you can quickly find yourself being billed for a lot more than you expected if you’re not certain about what a button does. I’m not knocking AWS by any means for its capability and utility, but it could be a little simpler to get started with small projects.

These problems are exactly what led me to start using the Google Cloud Platform in the first place. GCP has its own shortcomings, but it’s excellent for throwing up a website that you can be assured will be able to scale. Google has some great documentation on this how-to, but it’s not always the easiest to follow. If anyone is struggling, here’s how I get started quickly with Node.

To get started, you’ll need a few things.

  1. A GCP account
  2. Google Cloud SDK
  3. Node if you want to run this locally
  4. An IDE and a terminal (I’m going to use VSCode)
  5. I’ve provided a very simple github project to pair with this tutorial
Awesome introductory offer

Once you’ve made your account on Google Cloud, you’ll have access to GCP and the wealth of options available to you. For now, we’ll be staying within the App Engine. Go ahead and navigate to your console.

Google occasionally changes the workflow and requirements of accounts and what you must do to get started. While creating an account for purposes of creating this blog, I was required to create an organization with billing information. This involved adding a meta tag to a website I was already hosting to allow google to verify my ownership of a domain. If there are additional steps, I apologize. Feel free to let me know in the comments, or there’s plenty of help to be had with the docs.

Google Cloud SDK (gcloud init)

Once you’ve cloned the github project, open a command line instance and type gcloud init. If this is your first time, you’ll need to create a new configuration. This will require you to authenticate with your google account you created.

Then you’ll create a new project and give it a name. I simply named mine up-and-running for this example.

After successful init

After successfully completing that step, you should get the above confirmation, letting you know you’ve succeeded. If there are any issues here, it’s probably related to your installation of the SDK. More often than not, it lies between the Python 2.7 installation and the its PATH variable on your machine.

https://console.cloud.google.com/

You may need to refresh, but at this point, your console should reflect your new project. You can access it from the drop down shown above. Sometimes it’s not immediate. This is a good time to go make some coffee.

https://console.developers.google.com/apis/api/cloudbuild.googleapis.com/overview

We need to enable the Cloud Build API to allow GCP to bundle our app and serve it. In your Google Cloud Console dashboard, make sure your new project is selected. From the navigation pane, find APIs & Services, go to the library, and search for Cloud Build. Unless you deploy upwards of 240 times in a month, you’re not going to cross the free tier threshold, so don’t worry about any price notifications.

gcloud app deploy

Once we’ve enabled the Cloud Build API, it’s time to deploy our application. If you’re using the code I provided earlier, make sure you’re at the root directory and just type gcloud app deploy. This is a powerful command that you can stack on a lot of parameters to customize your deployment process. For now, we’re going to let it guide us.

Pick the region nearest to you, or wherever you want to host your project. Any prompts that pop up should just confirm that you’re pushing to the project you just created. Go ahead and confirm if correct to deploy the application to your project.

App is deployed!

If you had no issues, you should see something like the above screen. Wait a moment, as the actual website will take 10–15 seconds to reflect any CLI changes. Then type gcloud app browse to automatically open a link to your project’s page.

This is what you should see!

You should see this screen which is what I’ve provided in the example code.

app.get(“/”, (req, res) => {res.status(200).send(“Up and running”)});

If you did, congratulations! You’re a cloud developer!

But seriously, you’ve just pushed a website that you created to the cloud on a platform that can scale reliably and is used by companies like Philips, Coca-Cola, and HTC.

You’ve successfully deployed your first application to Google Cloud. From here you can continue to build out this node application as you see fit, adding html pages or new endpoints.

500 error

Ugh, errors. Why can’t it just work?

Anyhow, welcome. You’ve had some kind of problem. You see this screen, you don’t even know where to start. Get acquainted to this screen. I’m not going to cover every problem to ever come up that causes you to get this error, but I’ll help by giving you the tools to get some insight. Head over to your google cloud console.

Versions

Make sure the correct project is selected and from your navigation bar, go to App Engine and then into Versions.

You should see a screen like the one above. On the diagnose drop down, head into logs.

From the same menu, you can access the cloud debugger as well as see your source code. To use the debugger, you’ll need to enable the cloud debugger API. This debugger does not stop the application and let you step over it, but rather takes snapshots when the code is executed that you can inspect. But we’re just looking at logs for this example.

example logs

While you may need to zoom in on the image above, I’ve created an example problem. One of my stack trace errors provides me with the message

textPayload: “Error: Cannot find module ‘/srv/server.js’

So when this application is attempting to launch, it’s looking for a file called server.js. But in the code I was working with, I didn’t have a server.js as my server file, I called it index.js and left it at the root.

I could rename index to server and redeploy, but why let not solve it the right way? To resolve this problem, it’s simple. In package.json, we just need to specify an npm start script.

“scripts”: { “start”: “node index.js” }

If we ensure we have a start script that calls whatever we need to do to fully deploy our application, we resolve the problem and don’t gridlock ourselves into having a server.js file at the root driving the application. In this case, we just need to call node index.js which would also launch the application locally.

Once that’s fixed, redeploy your application using gcloud app deploy. Again, give it a moment before trying to access the website to allow your changes to be made.

If you continue to have issues, go back through the logs and check for issues. Feel free to drop a comment if you are having difficulties and I’d be happy to help.

Feel free to check out the second part of this tutorial available here.

--

--