亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢索 ec2 實例的公共 dns 名稱時出錯

檢索 ec2 實例的公共 dns 名稱時出錯

牛魔王的故事 2021-05-30 14:15:35
我正在嘗試檢索 ec2 實例的公共 dns 名稱。這是我的python3腳本。import sysimport boto3from botocore.exceptions import ClientErrorinstance_id = "i-03e7f6391a0f523ee"action = 'ON'ec2 = boto3.client('ec2')if action == 'ON':    # Do a dryrun first to verify permissions    try:        ec2.start_instances(InstanceIds=[instance_id], DryRun=True)    except ClientError as e:        if 'DryRunOperation' not in str(e):            raise    # Dry run succeeded, run start_instances without dryrun    try:        response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)        print(response)    except ClientError as e:        print(e)else:    # Do a dryrun first to verify permissions    try:        ec2.stop_instances(InstanceIds=[instance_id], DryRun=True)    except ClientError as e:        if 'DryRunOperation' not in str(e):            raise    # Dry run succeeded, call stop_instances without dryrun    try:        response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False)        print(response)    except ClientError as e:        print(e)instance = ec2.Instance('i-1234567890123456')while instance.state['Name'] not in ('running', 'stopped'):        sleep(5)        print("the instance is initializing")#pubdns=instance.PublicDnsName#print ("public dns name"+pubdns)def get_name(inst):    client = boto3.client('ec2')    response = client.describe_instances(InstanceIds = [inst[0].instance_id])    foo = response['Reservations'][0]['Instances'][0]['NetworkInterfaces'][0]['Association']['PublicDnsName']    return foofoo = get_name(instance_id)print (foo)如果我使用ec2 = boto3.client('ec2')在上面的代碼中,出現以下錯誤:AttributeError:“ EC2”對象沒有屬性“ Instance”如果我用ec2 = boto3.resource('ec2')然后我得到這個錯誤:AttributeError:“ ec2.ServiceResource”對象沒有屬性“ start_instances”我想要做的是能夠連接到 ec2 實例并檢索其 publicdns 名稱。但是現在我得到了錯誤in get_name    response = client.describe_instances(InstanceIds = [inst[0].instance_id])AttributeError: 'str' object has no attribute 'instance_id'
查看完整描述

2 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

您將兩個想法混為一談。

boto3.client 創建一個對象,您可以通過該對象查找 ec2 等資源。

一旦有了資源,就可以開始對其進行操作。

ec2 = boto3.client('ec2')

然后

instance = ec2.resource('ec2').instance(instance_id)

第二個從 ec2資源而不是 boto3 ec2 客戶端查找您的 ec2 實例。


查看完整回答
反對 回復 2021-06-01
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號