| 40 | |
| 41 | # [START compute_apiary_create_instance] |
| 42 | def create_instance(compute, project, zone, name, bucket): |
| 43 | # Get the latest Debian Jessie image. |
| 44 | image_response = ( |
| 45 | compute.images() |
| 46 | .getFromFamily(project="debian-cloud", family="debian-11") |
| 47 | .execute() |
| 48 | ) |
| 49 | source_disk_image = image_response["selfLink"] |
| 50 | |
| 51 | # Configure the machine |
| 52 | machine_type = "zones/%s/machineTypes/n1-standard-1" % zone |
| 53 | startup_script = open( |
| 54 | os.path.join(os.path.dirname(__file__), "startup-script.sh"), "r" |
| 55 | ).read() |
| 56 | image_url = "http://storage.googleapis.com/gce-demo-input/photo.jpg" |
| 57 | image_caption = "Ready for dessert?" |
| 58 | |
| 59 | config = { |
| 60 | "name": name, |
| 61 | "machineType": machine_type, |
| 62 | # Specify the boot disk and the image to use as a source. |
| 63 | "disks": [ |
| 64 | { |
| 65 | "boot": True, |
| 66 | "autoDelete": True, |
| 67 | "initializeParams": { |
| 68 | "sourceImage": source_disk_image, |
| 69 | }, |
| 70 | } |
| 71 | ], |
| 72 | # Specify a network interface with NAT to access the public |
| 73 | # internet. |
| 74 | "networkInterfaces": [ |
| 75 | { |
| 76 | "network": "global/networks/default", |
| 77 | "accessConfigs": [{"type": "ONE_TO_ONE_NAT", "name": "External NAT"}], |
| 78 | } |
| 79 | ], |
| 80 | # Allow the instance to access cloud storage and logging. |
| 81 | "serviceAccounts": [ |
| 82 | { |
| 83 | "email": "default", |
| 84 | "scopes": [ |
| 85 | "https://www.googleapis.com/auth/devstorage.read_write", |
| 86 | "https://www.googleapis.com/auth/logging.write", |
| 87 | ], |
| 88 | } |
| 89 | ], |
| 90 | # Metadata is readable from the instance and allows you to |
| 91 | # pass configuration from deployment scripts to instances. |
| 92 | "metadata": { |
| 93 | "items": [ |
| 94 | { |
| 95 | # Startup script is automatically executed by the |
| 96 | # instance upon startup. |
| 97 | "key": "startup-script", |
| 98 | "value": startup_script, |
| 99 | }, |