Tag non può essere creato fino a quando l'istanza non è stata creata. Anche se la funzione è chiamata create_instance, ciò che sta facendo è riservare e istanza. Quindi l'istanza potrebbe o non essere avviata. (Di solito lo è, ma a volte ...)
Quindi, non è possibile aggiungere un tag finché non è stato avviato. E non c'è modo di sapere se è stato lanciato senza eseguire il polling. In questo modo:
reservation = conn.run_instances(...)
# NOTE: this isn't ideal, and assumes you're reserving one instance. Use a for loop, ideally.
instance = reservation.instances[0]
# Check up on its status every so often
status = instance.update()
while status == 'pending':
time.sleep(10)
status = instance.update()
if status == 'running':
instance.add_tag("Name","{{INSERT NAME}}")
else:
print('Instance status: ' + status)
return None
# Now that the status is running, it's not yet launched. The only way to tell if it's fully up is to try to SSH in.
if status == "running":
retry = True
while retry:
try:
# SSH into the box here. I personally use fabric
retry = False
except:
time.sleep(10)
# If we've reached this point, the instance is up and running, and we can SSH and do as we will with it. Or, there never was an instance to begin with.
fonte
2012-05-24 02:39:35
Puoi effettivamente assegnare tag ad esempio creare tempo - vedi la mia risposta: https://stackoverflow.com/a/43723682/4988742 –