[Cialug] Python Question

Todd Walton tdwalton at gmail.com
Wed Sep 13 14:25:23 UTC 2023


I have a Python question. I've written (copy/pasted really) a bit of Python
code to run in AWS Lambda. It's triggered by AWS CloudWatch Logs. The input
from CloudWatch is a string like this:

{
  "awslogs": {
    "data": "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUg..."
  }
}

And the relevant bit of Python code looks like this:

def lambda_handler(event, context):
  encoded_zipped_data = event['awslogs']['data']
  zipped_data = base64.b64decode(encoded_zipped_data)
  data = gzip.decompress(zipped_data)
  response =
client.publish(TopicArn='arn:aws:sns:us-east-1:xxxxxx:log-error-topic',Message=json.dumps(data.decode('utf-8'),
indent=3))

So... I understand that the input string is fed into the Python function as
the "event" argument, and then the first line -- the "encoded_zipped_data
=" part -- reads that string and accesses the "data" element.

But how does Python know how to treat that string as an object with
sub-elements? I would expect that I would have to convert the string to
JSON first. But Python just knows? What am I not understanding here?

--
Todd


More information about the Cialug mailing list