[英]With boto, how can I name a newly spawned EC2 instance?
我正在使用 boto 生成一個基於 AMI 的新 EC2 實例。
ami.run 方法有許多參數,但沒有用於“名稱”的參數 - 也許它被稱為不同的東西?
import boto
c = boto.connect_ec2(ec2_key, ec2_secret)
image = c.get_image(ec2_ami)
reservation = image.run(key_name=ec2_keypair,
security_groups=ec2_secgroups,
instance_type=ec2_instancetype)
instance = reservation.instances[0]
c.create_tags([instance.id], {"Name": instance_name})
在 EC2 中,沒有 api 可以更改機器的實際名稱。 你基本上有兩個選擇。
ec2-create-tags <instance-id> --tag:Name=<computer name>
。 此解決方案的缺點是服務器實際上不會更新到此名稱。 此標簽僅適用於您或在 aws 中查詢服務器列表時使用。一般來說,如果您希望您的服務器在啟動時自行配置,我發現在 EC2 中重命名您的計算機只會帶來更多的麻煩。 如果沒有必要,我建議不要使用它們。 使用標簽或 elb 實例是更好的方法。
對於那些正在尋找 @Roberto 答案的boto3
版本的人:
ec2 = boto3.resource('ec2')
ec2.create_tags(Resources=[instance.id], Tags=[
{
'Key': 'Name',
'Value': instance_name,
},
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.