title: Parsing Dictionary Response in AWS Lambda
body: <p>I'm attempting to create an AWS Lambda function that consumes CloudTrail events via  an S3 trigger. This function will alert on the deletion of CloudWatch logs. The events:  </p>
  
  <blockquote>
    <p>'eventSource': 'logs.amazonaws.com'</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>'eventName': 'DeleteLogStream'</p>
  </blockquote>
  
  <p>need to be found together as the same event. I have the data in my event but I am unable to capture and print it.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import boto3
  import gzip
  import json
  
  SNS_TOPIC = "&lt;SNS TOPIC ARN&gt;"
  SNS_SUBJECT = "&lt;SUBJECT&gt;"
  
  
  s3_client = boto3.client('s3')
  sns_client = boto3.client('sns')
  
  
  def handler(event, context):
      for record in event['Records']:
          bucket = record['s3']['bucket']['name']
          key = record['s3']['object']['key']
  
      
      # Fetch logs from S3
      s3_object = s3_client.get_object(
          Bucket=bucket,
          Key=key,
      )
  
      # Extract file and metadata from gzipped S3 object
      with gzip.open(s3_object['Body'], 'rb') as binaryObj:
          binaryContent = binaryObj.read()
      
      # Convert from binary data to text
      raw_logs = binaryContent.decode()
      
      # Change text into a dictionary
      dict_logs = json.loads(raw_logs)
      
  
      # Make sure json_logs key 'Records' exists
      if 'Records' in dict_logs.keys():
      
          print("Printing Dictionary Content: {} \n\n".format(dict_logs))
          
   if dict_logs['Records'][0]['eventSource'] == 'logs.amazonaws.com' and dict_logs['Records'][0]['eventName'] == 'DeleteLogStream':
     print("Found DeleteLogStream event from logs.amazonaws.com!")
    
          # Print Key-Value pair for each item found
          for key, value in dict_logs['Records'][0].items():
              # Account for values that are also dictionaries
              if isinstance(value, dict):
                  print("Parent Key: {}".format(key))
                  for k, v in value.items():
                      print("Subdict Key: {}".format(k))
                      print("Subdict Value: {}".format(v))
                  continue
              else:
                  print("Key: {}".format(key))
                  print("Value: {}".format(value))
  
          
          alert_message = "The following log was found: &lt;extracted log contents here&gt;"
          
          # Publish message to SNS topic
          sns_response = sns_client.publish(
              TopicArn=SNS_TOPIC,
              Message=alert_message,
              Subject=SNS_SUBJECT,
              MessageStructure='string',
          )
  
      else:
          print("Records key not found")</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is the result I get:
  <a href="https://i.stack.imgur.com/lg1ce.png" rel="nofollow noreferrer">Result from Code</a></p>
  
  <p>My code prints the Keys/Values for debugging purposes. Any ideas why the 'DeleteLogStream' and 'logs.amazonaws.com' values aren't parsing out?</p>
  
  <p>Sample json event below:
  <a href="https://raw.githubusercontent.com/danielkowalski1/general-scripts/master/sampleevent" rel="nofollow noreferrer">https://raw.githubusercontent.com/danielkowalski1/general-scripts/master/sampleevent</a></p>