[Cialug] Python Question

Todd Walton tdwalton at gmail.com
Wed Sep 13 19:41:03 UTC 2023


Okay, from that last link:

https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html

I see that it says,"the Lambda runtime converts the event to an object". I
felt like *something* had to be converting the string to an object. It's
the runtime itself, apparently.

--
Todd


On Wed, Sep 13, 2023 at 11:07 AM Shane Nehring <shane at ntoast.com> wrote:

> Yeah, per https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html
> it's usually a dict. Barry brings up a good point though, unless something
> upstream of your code is doing some checking you'll probably want to add
> that in your handler for whatever data decodes to. I recently fell in love
> with pydantic when working with fastapi, you could look into that.
>
> On Wed, Sep 13, 2023 at 9:36 AM Barry Von Ahsen <vonahsen at gmail.com>
> wrote:
>
> > 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
> >
> > _______________________________________________
> > Cialug mailing list
> > Cialug at cialug.org
> > https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
> >
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>


More information about the Cialug mailing list