使用 Python 关闭没有特定标签的 EC2 实例

Shutdown EC2 instances that do not have a certain tag using Python(使用 Python 关闭没有特定标签的 EC2 实例)
本文介绍了使用 Python 关闭没有特定标签的 EC2 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mlapida 发布的这个脚本:https://gist.github.com/mlapida/1917b5db84b76b1d1d55#file-ec2-stopped-tagged-lambda-py

I'm using this script by mlapida posted here: https://gist.github.com/mlapida/1917b5db84b76b1d1d55#file-ec2-stopped-tagged-lambda-py

mlapida 的脚本与我需要的相反,我对 Python 不太熟悉,不知道如何重组它以使其工作.我需要关闭所有没有标识它们的特殊标签的 EC2 实例.

The script by mlapida does the opposite of what I need, I'm not that familiar with Python to know how to restructure it to make this work. I need to shutdown all EC2 instances that do not have a special tag identifying them.

逻辑是:1.) 识别所有正在运行的实例2.) 从该列表中删除任何具有特殊标签的实例3.) 处理剩余要关闭的实例列表

The logic would be: 1.) Identify all running instances 2.) Strip out any instances from that list that have the special tag 3.) Process the remaining list of instances to be shutdown

非常感谢任何帮助.

推荐答案

这应该可以解决问题:

# 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()

这篇关于使用 Python 关闭没有特定标签的 EC2 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)