title: Lambda function to search for all running EC2 instances, add tag 'Name' if not present, and then add tags to it's associated volumes?
body: <p>I have some code that works to add tags to all pending and running instances, and then searches for their associated volumes, and adds tags to those as well. However, if I spin up a new instance with NO tags at all, I get a KeyError and it does not work. What I'm looking to do is:</p>
  <ol>
  <li>Search for all running and pending EC2's</li>
  <li>If 'Name' tag does not exist on the instance, add a key: 'Name' tag as well as key: 'test_key', value: 'test_value'.</li>
  <li>If key: 'Name' tag does exist, just add the key: 'test_key' , value: 'test_value' to EC2's</li>
  <li>Add tags to all volumes associated with running/pending instances</li>
  </ol>
  <p>Here is the code:</p>
  <pre><code>#!/usr/bin/env python
  
  import boto3
  
  
  ec2 = boto3.resource('ec2')
  ec2client = boto3.client('ec2')
  
  
  #-----Define Lambda function-----#
  def lambda_handler(event, context):
  
  #-----Check&amp; filter Instances which  Instance State is running-----#
      instances = ec2client.describe_instances(
          Filters=[{
              'Name': 'instance-state-name',
              'Values': ['pending', 'running']
          }]
          )
  
  #-----Define dictionary to store Tag Key &amp; value------#
      dict={}
  
      mytags = [{
          &quot;Key&quot; : &quot;test_key&quot;, &quot;Value&quot; : &quot;test_value&quot;
          }]
  
  #-----Store Key &amp; Value of Instance ------#
      for reservation in instances['Reservations']:
          for instance in reservation['Instances']:
              ec2.create_tags(
                  Resources = [instance[&quot;InstanceId&quot;] ],
                  Tags = mytags)
              for tag in instance['Tags']:
                  if tag['Key'] == 'Name':
                      print ( instance['InstanceId'],tag['Value'])
                      #ids.append(( instance['InstanceId'],tag['Value']))
                      dict[instance['InstanceId']]= tag['Value']
                  
  #-----Store Key &amp; Value with attached instance ID of all volumes ------#     
      volumes = ec2.volumes.all() 
      for volume in volumes:
  
  #-----compare dictionary value Key:InstanceID and volume attached Key:InstanceID ------#     
          for a in volume.attachments:
              for key, value in dict.items():
  
  
  #-----Add tags to volumes ------#     
  
                  if a['InstanceId'] == key:
                       volume.create_tags(Tags =[{
                          'Key': 'test_key', 'Value': 'test_value'}
                      ])
  </code></pre>
  <p>It works fine if there is a 'Name' tag on the new instance, but does not work if the 'Name' tag isn't there. It throws the following error:</p>
  <p>{
  &quot;errorMessage&quot;: &quot;'Tags'&quot;,
  &quot;errorType&quot;: &quot;KeyError&quot;,
  &quot;stackTrace&quot;: [
  &quot;  File &quot;/var/task/lambda_function.py&quot;, line 38, in lambda_handler\n    for tag in instance['Tags']:\n&quot;
  ]
  }</p>
  <p>I think this is because it's searching for a tag but the new instance has no tags so it errors.</p>