背景介绍:
目前项目中使用了大量的AWS EC2 Instances作为服务器,在自动化运维方面,我们之前一直使用的是AWS CLI命令行工具,然后在Shell脚本中调用。
最近我想通过脚本实现一个“Clone”的功能,模拟Web Console上的“Launch More Like This”来创建Instance。但在Shell脚本中实现起来感觉不太舒服,于是就直接利用Python的boto库写了一个,在此分享给大家。
具体内容:
脚本地址:https://github.com/mcsrainbow/python-demos/blob/master/demos/awscli.py
相关示例:
$ ./awscli.py -h
usage: awscli.py [-h] (--create | --clone | --terminate) --region REGION [--instance_name INSTANCE_NAME] [--image_id IMAGE_ID] [--instance_type INSTANCE_TYPE] [--key_name KEY_NAME] [--security_group_ids SECURITY_GROUP_IDS] [--subnet_id SUBNET_ID] [--src_instance_name SRC_INSTANCE_NAME] [--dest_instance_name DEST_INSTANCE_NAME] [--private_ip_address PRIVATE_IP_ADDRESS] [--instance_id INSTANCE_ID] [--volume_size VOLUME_SIZE] [--volume_type {standard,io1,gp2}] [--volume_zone VOLUME_ZONE] [--volume_iops VOLUME_IOPS] [--volume_delete_on_termination] [--load_balancer_name LOAD_BALANCER_NAME] [--quick] examples: ./awscli.py --create --region us-west-1 --instance_name idc1-server2 --image_id ami-30f01234 --instance_type t1.micro --key_name idc1-keypair1 --security_group_ids sg-eaf01234f --subnet_id subnet-6d901234 ./awscli.py --create --region us-west-1 --instance_name idc1-server3 --image_id ami-30f01234 --instance_type t1.micro --key_name idc1-keypair1 --security_group_ids sg-eaf01234f --subnet_id subnet-6d901234 --volume_size 10 --volume_type gp2 --volume_zone us-west-1a --volume_delete_on_termination --load_balancer_name idc1-elb1 --private_ip_address 172.16.2.23 ./awscli.py --clone --region us-west-1 --src_instance_name idc1-server1 --dest_instance_name idc1-server2 ./awscli.py --clone --region us-west-1 --src_instance_name idc1-server1 --dest_instance_name idc1-server3 --private_ip_address 172.16.2.23 ./awscli.py --terminate --region us-west-1 --instance_name idc1-server3 ./awscli.py --terminate --region us-west-1 --instance_id i-01234abc ./awscli.py --terminate --region us-west-1 --instance_id i-01234abc --quick ... optional arguments: -h, --help show this help message and exit --create create instance --clone clone instance --terminate terminate instance --region REGION --instance_name INSTANCE_NAME --image_id IMAGE_ID --instance_type INSTANCE_TYPE --key_name KEY_NAME --security_group_ids SECURITY_GROUP_IDS --subnet_id SUBNET_ID --src_instance_name SRC_INSTANCE_NAME --dest_instance_name DEST_INSTANCE_NAME --private_ip_address PRIVATE_IP_ADDRESS --instance_id INSTANCE_ID --volume_size VOLUME_SIZE in GiB --volume_type {standard,io1,gp2} --volume_zone VOLUME_ZONE --volume_iops VOLUME_IOPS --volume_delete_on_termination delete volumes on termination --load_balancer_name LOAD_BALANCER_NAME --quick no wait on termination
$ ./awscli.py –create –region us-west-1 –instance_name idc1-server1 –image_id ami-30f01234
–instance_type t1.micro –key_name idc1-keypair1 –security_group_ids sg-eaf01234f
–subnet_id subnet-6d901234 –volume_size 10 –volume_type gp2 –volume_zone us-west-1a
–volume_delete_on_termination –load_balancer_name idc1-elb1 –private_ip_address 172.16.2.21
1. Launching instance: idc1-server1 2. Creating tag as instance name: {"Name": idc1-server1} Instance state: pending Instance state: running 3. Creating secondary volume for instance: idc1-server1 as gp2 10G Volume status: available 4. Attaching volume: vol-4ba6a54c to instance: idc1-server1 as device: /dev/sdf 5. Adding instance: idc1-server1 to ELB: idc1-elb1
$ ./awscli.py –clone –region us-west-1 –src_instance_name idc1-server1 –dest_instance_name idc1-server2
1. Launching instance: idc1-server2 2. Creating tag as instance name: {"Name": idc1-server2} Instance state: pending Instance state: running 3. Creating secondary volume for instance: idc1-server2 as gp2 10G Volume status: available 4. Attaching volume: vol-5b61635c to instance: idc1-server2 as device: /dev/sdf 5. Adding instance: idc1-server2 to ELB: idc1-elb1
$ ./awscli.py –terminate –region us-west-1 –instance_name idc1-server2
Terminating instance: idc1-server2 id: i-86976d62 Instance state: shutting-down Instance state: shutting-down Instance state: terminated
转载请注明:爱开源 » AWS自动化运维脚本