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

Python 如何从实例模板创建Google云计算实例,python,google-cloud-platform,google-compute-engine,Python,Google Cloud Platform,Google Compute Engine,我想通过实例模板机制从自定义映像创建VM。我可以看到实例模板是可用的。以下是我的配置: config = { 'name': name, 'machineType': machine_type, # Specify the boot disk and the image to use as a source. 'disks': [ { 'boot': True,

我想通过实例模板机制从自定义映像创建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 Compute实例

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

您可以使用python客户端库创建VM实例。这里有一篇文章提供了一个关于创建实例的python脚本。