How to create a virtual machine with R on Google Cloud

Many times our computer is not powerful enough to undertake the tasks we need to do. Besides, you cannot always be taking your computer with you. If you have ever been yourself in a situation like this, you will be very interested in learning how to create a virtual machine with R in Google Cloud. You can even have one for free! It sounds great, right? Let’s learn how to do it!

Virtual Machines on Google Cloud

In our case we will use the virtual machines on Google Cloud for two reasons:

  • Mark Edmonson has developed several awesome and super useful libraries to work with Google Cloud in R. In fact, we have already used some of these libraries when learning how to automate an R script on Google Cloud.
  • Google Cloud offers a free virtual machine every month. Despite this virtual machine is not very powerful (1CPU and 0,6Gb os memory) it can be enough for some tasks or just for learning how it works. Anyway, you can always pick a more powerful virtual machine. For example, you can get a virtual with7,5Gb of Ram for 16€/mont (interruptible) or 53€/month uninterruptible.
Features of free virtual machines from Google Cloud.
Features of free virtual machines from Google Cloud.

Previous steps to create a virtual machine with R in Google Cloud

Activating App Engine API

Before creating our virtual machine we have to configure some things. First, we have to create a Google Cloud account. If you don’t have one, on this post I explain you how you can create one and some good practices when you do so.

Once you have created your Google Cloud account you have to enable Compute Engine API. Yo can do it from this link. You just have to press the ‘Enable’ button.

How to enable compute engine API
How to enable compute engine API

Creating a service account

Once you have enable the App Engine’s API, you need to create a service account. A service account enables you is an account that only has permissions to Google Cloud services that we specify. We will use this service account to create our virtual machine with R in Google Cloud using R functions.

To create a service account, we have to go to Service Accounts within Iam. On the first step your need to give your service account a name and a description. I recommend you to give a clear description and name, so that you know what they are on the future.

Creating a service account. Defining the name and description.
Creating a service account. Defining the name and description.

On the next step, we need to define which service and with which role will be able to access this service account. We just two accesses: Compute Admin and Service Account user. If you don’t enable these roles, later on, the API will throw you an error.

Creating a service account. Defining the necessary roles.
Creating a service account. Defining the necessary roles.

The third step is not necessary unless you manage several users on the same Google Cloud account. Thus, you can skip this step.

After doing that you will have just created your service account. Now you will have to access it by clicking on its email.

Once on the service account page, you will need to create a JSON key for the service account you have just created. To do this simply click on “Add Key”. By default, the key will be marked in JSON format, so you just have to click on create.

Note: anyone with this key will be able to act on your behalf with the roles and services that you have determined. Thus, it is vital that you save this key in a secure place. As said before, we will use this key to enable googleComputeEngineR library to create the virtual machine in Google Cloud from R.

Download service account key
Download service account key

Once you have downloaded your key, move it to the root of your R project. With that, we are ready to create a virtual machine in Google Cloud with R, from R. Let’s do it!

How to create a virtual machine with R in Google Cloud

Before creating a virtual machine with R you need to consider two things: the type of virtual machine that you want and it’s location. These two parameters will determine the performance and cost of the virtual machine.

Types of virtual machines

Each type of virtual machine in Google Cloud has different characteristics and name. For example, a n class virtual machine can have from 3,75Gb of RAM to 360Gb of RAM (link)

The most typical virtual machines are n1-standard-1 and n1-standard-2, with 3,75Gb and 7,5Gb of RAM each. However, in our case, we will pick a f1-micro virtual machine, which is the one Google Cloud offers for free. Obviously, it is a very limited virtual machine with just 0.6 Gb of RAM, but it will be enough.

Besides, if you want to change the type of virtual machine that you want to build, you will just have to change one parameter.

Once we know what types of virtual machines there are, let’s see the importance of its location.

Location of the virtual machine

Google has servers at many different places and that’s why we can build our virtual machines at different places of the globe. Normally we will always choose the closest location that Google offers as this will speed up its response time.

However, Google Cloud only offers free virtual machines on the United States (except for North Virginia). That’s why in my case I have set the location of my virtual machine to be Iowa (us-central1-b). Anyway, here you can check all the possible locations of your virtual machine.

Having that clear, let’s create our virtual machine!

Creating our virtual machine with R

In order to create our on virtual machine we simply have to:

  1. Define three environment variables that refer to, the path to the service key, the location of the virtual machine, and the project in which we will create the virtual machine. It is important to define this variable before loading the library because if you do otherwise, it won’t work.
  2. Load the googleComputeEngineR library and create a virtual machine. To do so, we will require some additional parameters, like the name and type of the machine, the template, the user and password.

So, let’s create our virtual machine with R in Google Cloud!

project = "direct-analog-185510"
zona = "us-central1-b"
account_key = "clave_prueba.json"

Sys.setenv(GCE_AUTH_FILE = account_key,
           GCE_DEFAULT_PROJECT_ID = project,
           GCE_DEFAULT_ZONE = zona
)

library(googleComputeEngineR)
## Setting scopes to https://www.googleapis.com/auth/cloud-platform
## Successfully auto-authenticated via clave_prueba.json
## Set default project ID to 'direct-analog-185510'
## Set default zone to 'us-central1-b'
# Crear MV
vm <- gce_vm(template = "rstudio", 
             name="demo-rstudio",
             predefined_type = "f1-micro", 
             username = "ander",
             password = "prueba")
## 2020-06-29 22:04:38> Creating template VM
## 2020-06-29 22:04:40> Operation running...
## 2020-06-29 22:05:01> Operation complete in 6 secs
## 2020-06-29 22:05:01> ## VM Template: 'rstudio' running at http://35.224.101.3
## 2020-06-29 22:05:01> On first boot, wait a few minutes for docker container to install before logging in.
## ==Google Compute Engine Instance==
## 
## Name:                demo-rstudio
## Created:             2020-06-29 13:04:40
## Machine Type:        f1-micro
## Status:              RUNNING
## Zone:                us-central1-b
## External IP:         35.224.101.3
## Disks: 
##               deviceName       type       mode boot autoDelete
## 1 demo-rstudio-boot-disk PERSISTENT READ_WRITE TRUE       TRUE
## 
## Metadata:  
##                      key            value
## 2               template          rstudio
## 3 google-logging-enabled             true
## 4           rstudio_user            ander
## 5             rstudio_pw           prueba
## 6      gcer_docker_image rocker/tidyverse
## 2020-06-29 22:05:03> demo-rstudio VM running

Done! As you can see we can access the virtual machine on this URL: http://35.224.101.3. It may take some minute for everything to install, but when it does you will see something like this:

Access your virtual machine with R in Google Cloud
Access your virtual machine with R in Google Cloud

If we log in, we will see that we have RStudio Server installed. This installation already has some packages installed.

Loaded libraries on how to create your virtual machine with R in Google Cloud
Loaded libraries on how to create your virtual machine with R in Google Cloud

How to stop your virtual machine

Finally, as virtual machines charge depending on usage time, it is important to learn how to stop our virtual machine. To do so, we simply have to pass the name of the virtual machine to the function gce_vm_stop.

gce_vm_stop("demo-rstudio")
## [[1]]
## ==Zone Operation stop :  RUNNING
## Started:  2020-06-29 13:05:03

Anyway, if you don’t have a computer nearby, you can access Google Cloud console and pause the virtual machine from there.

How to stop virtual machine with R in Google Cloud
How to stop virtual machine with R in Google Cloud

And that was all! I hope that you have found this tutorial on how to create a virtual machine with R in Google Cloud useful. See you at the next one!

Blog sponsored by: