加入收藏 | 设为首页 | 会员中心 | 我要投稿 航空爱好网 (https://www.dakongjun.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 网站设计 > 教程 > 正文

centos6中AWS自动化运维脚本分享

发布时间:2022-06-20 10:08:42 所属栏目:教程 来源:互联网
导读:背景介绍:目前项目中使用了大量的AWS EC2 Instances作为服务器,在自动化运维方面,我们之前一直使用的是AWS CLI命令行工具,然后在Shell脚本中调用. 最近我想通过脚本实现一个Clone的功能,模拟Web Console上的Launch More Like This来创建Instance,但在Shell脚
  背景介绍:目前项目中使用了大量的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
 
  相关示例:温馨提示:我的Blog页面默认没有采用宽屏模式,如果觉得下面的代码不太美观,可以点击右上角的“<>”切换到宽屏模式,代码如下:
 
  $ ./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  //phpfensi.com
  Terminating instance: idc1-server2 id: i-86976d62
  Instance state: shutting-down
  Instance state: shutting-down
  Instance state: terminated。
 

(编辑:航空爱好网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读