我正在玩 EC2,这是我的场景:
一次:使用必要的 key 对创建了一个 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 实例并来回同步文件。我想去掉这一步。这是最好的方法吗?或者您有什么建议吗?如果您可以使用来自本地计算机 (abc.txt) 的示例输入文件向代码添加一些行,并在 ec2 上打印文件的内容并将其输出到 out.txt 并将其取回。事实证明,没有密码提示的文件传输是一项挑战。 (最终会添加到 hosts 文件,但还没有查看)
谢谢 SOers!
请您参考如下方法:
这是一个涉及面很广的问题,但您想让 boto 为您启动实例,轮询 EC2 以等待实例启动,然后将 rsync 与 passwordless logins 一起使用来回传输文件。处理完成后,您可以让 boto 关闭实例。
