使用boto与EC2进行服务器处理和结果获取的最有效方法是什么?

2 投票
1 回答
741 浏览
提问于 2025-04-17 06:36

我正在玩弄EC2(亚马逊的云服务器),这是我的情况:

一次性操作:创建了一个EC2实例,并设置了必要的密钥对。

每天的操作:

fire up an EC2 instance.
send a file of IDs to EC2 micro-instance from local machine.
fire a python script to process the IDs and generate an output file.
fetch the output file to the local machine from the EC2 instance.
stop the EC2 instance.

影响因素:

I am using the same EC2 instance every time I want to process this file.
I want to keep costs down, so I want to cron the whole process to start and stop at a certain time interval.

大致的代码:

from boto.ec2.connection import EC2Connection

AWS_ACCESS_KEY_ID = 'yourkey'
AWS_SECRET_ACCESS_KEY = 'yoursecret'

conn = EC2Connection(AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY)


reservation = conn.run_instances('ami-5647a33f', instance_type='m1.micro', key_name='mykey')

instance = reservation.instances[0]

while not instance.update() == 'running':
  time.sleep(5)

## Fetch the file from local machine
## --> Do the processing here --<
## send the file back to the local machine

# time up for the day, stop it
instance.stop()

现在,我是手动启动和停止EC2实例,然后用rsync命令来回传输文件。我想要省去这个步骤。这样做是最好的方法吗?你们有什么建议吗?如果可以的话,请在代码中添加一些行,使用本地机器上的一个示例输入文件(abc.txt),然后打印出这个文件在EC2上的内容,并把结果输出到out.txt,再把它取回来。没有密码提示的文件传输让我感到很棘手。(我最终会把它添加到hosts文件中,但我还没研究过)

谢谢大家!

1 个回答

0

这个问题有点广泛,但你想要的是让boto帮你启动一个实例,然后去检查这个实例是否已经启动。接着,你可以用rsync和无密码登录来传输文件。处理完成后,你可以让boto把这个实例关掉。

撰写回答