Reputation: 1312
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()
Upvotes: 1
Views: 686
Reputation: 1312
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": {}
}
],
Upvotes: 1