2323import re
2424import sys
2525from typing import Any , List
26+ import warnings
2627
2728from google .api_core .extended_operation import ExtendedOperation
2829from google .cloud import compute_v1
@@ -143,6 +144,8 @@ def create_instance(
143144 external_ipv4 : str = None ,
144145 accelerators : List [compute_v1 .AcceleratorConfig ] = None ,
145146 preemptible : bool = False ,
147+ spot : bool = False ,
148+ instance_termination_action : str = "STOP" ,
146149 custom_hostname : str = None ,
147150 delete_protection : bool = False ,
148151) -> compute_v1 .Instance :
@@ -175,7 +178,10 @@ def create_instance(
175178 accelerators: a list of AcceleratorConfig objects describing the accelerators that will
176179 be attached to the new instance.
177180 preemptible: boolean value indicating if the new instance should be preemptible
178- or not.
181+ or not. Preemptible VMs have been deprecated and you should now use Spot VMs.
182+ spot: boolean value indicating if the new instance should be a Spot VM or not.
183+ instance_termination_action: What action should be taken once a Spot VM is terminated.
184+ Possible values: "STOP", "DELETE"
179185 custom_hostname: Custom hostname of the new VM instance.
180186 Custom hostnames must conform to RFC 1035 requirements for valid hostnames.
181187 delete_protection: boolean value indicating if the new virtual machine should be
@@ -205,6 +211,7 @@ def create_instance(
205211
206212 # Collect information into the Instance object.
207213 instance = compute_v1 .Instance ()
214+ instance .network_interfaces = [network_interface ]
208215 instance .name = instance_name
209216 instance .disks = disks
210217 if re .match (r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$" , machine_type ):
@@ -215,13 +222,22 @@ def create_instance(
215222 if accelerators :
216223 instance .guest_accelerators = accelerators
217224
218- instance .network_interfaces = [network_interface ]
219-
220225 if preemptible :
221226 # Set the preemptible setting
227+ warnings .warn (
228+ "Preemptible VMs are being replaced by Spot VMs." , DeprecationWarning
229+ )
222230 instance .scheduling = compute_v1 .Scheduling ()
223231 instance .scheduling .preemptible = True
224232
233+ if spot :
234+ # Set the Spot VM setting
235+ instance .scheduling = compute_v1 .Scheduling ()
236+ instance .scheduling .provisioning_model = (
237+ compute_v1 .Scheduling .ProvisioningModel .SPOT .name
238+ )
239+ instance .scheduling .instance_termination_action = instance_termination_action
240+
225241 if custom_hostname is not None :
226242 # Set the custom hostname for the instance
227243 instance .hostname = custom_hostname
0 commit comments