Categories
Cloud GCP

Google Cloud Platform – Compute Engine Management through CLI

Sometimes it much easy to create a GCP Compute Engines in Command Line Interface, than going through process of slow gcp portal.

One of the pre-requisite before creating compute engines, is to make sure gcloud command-line tools are installed. The GCP Cloud SDK can be installed from https://cloud.google.com/sdk

To create a compute engine execute, using the default zone

gcloud compute instances create <name>

More option available at https://cloud.google.com/sdk/gcloud/reference/compute/instances

Since we are aware of creating the compute engine using bare-minimum options, its always to reuse the code to orchestrate the creation process, than providing the various options everytime creating compute engine. So, creating a shell script to reuse the code is quick and easy. For demo purpose, n1-standard and europe-west2-a machine and zone are used.

-----------
Bash Script
-----------

create_vm.sh

echo "creating "$1 " compute engine"
gcloud compute instances create $1 --machine-type n1-standard-1 --zone europe-west2-a

chmod 755 create_vm.sh

Now its very easy and quick to create compute engines

CREATE DEV VM
-------------

./create_vm.sh dev
creating dev  compute engine
Created [https://www.googleapis.com/compute/v1/projects/abc-xyz/zones/europe-west2-a/instances/dev].
NAME  ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP   STATUS
dev   europe-west2-a  n1-standard-1               10.154.0.9   34.89.55.187  RUNNING

CREATE TEST VM
---------------

./create_vm.sh test
creating test  compute engine
Created [https://www.googleapis.com/compute/v1/projects/abc-xyz/zones/europe-west2-a/instances/test].
NAME  ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
test  europe-west2-a  n1-standard-1               10.154.0.10  35.197.212.108  RUNNING

CREATE PROD VM
---------------

./create_vm.sh prod

creating prod  compute engine
Created [https://www.googleapis.com/compute/v1/projects/abc-xyz/zones/europe-west2-a/instances/prod].
NAME  ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
prod  europe-west2-a  n1-standard-1               10.154.0.11  35.197.198.16  RUNNING

To list the instances, execute gcloud compute instances list

gcloud compute instances list

NAME  ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
dev   europe-west2-a  n1-standard-1               10.154.0.9   34.89.55.187    RUNNING
prod  europe-west2-a  n1-standard-1               10.154.0.11  35.197.198.16   RUNNING
test  europe-west2-a  n1-standard-1               10.154.0.10  35.197.212.108  RUNNING

To delete compute engines, execute gcloud compute instances delete

-----------
Bash Script
-----------

cat destroy_vm.sh

echo "delete  "$1 "compute engine"
gcloud compute instances delete $1  --zone europe-west2-a


DELETE DEV VM
./destroy_vm.sh dev

delete  dev compute engine
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the --keep-disks flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost.
[dev] in [europe-west2-a] 
Do you want to continue (Y/n)?  Y
Deleted [https://www.googleapis.com/compute/v1/projects/abc-xyz/zones/europe-west2-a/instances/dev].

DELETE TEST VM

./destroy_vm.sh test
delete  test compute engine
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the --keep-disks flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost.
[test] in [europe-west2-a] 
Do you want to continue (Y/n)?  Y
Deleted [https://www.googleapis.com/compute/v1/projects/abc-xyz/zones/europe-west2-a/instances/test].

DELETE PROD VM

./destroy_vm.sh prod
delete  prod compute engine
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the --keep-disks flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost.
[prod] in [europe-west2-a] 
Do you want to continue (Y/n)?  Y
Deleted [https://www.googleapis.com/compute/v1/projects/abc-xyz/zones/europe-west2-a/instances/prod].

 31 total views,  1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.