我怎么能写一个Python脚本,可以启动新的EC2实例? [英] How can I write a python script that can start new EC2 instances?

查看:162
本文介绍了我怎么能写一个Python脚本,可以启动新的EC2实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个后端的一个新的项目,并在此后端我想一个控制器程序。

I'm writing a backend for a new project, and on this backend I'd like a controller program.

该项目是学院网站,但我需要一个新的Amazon EC2实例为每个学院。我想扩展是尽可能无痛。

The project is a website for colleges, but I need a new Amazon EC2 instance for each college. I want expansion to be as painless as possible.

总之,我希望能够运行控制器的新哈弗键,把它做到以下几点:

In short, I'd like to be able to run controller new harvard and have it do the following:

  • 创建一个新的EC2实例。
  • 创建在本地数据库的名称(哈佛)和它的位置(如何访问它)一个新的条目。
  • 连接到新创建的实例并开始发送和执行文件。

我读过有关博托和Fabric一点点,但没有任何真正的铅对如何开始。

I've read a little bit about boto and Fabric, but without any real lead on how to get started.

任何帮助将是大量AP preciated。

Any help would be massively appreciated.

推荐答案

我已经使用了博托库来实例化新的EC2实例几个我的项目,然后用面料以执行新的EC2系统的配置,一旦他们启动。

I have used the boto library to instantiate new EC2 instances for a few of my project and then used the fabric to perform the configuration of the new EC2 systems once they have booted.

要使用它们,你首先需要创建访问令牌亚马逊本身。这些令牌,为博托放入您的源文件,在连接方法的EC2实例中使用,也可以将它们放入一个 .boto 在你的主目录下的文件。后者是非常容易。

To use them you first need to create your access tokens for Amazon itself. These tokens, for boto are placed into your source file and used in the connect methods for the EC2 instances or you can place them into a .boto file in your home directory. The latter is much easier.

您从亚马逊需要的是以下内容:

What you need from Amazon is the following:

  • 安全组的名称和SSHKEY。
  • 阿米ID为您希望创建的实例。
  • 您想要实例的类型,例如:的m1.small

通过以​​上信息你会调用 run_instance 方法与上面的信息:

With the above information you will make a call to the run_instance method with the above information:

instance = conn.run_instances( ami.ami_id, key_name=ami.sshkey.name,
     instance_type=server.game.instance_type,
     security_groups=[server.game.security_group] )
instance = instance.instances[0]

while instance.update() == "pending":
    time.sleep( 5 )

在此做一个新的实例应该开始启动在亚马逊的控制面板。您需要检查该实例的状态,一旦它处于运行状态,然后你可以用布艺来配置实例。

After this is done a new instance should start to boot up in your Amazon control panel. You need to check the instance's status and once it is in a running state you can then use Fabric to configure the instance.

with settings( host_string="ec2-user@%s" % instance.ip_address,
    key_filename=os.path.join( os.getenv( "HOME" ), 
    ".ssh", "%s.pem" % ami.sshkey.name ),
    connection_attempts=5, timeout=60 ):

    ...
    sudo( "yum -y install mysql mysql-devel" )
    ...

使用它上面将运行织物命令在同一文件中,但使用的织物更可控的方法是通过制造设施的文件。这些都是在织物上的文档解释的更好。

With the above it will run the fabric commands in the same file, but a more controlled method of using Fabric is via fab files. These are explained better on the Fabric documentation.

以上是我用自动根据需要实例的创建和设置,因此调整的code,以满足您的配合。

The above is what I use to automate the creation and setup of instances as needed, so tweak the code to suit your fit.

这篇关于我怎么能写一个Python脚本,可以启动新的EC2实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆