簡體   English   中英

如何讓Boto返回EC2實例 - S3工作正常

[英]How do I get Boto to return EC2 instances - S3 works fine

我在使用Boto的EC2位時遇到了一些問題(Boto v2.8.0,Python v2.6.7)。

第一個命令返回S3存儲桶列表 - 一切都很好! 獲取EC2實例列表的第二個命令會突然顯示403,“查詢字符串身份驗證需要簽名,過期和AWSAccessKeyId參數”

s3_conn = S3Connection(AWSAccessKeyId, AWSSecretKey)
print s3_conn.get_all_buckets()

ec2_conn = EC2Connection(AWSAccessKeyId, AWSSecretKey)
print ec2_conn.get_all_instances()

此外,我的憑據都很好(完全管理員) - 我使用Ruby aws-sdk測試它們,EC2和S3都可以正常工作。

我還注意到ec2_conn對象中的host屬性是s3-eu-west-1.amazonaws.com ,“s3”......? 那肯定是錯的? 我已經嘗試將它復制到正確的終點,但沒有運氣。

任何幫助將非常感謝謝謝

這是我用來列出可能多個區域的所有實例的一些工作代碼。 它做的比你需要的要多得多,但也許你可以把它削減到你想要的。

#!/usr/bin/python
import boto
import boto.ec2
import sys

class ansi_color:
  red   = '\033[31m'
  green = '\033[32m'
  reset = '\033[0m'
  grey  = '\033[1;30m'


def name(i):
  if 'Name' in i.tags:
    n = i.tags['Name']
  else:
    n = '???'
  n = n.ljust(16)[:16]
  if i.state == 'running':
    n = ansi_color.green + n + ansi_color.reset
  else:
    n = ansi_color.red + n + ansi_color.reset
  return n

def pub_dns( i ):
  return i.public_dns_name.rjust(43)

def pri_dns( i ):
  return i.private_dns_name.rjust(43)

def print_instance( i ):
  print '  ' + name(i) + '| ' + pub_dns(i) + ' ' + pri_dns(i)


regions = sys.argv[1:]
if len(regions)==0:
  regions=['us-east-1']

if len(regions)==1 and regions[0]=="all":
  rr = boto.ec2.regions()
else:
  rr = [ boto.ec2.get_region(x) for x in regions ]

for reg in rr:
  print "========"
  print reg.name
  print "========"
  conn = reg.connect()

  reservations = conn.get_all_instances()

  for r in reservations:
  #  print ansi_color.grey + str(r) + ansi_color.reset
    for i in r.instances:
      print_instance(i)

有connect_to_region命令:

import boto.ec2

connection = boto.ec2.connect_to_region('eu-west-1', aws_access_key_id=AWSAccessKeyId,
                                        aws_secret_access_key=AWSSecretKey)

Boto教程提供了另一種方式。 該方法基本上可以這樣工作:

import boto.ec2

for region in boto.ec2.regions():
    if region.name == 'my-favorite-region':
        connection = region.connect()
        break

這還沒有在舊版本的Boto上使用。

你有順序的IAM證書嗎? 給定的訪問密鑰應該具有EC2的權限。 如果您不確定,可以添加AmazonEC2FullAccess策略進行測試,然后將其調低。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM