[Cialug] Python Question

Barry Von Ahsen vonahsen at gmail.com
Wed Sep 13 14:35:14 UTC 2023


The event parameter is defined as an event object, which is a JSON document

https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-concepts.html#gettingstarted-concepts-event

I'd _hope_ there were a bit more error checking to verify that fact, or a more strongly-typed method declaration, but that's just me :)


-barry




> On Sep 13, 2023, at 09:25, 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