简体   繁体   中英

How to create a Google Cloud Compute Instance from the an instance template

I want to create a VM from my custom image via the instance template mechanism. I can see that the instance template is available. Below is my config:

config = {
        'name': name,
        'machineType': machine_type,

        # Specify the boot disk and the image to use as a source.
        'disks': [
            {
                'boot': True,
                'autoDelete': True,
                'initializeParams': {
                    'sourceImage': source_disk_image,
                }
            }
        ],

        # Specify a network interface with NAT to access the public
        # internet.
        'networkInterfaces': [{
            'network': 'global/networks/default',
            'accessConfigs': [
                {'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
            ]
        }],

        # Allow the instance to access cloud storage and logging.
        'serviceAccounts': [{
            'email': 'default',
            'scopes': [
                'https://www.googleapis.com/auth/devstorage.read_write',
                'https://www.googleapis.com/auth/logging.write'
            ]
        }],

        # Metadata is readable from the instance and allows you to
        # pass configuration from deployment scripts to instances.
        'metadata': {
            'items': [{
                # Startup script is automatically executed by the
                # instance upon startup.
                'key': 'startup-script',
                'value': startup_script,
                'VIDEOPATH': videopath,
                'uuid': uuid
            }]
        }
    }

How can I use the python api to create the VM instance via the instance template?

compute.instances().insert(
        project=project,
        zone=zone,
        body=config).execute()

The below item to should be added to the json including device name and sourceImage, and the Google Compute Instance will be created from the particular image.

    # Specify the boot disk and the image to use as a source.
    "disks": [
        {
            "kind": "compute#attachedDisk",
            "type": "PERSISTENT",
            "boot": True,
            "mode": "READ_WRITE",
            "autoDelete": True,
            "deviceName": "instance-template-1",
            "initializeParams": {
                "sourceImage": "projects/supereye/global/images/image-1",
                "diskType": "projects/supereye/zones/europe-west2-b/diskTypes/pd-standard",
                "diskSizeGb": "10"
            }, "diskEncryptionKey": {}
        }
    ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM