python 脚本使用boto3启动类似于给定实例的实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 脚本使用boto3启动类似于给定实例的实例相关的知识,希望对你有一定的参考价值。

import boto3

# Method has functions 
#   - to list all instance given a tag name
#   - to list all instance
#   - to launch a instance similar to a given instance
#   - Listing properties of an instances
class Ec2Instance:

    def __init__(self):
        self.ec2 = boto3.resource('ec2')

    def get_instance_by_tag_name(self, value):
        filter = [{
            'Name': 'tag:Name',
            'Values': [value]
        }]
        instances = self.__filter_instances(filter)
        return instances[0]

    def get_instances_by_tag_role(self, value):
        filter = [{
            'Name': 'tag:Role',
            'Values': [value]
        }]
        return self.__filter_instances(filter)

    def get_instances_by_tag_state(self, value):
        print value
        filter = [{
            'Name': 'instance-state-name',
            'Values': [value]
        }]
        return self.__filter_instances(filter)

    def get_all_instances(self):
        filter = []
        return self.__filter_instances(filter)

    def list_instance_property(self, ec2_instance):
        volume_details = []
        for volume in instance.volumes.all():
            volume_details.append({"size": volume.size,
                                   "type": volume.volume_type,
                                   "iops": volume.iops})
        # security_groups =          all_sg_ids = [sg['GroupId'] for sg in ec2_instance.security_groups]

        return {
            "tags": [self.list_all_tags(ec2_instance)],
            "instance_type": ec2_instance.instance_type,
            "instance_state": instance.state['Name'],
            "image_type": ec2_instance.image,
            "subnet": ec2_instance.subnet_id,
            "key_name": ec2_instance.key_name,
            "key_pair": ec2_instance.key_pair,
            "volume_details": volume_details,
            "security_groups": ec2_instance.security_groups
        }

    def list_all_tags(self, ec2_instance):
        tags = {}
        if ec2_instance.tags is None:
            return []
        for tag in ec2_instance.tags:
            key = tag['Key']
            tags[key] = tag['Value']
        return tags

    def launch_instance_similar(self, ec2_instance, name, role=None):
        instance = self.ec2.create_instances(ImageId=ec2_instance.image.id,
                                             MinCount=1,
                                             MaxCount=1,
                                             KeyName=ec2_instance.key_name,
                                             InstanceType='t2.micro',
                                             SubnetId=ec2_instance.subnet.id,
                                             SecurityGroupIds=[
                                                 sg['GroupId'] for sg in ec2_instance.security_groups],
                                             BlockDeviceMappings=[
                                                 {"DeviceName": "/dev/sda1", "Ebs": {"VolumeSize": 50}}]
                                             )
        instance[0].wait_until_running()
        instance[0].create_tags(
            Tags=[{
                'Key': 'Name',
                'Value': name
            }
            ])
        instance[0].create_tags(
            Tags=[{
                'Key': 'Role',
                'Value': role
            }
            ])
        return instance[0][id]

    def __filter_instances(self, filter):
        instances = []
        for instance in self.ec2.instances.filter(
                Filters=filter):
            instances.append(instance)
        print instances
        return instances


ec2_instance = Ec2Instance()
instance = ec2_instance.get_instance_by_tag_name('Bastion Host')
print ec2_instance.launch_instance_similar(instance, 'Script-Server', role='script-sever')

以上是关于python 脚本使用boto3启动类似于给定实例的实例的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 boto3 在集群中启动 EC2 实例

使用Python boto3上传Windows EC2实例中的文件至S3存储桶中

Boto3 get_waiter 不会正确停止和启动实例

jar 文件的参数不正确 - 使用 Boto3 启动 EMR 集群

如何从类似于 pip 或 howdoi 命令的任何终端实例将 python 脚本作为全局命令运行?

python 使用Python和Boto3获取具有实例ID的实例标记信息