In this post we will start with creating an GCP Virtual machine instance and create an express.js web application.
Prerequisite: Have a GCP account with billing enabled. Please refer how to get started with Google Cloud Platform account setup.
Lets open GCP console and create a VM instance. (If you are creating very first time then Google will enforce to enable Compute engine). It will take few seconds to complete and
Configure all the details to define your virtual machine: Click "Create Instance" and provide a valid name for VM. I have used "instance-express-learn". There are multiple ways to create VM(using predefined template, create custom template, etc), I am going ahead with default options of machine configuration.
Click "CREATE" or Copy command from "EQUIVALENT COMMAND LINE" (run command in Cloud shell) to create VM.
What is Google Cloud shell and Where you can find google Cloud shell?
- Ephemeral VM which offers a terminal on the browser.
- Click on Activate Cloud Shell ">". It will open session for executing commands.
Verify VM is running/listing of your VM instances:
- Green tick indicates that VM is successfully created and running.
- From cloud shell, run following command and list of all VM's running.When you connect to VMs using the Cloud Console, Compute Engine creates an ephemeral SSH key for you
n0r0082@m-c02z31rnlvdt ~ % gcloud compute instances list NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS instance-express-learn us-central1-a e2-medium 10.128.0.2 34.123.1xx.2xx RUNNING
We have a running virtual machine with adequate infrastructure spawned with just few clicks/commands. This summarize beautifully why Cloud is a buzzword :)
How to access GCP VM from local terminal
- Please refer Install the Google Cloud CLI / Cloud SDKs by language to setup gcloud locally.
- Once gcloud has been setup, we can login to GCP from terminal. First authenticate terminal session using following command.
n0r0082@m-c02z31rnlvdt ~ % gcloud auth login Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?response_type=code
&client_id=VERY_BIG_STRING..... You are now logged in as [nikhil*******@gmail.com]. Your current project is [None]. You can change this setting by running: $ gcloud config set project PROJECT_ID Updates are available for some Cloud SDK components. To install them, please run: $ gcloud components update
- Once authenticated, close web browser or you can go to console.
- Set PROJET_ID as recommended by above command and Run command to list VM's and we will get same result as command ran from Cloud shell.
n0r0082@m-c02z31rnlvdt ~ % gcloud config set project named-magnet-342122 Updated property [core/project]. n0r0082@m-c02z31rnlvdt ~ % n0r0082@m-c02z31rnlvdt ~ % n0r0082@m-c02z31rnlvdt ~ % gcloud compute instances list NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS instance-express-learn us-central1-a e2-medium 10.128.0.2 34.123.124.191 RUNNING
- What is PROJECT_ID value? - Fetch PROJECT_ID value from GCP Console. ID value is PROJECT_ID value.
Create express.js web-application in VM:
- Install node.js and update with latest versio{by default node is installed in VM}
nikhilranjan2712@cloudshell:~ (named-magnet-342122)$ node --version v12.14.1 >>>Update node version with latest nikhilranjan2712@cloudshell:~ (named-magnet-342122)$ nvm install stable Downloading and installing node v17.6.0... Downloading https://nodejs.org/dist/v17.6.0/node-v17.6.0-linux-x64.tar.xz... ###############################################.... 100.0% Computing checksum with sha256sum Checksums matched! Now using node v17.6.0 (npm v8.5.1) nikhilranjan2712@cloudshell:~ (named-magnet-342122)$ node --version v17.6.0
- Set default to latest version
nikhilranjan2712@cloudshell:~ (named-magnet-342122)$ nvm alias default stable default -> stable (-> v17.6.0)
- Install exress-generator and then using exress-generator create an app named "client-app"
nikhilranjan2712@cloudshell:~ (named-magnet-342122)$ npm install -g express-generator nikhilranjan2712@cloudshell:~ (named-magnet-342122)$ npm audit fix --force nikhilranjan2712@cloudshell:~ (named-magnet-342122)$ npm install -g express-generator create : client-app/ create : client-app/public/ create : client-app/public/javascripts/ create : client-app/public/images/ create : client-app/public/stylesheets/ create : client-app/public/stylesheets/style.css create : client-app/routes/ create : client-app/routes/index.js create : client-app/routes/users.js create : client-app/views/ create : client-app/views/error.jade create : client-app/views/index.jade create : client-app/views/layout.jade create : client-app/app.js create : client-app/package.json create : client-app/bin/ create : client-app/bin/www change directory: $ cd client-app install dependencies: $ npm install run the app: $ DEBUG=client-app:* npm start
- Start app using following command
nikhilranjan2712@cloudshell:~/client-app (named-magnet-342122)$ DEBUG=client-app:* npm start > client-app@0.0.0 start > node ./bin/www client-app:server Listening on port 3000 +0ms
- Access application http://localhost:3000. Open a new console and access app using curl command.
-----====-----------
I read your blog now share great information here.
ReplyDeleteBobcat hire