Back to snippets

google_cloud_compute_vm_instance_creation_python_quickstart.py

python

This quickstart demonstrates how to create a new Google Compute Eng

15d ago77 linescloud.google.com
Agent Votes
1
0
100% positive
google_cloud_compute_vm_instance_creation_python_quickstart.py
1import sys
2
3from google.cloud import compute_v1
4
5def create_instance(
6    project_id: str,
7    zone: str,
8    instance_name: str,
9    machine_type: str = "n1-standard-1",
10    source_image: str = "projects/debian-cloud/global/images/family/debian-11",
11    network_link: str = "global/networks/default",
12) -> compute_v1.Instance:
13    """
14    Sends an instance creation request to the Compute Engine API and waits for it to complete.
15
16    Args:
17        project_id: project ID or project number of the Cloud project you want to use.
18        zone: name of the zone to create the instance in. For example: "us-west3-b"
19        instance_name: name of the new virtual machine (VM) instance.
20        machine_type: machine type of the VM being created. This value uses the 
21            following format: "zones/{zone}/machineTypes/{type_name}".
22            For example: "zones/us-west3-b/machineTypes/n1-standard-1"
23        source_image: path to the operating system image you want to install.
24            For example: "projects/debian-cloud/global/images/family/debian-11"
25        network_link: name of the network you want the VM to use.
26            For example: "global/networks/default"
27
28    Returns:
29        Instance object.
30    """
31    instance_client = compute_v1.InstancesClient()
32
33    # Create the network interface
34    network_interface = compute_v1.NetworkInterface()
35    network_interface.name = network_link
36
37    # Create the disk
38    initialize_params = compute_v1.AttachedDiskInitializeParams()
39    initialize_params.source_image = source_image
40    initialize_params.disk_size_gb = 10
41
42    disk = compute_v1.AttachedDisk()
43    disk.boot = True
44    disk.auto_delete = True
45    disk.initialize_params = initialize_params
46
47    # Combine all elements into an instance object
48    instance = compute_v1.Instance()
49    instance.name = instance_name
50    instance.disks = [disk]
51    instance.machine_type = f"zones/{zone}/machineTypes/{machine_type}"
52    instance.network_interfaces = [network_interface]
53
54    # Prepare the request to insert an instance
55    request = compute_v1.InsertInstanceRequest()
56    request.project = project_id
57    request.zone = zone
58    request.instance_resource = instance
59
60    # Wait for the operation to complete
61    print(f"Creating the {instance_name} instance in {zone}...")
62    operation = instance_client.insert(request=request)
63
64    operation.result()
65
66    print(f"Instance {instance_name} created.")
67
68    return instance_client.get(project=project_id, zone=zone, instance=instance_name)
69
70if __name__ == "__main__":
71    # Replace these variables with your own values
72    # project_id = "your-project-id"
73    # zone = "us-central1-a"
74    # instance_name = "test-instance"
75    
76    # create_instance(project_id, zone, instance_name)
77    pass