Finding all Amazon AWS Instances That Do Not Have a Certain Tag

前端 未结 9 2021
借酒劲吻你
借酒劲吻你 2020-12-23 12:09

I\'m trying to use the Amazon AWS Command Line Tools to find all instances that do not have a specified tag.

Finding all instances WITH a tag is simple enough, e.g.<

9条回答
  •  一生所求
    2020-12-23 12:34

    I too was totally shocked by how difficult this is to do via the CLI. I liked user2616321's answer, but I was having a little trouble making it output the exact fields I wanted per instance. After spending a while messing around and failing with JMESPath in the query syntax, I ended up just making a little ruby script to do this. In case anyone wants to save a few minutes writing one of their own, here it is:

    #!/usr/bin/env ruby
    require 'json'
    
    # We'll output any instance that doesn't contain all of these tags
    desired_tags = if ARGV.empty?
                     %w(Name)
                   else
                     ARGV
                   end
    
    # Put the keys we want to output per instance/reservation here
    reservation_keys = %w(OwnerId RequesterId)
    instance_keys = %w(Tags InstanceId InstanceType PublicDnsName LaunchTime PrivateIpAddress KeyName) 
    instances_without_tags = []
    
    # Just use CLI here to avoid AWS dependencies
    reservations = JSON.parse(
      `aws ec2 describe-instances`
    )["Reservations"]
    
    # A reservation is a single call to spin up instances. You could potentially
    # have more than one instance in a reservation, but often only one is
    # spun up at a time, meaning there is a single instance per reservation.
    reservations.each do |reservation|
      reservation["Instances"].each do |instance|
        # Filter instances without the desired tags
        tag_keys = instance["Tags"].map { |t| t["Key"] }
        unless (tag_keys & desired_tags).length == desired_tags.length
          instances_without_tags << 
            reservation.select { |k| reservation_keys.include?(k) }.
              merge(instance.select { |k| instance_keys.include?(k) })
        end
      end
    end
    
    puts JSON.pretty_generate(instances_without_tags)
    

提交回复
热议问题