简体   繁体   English

如何编写可以启动新EC2实例的python脚本?

[英]How can I write a python script that can start new EC2 instances?

I'm writing a backend for a new project, and on this backend I'd like a controller program. 我正在为一个新项目编写一个后端,在这个后端上,我想要一个控制器程序。

The project is a website for colleges, but I need a new Amazon EC2 instance for each college. 该项目是大学的网站,但我需要为每个大学使用一个新的Amazon EC2实例。 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: 简而言之,我希望能够运行controller new harvard并执行以下操作:

  • Create a new EC2 instance. 创建一个新的EC2实例。
  • Create a new entry in a local database with the name (harvard) and its location (how to access it). 在本地数据库中使用名称(哈佛)及其位置(如何访问)创建一个新条目。
  • Connect to the newly created instance and start sending and executing files. 连接到新创建的实例,然后开始发送和执行文件。

I've read a little bit about boto and Fabric, but without any real lead on how to get started. 我已经阅读了一些有关boto和Fabric的知识,但是对如何入门没有真正的帮助。

Any help would be massively appreciated. 任何帮助将不胜感激。

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. 我已经使用boto库为我的一些项目实例化了新的EC2实例,然后在启动新EC2系统后使用该结构执行配置。

To use them you first need to create your access tokens for Amazon itself. 要使用它们,您首先需要为Amazon本身创建访问令牌。 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. 这些用于boto的令牌被放置到您的源文件中,并在EC2实例的connect方法中使用,或者您可以将它们放置在主目录中的.boto文件中。 The latter is much easier. 后者要容易得多。

What you need from Amazon is the following: 您需要从亚马逊获得以下物品:

  • Security group name and sshkey. 安全组名称和sshkey。
  • Ami id for the instance you wish to create. 您要创建的实例的Ami ID。
  • The type of instance you want, eg: m1.small 您想要的实例类型,例如:m1.small

With the above information you will make a call to the run_instance method with the above information: 使用以上信息,您将使用以上信息调用run_instance方法:

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. 完成此操作后,新实例应开始在您的Amazon控制面板中启动。 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. 您需要检查实例的状态,一旦它处于running状态,就可以使用Fabric来配置实例。

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. 通过上面的方法,它将在同一文件中运行结构命令,但是使用结构的一种更受控制的方法是通过fab文件。 These are explained better on the Fabric documentation. 这些在Fabric文档中有更好的解释。

The above is what I use to automate the creation and setup of instances as needed, so tweak the code to suit your fit. 以上是我用来根据需要自动创建和设置实例的方法,因此请调整代码以适合您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM