title: Python3 - Parsing AWS instance tags from json. Experiencing erradic behavior
body: <p>My task: Parse output from "aws ec2 describe-instances" json output to gather various instance details including the "Name" tag assigned to the instance. </p>
  
  <p>My Code excerpt: </p>
  
  <pre><code># Query AWS ec2 for instance information
          my_aws_instances = subprocess.check_output("/home/XXXXX/.local/bin/aws ec2 describe-instances --region %s --profile %s" % (region, current_profile), shell=True)
  
          # Convert AWS json to python dictionary
          my_instance_dict = json.loads(my_aws_instances)
  
          # Pre-enter the 'Reservations' top level dictionary to make 'if' statement below cleaner.
          my_instances = my_instance_dict['Reservations']
  
          if my_instances:
  
              for my_instance in my_instances:
  
                  if 'PublicIpAddress' in my_instance['Instances'][0]:
                      public_ip = my_instance['Instances'][0]['PublicIpAddress']
                  else:
                      public_ip = "None"
  
                  if 'PrivateIpAddress' in my_instance['Instances'][0]:
                      private_ip = my_instance['Instances'][0]['PrivateIpAddress']
                  else:
                      private_ip = "None"
  
                  if 'Tags' in my_instance['Instances'][0]:
                      tag_list = my_instance['Instances'][0]['Tags']
  
                      for tag in tag_list:
                          my_tag = tag.get('Key')
  
                          if my_tag == "Name":
                              instance_name = tag.get('Value')
                          else:
                              instance_name = "None"
  
                  state         = my_instance['Instances'][0]['State']['Name']
                  instance_id   = my_instance['Instances'][0]['InstanceId']
                  instance_type = my_instance['Instances'][0]['InstanceType']
  </code></pre>
  
  <p>Here's an example of what is contained in the "tag" variable as it loops. This is a python dictionary:</p>
  
  <pre><code>{'Value': 'server_name01.domain.com', 'Key': 'Name'}
  </code></pre>
  
  <p>If it helps, this is the raw json for the instance tags:</p>
  
  <pre><code> "Tags": [
                          {
                              "Value": "migration test",
                              "Key": "Name"
                          }
                      ],
  </code></pre>
  
  <p>Everything is working except for the "Tags" section which works in some cases and doesn't work in others, even though the "Name" value which I'm testing for exists in all cases. In other words I'm getting "None" as a result on some instances that do indeed have a "Name" tag and a name. 
  I've ruled out problems with the server names themselves i.e. spaces and special chars screwing with the result. 
  I've also tried to make sure that python is looking for exactly "Name" and no other variations.
  I'm perplexed at this point and any help would be appreciated. </p>
  
  <p>Thanks in advance</p>