2016-06-28 84 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()