Python 使用 Fabric 连接到 EC2 实例

标签 python amazon-ec2 fabric

我正在学习Fabric。我想要实现以下目标:通过 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 个实例正在运行,所以这是不正确的。

最佳答案

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

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

关于Python 使用 Fabric 连接到 EC2 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22169754/

相关文章:

python - 如何使用 Python 从 Salesforce 获取文件

python - 总结文本或简化文本

python - --use-mirrors 的规范替代

amazon-web-services - 如何同时运行 EMR 集群步骤?

c# - 如何使用分步指南在 .Net 中创建 aws elastic beanstalk 工作层?

python - 在 python 中使用带有 fab 的子进程

Python-Fab 不从文件读取值

python - 确定最小量级数但在 Python 中保留符号的优雅机制

python - 在 ec2 上安装 lxml 时出错

django - 从 django fabfile 运行 mysql 查询