我正在尝试重启远程实例:
ec2 = boto.connect_ec2(aws_access_key_id = key_id, aws_secret_access_key = access_key)
ec2.reboot_instances([instanceId])
并收到错误The instance ID 'i-xxxxx' does not exist
看起来我应该连接到region - 如果实例ID是唯一的输入参数,我怎么能得到这个值?
答案 0 :(得分:0)
您的实例是否处于运行状态?尝试使用此命令: -
reservations = ec2_conn.get_all_instances(filters={'instance-id' : 'i-xxxxxxxx'})
new_instance = reservations[0].instances[0]
答案 1 :(得分:0)
应在代码之外配置凭据相关信息,如访问密钥,秘密访问密钥和区域。这是为了确保可移植性。
boto3查找凭据的机制是搜索可能的位置列表,并在找到凭据后立即停止。 Boto3搜索凭据的顺序是:
6/21/2017 2:22:02 AM
方法boto.client()
)~/.aws/credentials
)~/.aws/config
和/etc/boto.cfg
)如果脚本本身在EC2实例中运行,最佳做法是依赖将承担IAM角色的实例元数据。
假设您从本地计算机调用脚本,最简单的方法是通过运行~/.boto
并在那里指定区域来修改~/.aws/credentials
。
了解详情:http://boto3.readthedocs.io/en/latest/guide/configuration.html