查找不等于 X 的 EC2 实例 - AWS CLI

Find EC2 instances that not equal to X - AWS CLI

我正在寻找不等于平台“Windows”的实例并用特定标签标记它们。

现在我有这个脚本来标记等于平台“Windows”的实例:

import boto3

ec2 = boto3.client('ec2')
response = ec2.describe_instances(Filters=[{'Name' : 'platform', 'Values' : ['windows']}])
instances = response['Reservations']

for each_res in response['Reservations']:
    for each_inst in each_res['Instances']:
        for instance in instances:
            response = ec2.create_tags(
                Resources=[each_inst['InstanceId']],
                Tags = [
                    {
                        'Key' : 'test',
                        'Value': 'test01'
                    }
                ]
            )

我需要帮助向此脚本添加一个块,该块将仅向不等于平台“Windows”的 EC2 实例添加另一个标签。

谢谢

只需删除过滤器并遍历所有实例,然后在代码中在 platform 键上添加一个 if 条件。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto3

ec2 = boto3.client("ec2", region_name="eu-central-1")
response = ec2.describe_instances()
instances = response["Reservations"]

for each_res in response["Reservations"]:
    for each_inst in each_res["Instances"]:
        platform = each_inst.get('Plaform')
        instance_id  = each_inst.get('InstanceId')
        if platform == 'Windows':
            response = ec2.create_tags(
                Resources=[instance_id],
                    Tags = [
                        {
                            'Key' : 'test',
                            'Value': 'test01'
                        }
                    ]
                )
        else:
            print(f'found non windows intance: {instance_id}')
            response = ec2.create_tags(
                Resources=[instance_id],
                    Tags = [
                        {
                            'Key' : 'nonwindow',
                            'Value': 'nonwindowvalue'
                        }
                    ]
                )

根据 API docs

The value is Windows for Windows instances; otherwise blank.

我测试的代码工作正常:

$ python3 describe_instances.py
found non windows intance: i-0ba1a62801c895

describe_instances

describe_instnaces 调用收到的响应结构

{
    'Reservations': [
        {
            'Groups': [
                {
                    'GroupName': 'string',
                    'GroupId': 'string'
                },
            ],
            'Instances': [
                {
                    'AmiLaunchIndex': 123,
                    'ImageId': 'string',
                    'InstanceId': 'string',
                     ....
                    'Platform': 'Windows',
                    'PrivateDnsName': 'string',
                    'PrivateIpAddress': 'string',
                    'ProductCodes': [
                    ....

试试这个。为我工作。此外,运行 create_tags 在 for 循环内,您正在为每个资源执行一个 API。而 create_tags 支持多个资源作为输入。参考:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_tags

import boto3


#Initialize an empty list to store non windows instance IDs.

list_nonwindows = []

ec2 = boto3.client("ec2", region_name="us-east-1")
response = ec2.describe_instances()
instances = response["Reservations"]
for each_res in response["Reservations"]:
    for each_inst in each_res["Instances"]:
        if each_inst.get('Platform') == None:
          instance_s = each_inst.get('InstanceId')
          list_nonwindows.append(instance_s)

response = ec2.create_tags(
    Resources=list_nonwindows,
        Tags = [
            {
                'Key' : 'test',
                'Value': 'test01'
            }
        ]
    )