How to clone an GCP instance to machine-image and restore it

Getting Started

  1. Download gcloud private key on google cloud console

IAM & Admin -> Service Accounts -> Your account name -> Add key -> Create New Key

  1. Install google cloud sdk
  • Download

curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-317.0.0-linux-x86_64.tar.gz

  • Untar

tar -xf google-cloud-sdk-317.0.0-linux-x86_64.tar.gz

  • Install

./google-cloud-sdk/install.sh

  1. Authorizing gcloud

Follow the gcloud official document

List running instance

  • Using shell script

    gcloud beta compute instances list


    NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
    c****01 us-central1-a f1-micro xx.xxx.x.x xxx.xxx.xx.xxx RUNNING
    c****02 us-central1-a f1-micro xx.xxx.x.x xx.xxx.xx.xxx RUNNING

  • Using Python

    from googleapiclient.discovery import build

    # Here I use beta version because it support create instance from machine image
    compute = build('compute', 'beta')
    instances = compute.instances()

    def show_running_instances(status='RUNNING'):
    # Here I will use the instances client to list all running instance
    items = instances.list(
    project=PROJECT,
    zone=ZONE,
    filter=f'status={status}'
    ).execute()

    # Let display few information of those instance
    items = items.get('items')
    formated_items = [(item.get('name'), item.get('status')) for item in items]
    print('\n'.join([str(item) for item in formated_items]))

    show_running_instances()

Take image of a running instance

This command will take about 40-60 seconds

gcloud beta compute machine-images create my-image-v01 --source-instance c****01


Created [https://www.googleapis.com/compute/beta/projects/xxx-xxx-2xxxxx5/global/machineImages/my-image-v01].
NAME STATUS
my-image-v01 READY

Create new instance of the my-image-v01

This command will take about 10s to spin up an instance

  • Using shell script

    gcloud beta compute instances create c****03 --source-machine-image my-image-v01


    Created [https://www.googleapis.com/compute/beta/projects/xxx-xxx-2xxxxx5/zones/us-central1-a/instances/c****03].
    NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
    c****03 us-central1-a f1-micro xx.xxx.x.xx xx.xxx.xxx.xxx RUNNING

  • Using Python Api

    from googleapiclient.discovery import build

    # Here I use beta version because it support create instance from machine image
    compute = build('compute', 'beta')
    instances = compute.instances()

    def create_instance(name=''):
    return instances.insert(
    project=PROJECT,
    zone=ZONE,
    body={ 'sourceMachineImage': 'global/machineImages/my-image-v01', 'name': name }
    ).execute()

    create_instance('test001')

To create an instance and run the startup script after running. Read more

# You can also upload a script to make the instance automatically run it after created.
startup_script = open('./startup-script.sh', 'r').read()

instances.insert(
project=PROJECT,
zone=ZONE,
body={
'sourceMachineImage': 'global/machineImages/my-image-v01',
'name': 'test001',
'metadata': {
'items': [{
'key': 'startup-script',
'value': startup_script
}]
}
}
).execute()

Delete running instance

  • Using shell script

    gcloud beta compute instances delete c****03


    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.
    - [c****03] in [us-central1-a]

    Do you want to continue (Y/n)? y

    Deleted [https://www.googleapis.com/compute/beta/projects/xxx-xxx-2xxxxx5/zones/us-central1-a/instances/c****03].

  • Using python api

    from googleapiclient.discovery import build

    # Here I use beta version because it support create instance from machine image
    compute = build('compute', 'beta')
    instances = compute.instances()

    def delete(name=''):
    return instances.delete(
    project=PROJECT,
    zone=ZONE,
    instance=name
    ).execute()

    delete('test001')

Delete the machine-image

  • Using shell script
    gcloud beta compute machine-images delete my-image-v01


    The following machine images will be deleted:
    - [my-image-v01]

    Do you want to continue (Y/n)? y

    Deleted [https://www.googleapis.com/compute/beta/projects/xxx-xxx-2xxxxx5/global/machineImages/my-image-v01].