1. Home
  2. Training Library
  3. Google Cloud Platform
  4. Courses
  5. Building and Testing Applications on Google Cloud Platform

Emulating GCP Services for Local Application Development

play-arrow
Start course
Overview
DifficultyIntermediate
Duration41m
Students176
Ratings
4.2/5
starstarstarstarstar-border

Description

Google Cloud Platform (GCP) is a powerful platform that brings the flexibility and reliability of Google’s infrastructure to your projects. As a leader in AI, machine learning, and networking, GCP has a suite of tools and services for developers to use for almost any circumstance. Before using them, it’s good to learn the ins and outs of GCP and the most effective patterns for software development. The Professional Cloud Developer exam, as well as the industry at large, has been transformed by Continuous Integration (CI) and Continuous Deployment (CD), which enable developers to deliver code safely and securely into production once properly setup. This course will cover the Google best practices for setting up a CI/CD pipeline on GCP.

For support, queries or feedback relating to this course, please contact us at support@cloudacademy.com.

Learning Objectives

  • Learn GCP core developer services
  • Create a CI/CD pipeline with Google Cloud Build
  • Test and deploy a containerized application

Intended Audience

Developers who are studying for the Google Cloud Professional Cloud Developer Certification exam.

Prerequisites

To get the most out of this course, you should be fluent in at least one programming language and have some experience with Docker and Kubernetes.

Transcript

In this section, we're going to go over the GCP emulators provided by the Google Cloud SDK. Emulators are extremely useful for local or offline development because they allow you to test code without affecting data in your cloud environment. There are currently four emulators available in the Google Cloud SDK: BigTable, Datastore, Firestore, and Cloud Pub/Sub.

Additionally, there's also a way to run Cloud Build locally. We won't cover that in this section but it will be very important for the next one, so keep that in mind.

Each of the emulators is a little different from the others because they're all mocking up different services, but they all require a few common things: a GCP service account and source code that utilizes the G-Cloud SDK.

The CLI commands are not compatible with emulators. So for our demo we'll be using some simple Python code provided by Google. If you're interested in following along, I'll put a link to the repository at the bottom of the page. And the demo for this section will show you how to install the prerequisite packages to use Google Cloud SDK, set up a service account, and start an emulator with the sample code.

Let's get started. In this demo, we're going to go over how to install the Google Cloud SDK, how to set up a service account, and then how to start an emulator using that SDK. So first things first. To install the Google Cloud SDK I'm using a WSL Ubuntu 18.04 instance here. And the first thing I want to do is add the distribution URI as a package source. So once that's done, I want to make sure that I have the API transport HTTPS package installed. And next, I wanna make sure I import the Google Cloud public key.

Now that all that's done, I can run a sudo apt-get update and sudo apt-get install to install the Google Cloud SDK. Okay, now that the SDK is installed, we can get started with it by running G-Cloud in it. This is where we're gonna be asked to log in to make sure that we are the account that we're claiming to be.

Now, the project I wanna select for this is cloudacademy-gcp. So let's enter that. Alright, excellent my G-Cloud SDK is set up. I can verify that again by running G-cloud version, and the next thing I want to do is set up a service account. Using the G-cloud command line I'm going to enter gcloud iam service-accounts create and that ca-service-account is the name of the account I wanna create in this case.

Once that's done I have to go ahead and bind that to the project I'm using. Alright, great. Now that that's done, I can create a credentials.json file that I can then export to my local system. Now, if I run an LS there's my credential.json. Let's go ahead and also see where I am. I know it's not best practice to run in-route but it's just for the demo today so it should be fine.

Now, the last thing I wanna do to finish setting up my service account is set an environment variable to point to this credentials file. And the variable in this case is gonna be called GOOGLE_APPLICATION_CREDENTIALS. And let's go ahead and set that. And now anything I run using this terminal is going to use those credentials for that service account. Alright, now that we are all set there, let's go ahead and start using our emulator. At the time of this recording the G-Cloud emulators are still a beta feature. 

So we're gonna start with the Pub/Sub emulator because that one is probably going to be the most used, especially if you take some of the other Google certifications like Data Engineer. Alright, now that our emulator has been installed, let's go ahead and start it. Great. Now our emulator is running on port 8085. We're gonna want to remember that later because we're going to export that into a variable. Let's open up another tab on this same WSL instance and continue with the demo.

Alright, I've got my second WSL tab open here. This is just another terminal window into the same instance. And the next thing I'm gonna do is export an environment variable called PUBSUB_EMULATOR_HOST and I'm gonna point that towards localhost on the port that was given to us in the last step.

The next thing I'm gonna do is export another environment variable called PUBSUB_PROJECT_ID. And this just has the name of our project on it. Great, now I'm all set up for my environment. Let's take a look at the code.

Alright and I'm back here on GitHub. This is the GoogleCloudPlatform organization under the python-docs-samples repo. These are a bunch of the code samples that use the Google Cloud SDK to interact with GCP services. Alright, I'm back here on the terminal. I'm going to go ahead and clone the repo. Alright, and you see I have the python-docs- samples repo there. Let's go ahead and go into it and specifically because I'm testing Pub/Sub, the section I want to go into is /pubsub/cloud-client. Alright, and from here to use the Pub/Sub code, I'm going to go ahead and run a pip install.

Okay, now that my requirements are installed, let's go ahead and create a new topic. Since I've already got my PUBSUB_PROJECT_ID defined as an environment variable, all I need to do is name the topic, which in this case is going to be test topic.

Now that my topic's created, let's go ahead and create a subscription to the topic. Same kind of syntax here but my topic is still test-topic and my subscription is test-subscription. Now we're gonna go ahead and publish a message to the test topic. And finally, we'll receive the message using that test subscription.

Alright, there you have it. Let's go ahead and check out our GCP console. I'm here in GCP under Pub/Sub and you'll see I don't have any topics or subscriptions created. And that's because I've been using the local emulator. So what happens if we want to create a topic or subscription while using the emulator? How can we do that? Let's go back to our terminal and find out.

I'm back here in my WSL terminal and I want to stop using the emulator and start using my GCP account. So to do that, all I have to do is unset the emulator host variable. Alright, now that that's done, I'm going to run through the same process I just did with the Publisher/Subscriber and the test topic and test subscription.

Now, that that's run, let's check our GCP console. You see now we do have a test topic here in pub/sub because we unset the emulator host variable. And that tells the SDK that we don't wanna use an emulator anymore, we wanna go straight up to our GCP project.

Alright, that's it for this lesson. Thanks for watching.

About the Author
Students177
Courses1

Chris Blackden started his first IT job at the age of fifteen and has been doing it ever since. He’s held roles from Hardware Technician to Senior Software Engineer, and is currently working as a DevOps Engineer in a healthcare research organization. Some of his passions outside of work include cooking and eating spicy food, and old-school kung fu movies.