aws boto 停止 和 删除实例

停止实例

import boto.ec2

region = ""
aws_access_key_id = ""
aws_secret_access_key = ""

conn = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)

instance_list = []

conn.stop_instances(instance_ids=instance_list)

删除实例

import boto.ec2

region = ""
aws_access_key_id = ""
aws_secret_access_key = ""

conn = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)

instance_list = []

for instance in instance_list :
 conn.modify_instance_attribute(instance, "disableApiTermination", False)

conn.terminate_instances(instance_ids=instance_list)

aws volume 的查询、创建、挂载 和 卸载

做个集合,方面查阅,欢迎参考,哈哈哈。

 

查看  volume-id 

import boto.ec2

region = “ap-southeast-1”
aws_access_key_id = “”
aws_secret_access_key = “”

conn = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
reservations = conn.get_all_instances()
for res in reservations:
    for instance in res.instances:
        if ‘Name’ in instance.tags:
           volume_list = []
           for i in instance.block_device_mapping:
               volume_dict = {}
               volume_dict[“device”] = i
               volume_dict[“volume_id”] = instance.block_device_mapping.get(i).volume_id
               volume_list.append(volume_dict)

           print “%s (%s) [%s]:\n %s” % (instance.tags[‘Name’], instance.id, instance.state, volume_list)
        else:
           volume_list = []
           for i in instance.block_device_mapping:
               volume_dict = {}
               volume_dict[“device”] = i
               volume_dict[“volume_id”] = instance.block_device_mapping.get(i).volume_id
               volume_list.append(volume_dict)

           print “%s [%s]:\n %s” % (instance.id, instance.state, volume_list)
        print

 

创建volume

import boto.ec2

region = “ap-southeast-1”
aws_access_key_id = “”
aws_secret_access_key = “”

conn = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)

size = “126”
zone = “ap-southeast-1b”

volume_info = conn.create_volume(size, zone)
volume_id = volume_info.id

time_init = 0
time_total = 20
time_interval = 1
while time_init < time_total:
    volume_info.update()
    status = volume_info.status
    if status == ‘available’:
        print “{0}, {1}”.format(volume_id, status)
        break
    else:
        time.sleep(time_interval)
        time_init += time_interval

 

把一个 volume-id 挂载到 一个实例上

import boto.ec2

region = “ap-southeast-1”
aws_access_key_id = “”
aws_secret_access_key = “”

volume_id = “”
instance_id = “”
device = “”

conn = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
reservations = conn.get_all_instances()
curr_vol = conn.get_all_volumes([volume_id])[0]
#print curr_vol
#print curr_vol.zone
if curr_vol.status == ‘available’:
    conn.attach_volume(volume_id, instance_id, device)

 

把一个 volume-id 从一个实例上卸掉

import boto.ec2

region = “”
aws_access_key_id = “”
aws_secret_access_key = “”

instance_id = “”
volume_id = “”
device = “”

conn = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
reservations = conn.get_all_instances()
curr_vol = conn.get_all_volumes([volume_id])[0]
#print curr_vol
#print curr_vol.zone
if curr_vol.status == ‘in-use’:
    conn.detach_volume(volume_id, instance_id, device)

使用aws boto 创建 自定义 的实例

之前的笔记,觉得有价值,还是拿出来分享下。

 

参考下面的python 代码:

     import requests
     import time
     import boto.ec2
     from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
     from boto.ec2.networkinterface import NetworkInterfaceSpecification, NetworkInterfaceCollection

    block_device_map = BlockDeviceMapping()
    block_dev_type = BlockDeviceType()
    block_dev_type.snapshot_id = snap_id
    block_dev_type.delete_on_termination = False
    block_dev_type.size = volume_capacity
    block_device_map[‘/dev/sdb‘] = block_dev_type

    hostname = “”
    user_data= user_data.replace(“hostname=”,”hostname=%s” % hostname)

    network_interface = NetworkInterfaceSpecification(subnet_id=subnet_id, groups=[sg_id])
    network_interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(network_interface)

    conn = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
    reservation = conn.run_instances(ami_id,
        key_name=key_name,
        network_interfaces=network_interfaces,
        instance_type=instance_type,
        block_device_map=block_device_map,
        # security_groups=[security_groups],
        min_count=1,
        max_count=1,
        user_data=user_data,
        )

    instance = reservation.instances[0]

    time_init = 0
    time_total = 300
    time_interval = 5       
    while time_init < time_total:
        status = instance.update()   
        if status == ‘running’:
            instance.add_tag(“Name“,hostname)
            break
        else:
            time.sleep(time_interval)
            time_init += time_interval

    install_list[“instance_id”] = str(instance).split(“:”)[-1]
    install_list[“placement”] = instance.placement
    install_list[“status”] = instance.update()
    install_list[“hostname”] = hostname

1. ami_id 是制作的镜像,系统的基础配置已经在镜像里做了。

2. snap_id 是10G的一个snapshot ,我们把它挂载/home,用作数据盘,注意,block_dev_type.size 指定的值如果小于10,依然会创建10G的盘。

3. user_data 是通过http获取的脚本内容,我们把脚本里面的hostname值替换掉,实现不同机器不同主机名的目的(hostname 通过API从资产系统获得)。

4. 这里我把 “Name” 这个 tag 改成 主机名。

5. user_data 的内容如下:

#!/bin/bash

hostname=
hostname $hostname
sed -i “s/^HOSTNAME=.*/HOSTNAME=$hostname/g” /etc/sysconfig/network

mount /dev/sdb /home/
/sbin/blkid |egrep -v “vda” |sort -u -k1 |awk ‘{print $2″ /home/ ext4    noexec,nosuid,noatime   1 2″}’ >>/etc/fstab

echo “[`date`] personal post config script exec finished.” >>/var/log/message

 

问题:

1. 装完机器发现 /dev/sdb 或者 /dev/xvdb 只有 快照的大小,而不是指定的 volume_capacity
可以在装机之后执行下 resize2fs ,类似:
echo “resize2fs /dev/xvdb ;sed -i ‘/resize2fs/d’ /etc/rc.d/rc.local ” >>/etc/rc.d/rc.local
    reboot