如何从实例模板创建 Google 云计算实例

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

我想通过实例模板机制从我的自定义映像创建一个 VM。我可以看到实例模板可用。以下是我的配置:

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
            }]
        }
    }

如何使用 python api 通过实例模板创建 VM 实例?

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

应将以下项目添加到 json,包括设备名称和 sourceImage,Google 计算实例将从特定图像创建。

    # 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": {}
        }
    ],