この回答は、執筆時点では正確でしたが、現在は古くなっています。AWS APIとライブラリ(boto3など)は、「create_instances」呼び出しの実行時にタグを指定できる「TagSpecification」パラメーターを使用できるようになりました。
インスタンスが作成されるまで、タグを作成することはできません。関数はcreate_instanceと呼ばれますが、実際に実行しているのは予約とインスタンスです。次に、そのインスタンスが起動される場合と起動されない場合があります。(通常はそうですが、時々...)
そのため、タグが起動されるまでタグを追加することはできません。そして、ポーリングせずに起動されたかどうかを判断する方法はありません。そのようです:
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.