1. <i id='XZ3Ie'><tr id='XZ3Ie'><dt id='XZ3Ie'><q id='XZ3Ie'><span id='XZ3Ie'><b id='XZ3Ie'><form id='XZ3Ie'><ins id='XZ3Ie'></ins><ul id='XZ3Ie'></ul><sub id='XZ3Ie'></sub></form><legend id='XZ3Ie'></legend><bdo id='XZ3Ie'><pre id='XZ3Ie'><center id='XZ3Ie'></center></pre></bdo></b><th id='XZ3Ie'></th></span></q></dt></tr></i><div id='XZ3Ie'><tfoot id='XZ3Ie'></tfoot><dl id='XZ3Ie'><fieldset id='XZ3Ie'></fieldset></dl></div>
      • <bdo id='XZ3Ie'></bdo><ul id='XZ3Ie'></ul>

      <small id='XZ3Ie'></small><noframes id='XZ3Ie'>

      <tfoot id='XZ3Ie'></tfoot>
    1. <legend id='XZ3Ie'><style id='XZ3Ie'><dir id='XZ3Ie'><q id='XZ3Ie'></q></dir></style></legend>

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

      时间:2023-09-03

      <i id='VI1Hd'><tr id='VI1Hd'><dt id='VI1Hd'><q id='VI1Hd'><span id='VI1Hd'><b id='VI1Hd'><form id='VI1Hd'><ins id='VI1Hd'></ins><ul id='VI1Hd'></ul><sub id='VI1Hd'></sub></form><legend id='VI1Hd'></legend><bdo id='VI1Hd'><pre id='VI1Hd'><center id='VI1Hd'></center></pre></bdo></b><th id='VI1Hd'></th></span></q></dt></tr></i><div id='VI1Hd'><tfoot id='VI1Hd'></tfoot><dl id='VI1Hd'><fieldset id='VI1Hd'></fieldset></dl></div>
      1. <tfoot id='VI1Hd'></tfoot>

        <legend id='VI1Hd'><style id='VI1Hd'><dir id='VI1Hd'><q id='VI1Hd'></q></dir></style></legend>

          <small id='VI1Hd'></small><noframes id='VI1Hd'>

                <bdo id='VI1Hd'></bdo><ul id='VI1Hd'></ul>

                  <tbody id='VI1Hd'></tbody>
                本文介绍了使用 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 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 BeautifulSoup 中使用字典解析脚本标签 下一篇:如何从 HTML 字符串中获取美丽汤中的开始和结束标记?

                相关文章

                1. <tfoot id='Cg70r'></tfoot>
                    <bdo id='Cg70r'></bdo><ul id='Cg70r'></ul>
                2. <legend id='Cg70r'><style id='Cg70r'><dir id='Cg70r'><q id='Cg70r'></q></dir></style></legend>

                    <small id='Cg70r'></small><noframes id='Cg70r'>

                  1. <i id='Cg70r'><tr id='Cg70r'><dt id='Cg70r'><q id='Cg70r'><span id='Cg70r'><b id='Cg70r'><form id='Cg70r'><ins id='Cg70r'></ins><ul id='Cg70r'></ul><sub id='Cg70r'></sub></form><legend id='Cg70r'></legend><bdo id='Cg70r'><pre id='Cg70r'><center id='Cg70r'></center></pre></bdo></b><th id='Cg70r'></th></span></q></dt></tr></i><div id='Cg70r'><tfoot id='Cg70r'></tfoot><dl id='Cg70r'><fieldset id='Cg70r'></fieldset></dl></div>