[Cialug] Python Question
Shane Nehring
shane at ntoast.com
Wed Sep 13 14:29:56 UTC 2023
My guess is whatever is calling the lambda_handler method is passing event
as a python dictionary.
On Wed, Sep 13, 2023, 09:26 Todd Walton <tdwalton at gmail.com> wrote:
> 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
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>
More information about the Cialug
mailing list