阅读背景:

python Boto: 管理AWS EC2 实例

来源:互联网 

1,设置连接区域,与要连接的实例一致
使用stream logging可以看到更多信息

>>> import boto
>>> boto.set_stream_logger('boto')
>>> import boto.ec2
>>> ec2 = boto.ec2.connect_to_region('us-west-2')
2018-05-16 01:26:26,012 boto [DEBUG]:Using access key found in config file.
2018-05-16 01:26:26,012 boto [DEBUG]:Using secret key found in config file.
>>> ec2
EC2Connection:ec2.us-west-2.amazonaws.com
2,确定相关参数

>>> image_id = 'ami-d0d8b8e0'
>>> key_pair = 'pk-aws'
>>> security_group = 'ssh-access'
>>> instance_type = 't1.micro'

3, 连接实例
>>> reservations = ec2.run_instances(
...     image_id,
...     key_name=key_name,
...     instance_type=instance_type,
...     security_groups=[security_group])
>>>

上面命令返回的是实例列表,可以查看相关状态

>>> reservations
Reservation:r-9f78c096
>>> reservations.instances
[Instance:i-d0d558d9]
>>> len(reservations.instances)
1
>>> instance = reservations.instances[0]
>>> instance
Instance:i-d0d558d9>>> instance.id
u'i-d0d558d9'
>>> instance.state
u'pending'
>>> instance.update()
2014-02-14 22:14:00,660 boto [DEBUG]:Method: POST
...
>>> instance.state
u'running'

有时需要update几次才可以看到instance running的状态

可以用实例的public dns name 通过ssh进入running状态的实例

>>> instance.public_dns_name
u'ec2-54-203-88-113.us-west-2.compute.amazonaws.com'
$ ssh -i ~/.ssh/pk-aws.pem [email protected]
The authenticity of host 'ec2-54-203-88-113.us-west-2.compute.amazonaws.com (54.203.88.113)' can't be established.
RSA key fingerprint is 56:3e:9c:b3:75:96:4f:11:44:e9:2b:14:3a:02:f8:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-54-203-88-113.us-west-2.compute.amazonaws.com,54.203.88.113' (RSA) to the list of known hosts.
Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-58-virtual x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Feb 15 06:20:44 UTC 2014

  System load:  0.0               Processes:           58
  Usage of /:   11.1% of 7.87GB   Users logged in:     0
  Memory usage: 8%                IP address for eth0: 10.235.47.92
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    https://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
exit推出
4,停止实例连接

>>> instance.stop()
2014-02-14 22:25:25,012 boto [DEBUG]:Method: POST
...
>>> instance.state
u'stopping'
>>> instance.update()
2014-02-14 22:28:55,768 boto [DEBUG]:Method: POST
...
>>> instance.state
u'stopped'
>>> instance.terminate()
2014-02-14 22:31:06,801 boto [DEBUG]:Method: POST
...
>>> instance.state
u'terminated'
>>>

stop后执行teminate可以彻底停止这个实例

参考:https://cewing.github.io/training.codefellows/lectures/day11/boto.html
https://cloudacademy.com/blog/boto-python-automate-aws-services/


分享到: