【问题标题】:Python using Fabric to connect to an EC2 instancePython 使用 Fabric 连接到 EC2 实例
【发布时间】:2017-03-26 01:16:49
【问题描述】:

我正在学习面料。我想实现以下目标:ssh 进入我的 EC2 机器并 ls 主目录

我从以下开始:

from boto import ec2

from fabric.colors import green as _green, yellow as _yellow


class EC2Conn:
    def __init__(self):
        print(_green("Started..."))
        self.ec2conn = None
        self.user = 'fabUser'
        self.access_key = 'xxxx'
        self.secret_key = 'xxxx'

    def connect(self):
        print(_green("Connecting..."))
        ec2.connect_to_region("eu-west-1a")
        self.ec2conn = ec2.connection.EC2Connection(self.access_key, self.secret_key)

        print(self.get_instances())


    def get_instances(self):
        return self.ec2conn.get_all_instances()


def run_me():
    a = EC2Conn()
    a.connect()

但这给了我一个空白列表 [] 我确实有 1 个实例正在运行,所以这是不正确的。

【问题讨论】:

  • 是否显示错误?
  • 没有错误,它与详细信息连接,但显示并为空 []。当它应该有2个实例时。我设置了一个新用户并给他们正确的权限(完全管理员)

标签: python amazon-ec2 fabric


【解决方案1】:

尝试在您的代码中进行一些更改,如下所示

self.ec2conn = ec2.connect_to_region('eu-west-1', 
                  aws_access_key_id=self.access_key,
                  aws_secret_access_key=self.secret_key)

print(self.get_instances())

或者

region = ec2.get_region('eu-west-1')
self.ec2conn = ec2.connection.EC2Connection(self.access_key,
                  self.secret_key, region=region)

print(self.get_instances())

记住ec2.connect_to_regionec2.connection.EC2Connection 都返回ec2.connection.EC2Connection 对象。参考here

【讨论】:

  • 顶级代码给我 NoneType' 对象没有属性 'get_all_instances' 而第二个也一样 [] 空白
  • 你检查区域了吗?这是对的吗?使用 aws 控制台进行检查。
  • 在控制台中指出该实例的可用区域是 eu-west-1a。
  • eu-west-1a 更改为 eu-west-1。现在它应该可以工作了。
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 2012-11-23
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 2021-05-07
相关资源
最近更新 更多