2016-06-28 58 views
1

一定的标签我使用这个脚本由mlapida张贴在这里: https://gist.github.com/mlapida/1917b5db84b76b1d1d55#file-ec2-stopped-tagged-lambda-py关机EC2实例没有使用Python

通过mlapida该脚本的什么,我需要相反的,我没那么熟悉与Python知道如何重组它来完成这项工作。我需要关闭所有没有标识它们的特殊标签的EC2实例。

逻辑将是: 1)确定所有正在运行的实例 2.)剥去出从该列表中具有特殊标签 3.任何实例)的过程实例的剩余列表被关闭

任何帮助是极大的赞赏。

回答

1

这是应该做的伎俩:

# open connection to ec2 
conn = get_ec2_conn() 
# get a list of all instances 
all_instances = conn.get_all_instances() 
# get instances with filter of running + with tag `Name` 
instances = conn.get_all_instances(filters={'tag-key': 'Name', 'instance-state-name': 'running'}) 
# make a list of filtered instances IDs `[i.id for i in instances]` 
# Filter from all instances the instance that are not in the filtered list 
instances_to_delete = [to_del for to_del in all_instances if to_del.id not in [i.id for i in instances]] 
# run over your `instances_to_delete` list and terminate each one of them 
for instance in instances_to_delete: 
    conn.stop_instances(instance.id) 

而且在boto3:

# open connection to ec2 
conn = get_ec2_conn() 
# get a list of all instances 
all_instances = [i for i in conn.instances.all()] 
# get instances with filter of running + with tag `Name` 
instances = [i for i in conn.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}, {'Name':'tag-key', 'Values':['Name']}])] 
# make a list of filtered instances IDs `[i.id for i in instances]` 
# Filter from all instances the instance that are not in the filtered list 
instances_to_delete = [to_del for to_del in all_instances if to_del.id not in [i.id for i in instances]] 
# run over your `instances_to_delete` list and terminate each one of them 
for instance in instances_to_delete: 
    instance.stop() 
+0

我正在使用仅支持boto3的AWS Lambda,您会如何推荐将其写入boto3和Python 2.7的上下文中? –

+0

看看我的更新,这是一种哈克,但这是我与boto3的第一枪,但它的工作:) @TimHolm –

+0

另外,我会尝试阅读亚马逊的文档http://boto3.readthedocs.io/en/latest/guide/migrationec2的.html#检查乜情况下,是运行的 –

0

随着信用mlapida和Yonatan,我有关闭不具备所有实例工作脚本一个标签“AutoOff”和任何具有“AutoOff”设置为“False”的实例都会保留,希望这可以帮助那里的人。

import boto3 
import logging 

#setup simple logging for INFO 
logger = logging.getLogger() 
logger.setLevel(logging.INFO) 

#define the connection 
ec2 = boto3.resource('ec2') 
# open connection to ec2 
#conn = get_ec2_conn() 

# get a list of all instances 
all_instances = [i for i in ec2.instances.all()] 

def lambda_handler(event, context): 

    #instances = ec2.instances.filter(Filters=filters) 
    # get instances with filter of running + with tag `Name` 
    instances = [i for i in ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}, {'Name':'tag:AutoOff', 'Values':['False']}])] 

    # make a list of filtered instances IDs `[i.id for i in instances]` 
    # Filter from all instances the instance that are not in the filtered list 
    instances_to_delete = [to_del for to_del in all_instances if to_del.id not in [i.id for i in instances]] 

    # run over your `instances_to_delete` list and terminate each one of them 
    for instance in instances_to_delete: 
     instance.stop()