Boto3 EC2 describe_instances始终返回空 [英] Boto3 ec2 describe_instances always returns empty

查看:272
本文介绍了Boto3 EC2 describe_instances始终返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的沙箱,我想开始工作,以便可以在更大的应用程序中使用它:

I have a very simple sandbox I'm trying to get to work so I can use it in a bigger application:

ec2_client = boto3.client(
    'ec2',
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY)

response = ec2_client.describe_instances()
print(response)

它会导致...

{
  'Reservations': [], 
  'ResponseMetadata': {
    'RequestId': '2c28e8aa-da6d-4ca4-8ea7-f672518cac9f',
    'HTTPStatusCode': 200, 
    'HTTPHeaders': {
      'content-type': 'text/xml;charset=UTF-8', 
      'transfer-encoding': 'chunked', 
      'vary': 'Accept-Encoding', 
      'date': 'Thu, 07 Dec 2017 16:44:30 GMT', 
      'server': 'AmazonEC2'
  }, 
  'RetryAttempts': 0}
}

但是问题是无论我运行多少次Reservations始终为空:(.

But the problem is no matter how many times I run this Reservations is ALWAYS empty :(.

在AWS consonle中,我可以清楚地看到实例正在运行...

In AWS consonle I can CLEARLLY see an instance is running...

我尝试启动更多实例,然后重新启动我正在运行的实例.我将初始脚本放入一个循环中,然后重复运行,以查找实际上有数据的Reservations数组的任何符号.

I tried starting more instances, restarting the instances I had running. I put my initial script in a loop and ran it on repeat while I did this looking for any sign of the Reservations array actually having data.

我再次检查了我的AWS ACCESS_KEY和SECRET_KEY是否都正确并指向正确的帐户.他们是.

I double checked that my aws ACCESS_KEY and SECRET_KEY are both correct and pointing to the correct account. They are.

我不知道为什么会这样.它是如此简单,应该可以正常工作.我是AWS的新手,因此非常感谢您的帮助.

I have no clue why this is. Its so simple and should be working. I'm new to AWS so any help is appreciated.

推荐答案

似乎您忘记添加区域了.

Seems that you forgot to add the region.

在创建客户时设置区域

ec2_client = boto3.client(
  'ec2',
  aws_access_key_id=ACCESS_KEY,
  aws_secret_access_key=SECRET_KEY,
  region_name=REGION_NAME
)

response = ec2_client.describe_instances()
print(response)

如果您的EC2实例位于俄勒冈州,则可以执行region_name='us-west-2'

If your EC2 instances are in Oregon, you can do region_name='us-west-2'

不建议使用硬编码凭据.您可以使用awscli配置配置文件,然后在代码中引用它.

Hard coding credentials is not recommended. You can configure your profiles using the awscli and then reference it in your code.

session = boto3.Session(profile_name='dev')
# Any clients created from this session will use credentials
# from the [dev] section of ~/.aws/credentials.
ec2_client = session.client('ec2')

您可以阅读有关Boto3凭据的更多信息 Boto3凭据

You can read more about Boto3 credentials Boto3 Credentials

这篇关于Boto3 EC2 describe_instances始终返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆