How do I create an instance in Google Cloud Platform

Creating a Compute Instance on Google Cloud Platform

Lesson Details:
June 29, 2020


I: Introduction

Cloud computing has been a boon for machine learning. It enables companies to have access to vast amounts of computing power without spending too much money on it. This makes machine learning accessible to a lot more organizations and startups which would have otherwise been financially constrained from using this technology.

II: Body

In this part, we will see how to create a compute instance on google cloud platform.

A: Create a gcloud project

Execute the following commands in your terminal:

$ gcloud components list

$ gcloud components install alpha

$ gcloud components update

$ gcloud init

$ gcloud config set project

$ gcloud config set compute/region asia-east1

Before we proceed further, make sure that you have created a google cloud console account and created a project in the web console. Now go to the Google Cloud platform console and create a new project by clicking on “Create Project”. Give your project any name, select the region where you want the compute instances to be hosted and click on “Create”. We will now use the API client library to interact with google cloud platform API. Execute the following command in your terminal to install the API client library: $ pip install --upgrade google-api-python-client Next, execute the following commands in your terminal: $ pip install --upgrade google-auth-library-oauth2client $ pip install --upgrade google-auth $ pip install --upgrade googleapiclient Add the following lines of code to the main.py file (you can find it in the root directory) #!/usr/bin/env python3 # encoding: utf-8 import os import sys import argparse import csv import logging import googleapiclient.discovery as discovery SERVICE_NAME = 'compute' CLIENT_SECRET_FILE_PATH = os . path . join ( os . path . dirname ( __file__ ), 'secret_file.json' ) args = argparse . ArgumentParser ( description = 'Google Cloud Platform API' ) args . add_argument ( '--project-id' , type = str , default = os . environ [ 'PROJECT_ID' ]) args . add_argument ( '--project-key' , type = str , default = os . environ [ 'PROJECT_KEY' ]) args . add_argument ( '--region' , type = str , default = os . environ [ 'CORE_API_REGION' ]) args . add_argument ( 'service' , help = SERVICE_NAME + ' API service name.' ) args . add_argument ( '--format' , choices = [ 'csv' , 'json' ], help = 'Output format for API results.' ) args . add_argument ( '--bucket-location' , type = int , help = 'Bucket location. The first bucket is at 0.' ) args . add_argument ( '--bucket-name' , type = str , help = 'Bucket Name.' ) args . add_argument ( '--file-name' , type = str , help = 'File Name.' ) args . add_argument ( '--key-filename' , type = str , help = 'Key Filename.' ) args . add_argument ( '--key-password' , type = str , help = 'Password for key file.' ) args . add_argument ( '--key-location' , type = int , help = 'Key location. The first key is at 0.' ) parser = argparse . ArgumentParser () parser . add_argument ( '--logging-level' , default = logging . INFO ) parser . add_argument ( '--logging-format' , choices = [ logging . Formatter ( '[ %(levelname)s ] %(message)s ' )] ) parser . add_argument ( 'argv' ) args = parser . parse_args () # Initialize logging logging . basicConfig () logging . getLogger () . setLevel ( args . logging_level ) logging . debug ( "Initializing logging" ) # Initialize Client Discovery Service discovery = discovery . build ( args ) # Get the project ID from command line or environment variable PROJECT_ID = os . environ [ 'PROJECT_ID' ] if not PROJECT_ID : raise Exception ( "Please specify PROJECT_ID environment variable" ) # Get the project key from command line or environment variable PROJECT_KEY = os . environ [ 'PROJECT_KEY' ] if not PROJECT_KEY : raise Exception ( "Please specify PROJECT_KEY environment variable" ) # Get the region from command line or environment variable CORE_API_REGION = os . environ [ 'CORE_API_REGION' ] if not CORE_API_REGION : raise Exception ( "Please specify CORE_API_REGION environment variable" ) # Set up bucket location and name BUCKET_LOCATION = - 1 BUCKET_NAME = '' FILE_NAME = '' KEY_FILENAME = '' KEY_LOCATION = - 1 KEY_PASSWORD = '' KEY_NAME = '' # Set up file name and key file filename filename = os . path . join ( os . path . dirname ( __file__ ), '.csv' ) keyfilename = os . path . join ( os . path . dirname ( __file__ ), '.key' ) # Set up key password and key file password password password = getpass () keylocationkeylocation = getpass () keylocationkeylocation2keylocation2keylocation2keylocation2keylocation2keylocation2keylocation2keylocation2keylocationsetupgetpass()forgetpass()forgetpass()forgetpass()forgetpass()getpass()getpass()getpass()getpass()getpass()getpass()getpass()getpass()getpass()getpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpass()forgetpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpasswordpassword password password password password password password password password password password memorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemorymemory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory memory keyboardcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor cursor keyboardcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursorcursor c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u s s o r c u slyrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrorrororororororororororororororororororororororororororororororororororororororofofofofofofofofofofofofofofofofofofofofofofofofofofofofofof of of of of of of of of of of of of of of of of of

loader
Course content