[Cialug] Python Question

Jeffrey Ollie jeff at ocjtech.us
Thu Sep 14 03:40:20 UTC 2023


Yeah, the Lambda runtime (e.g. Python code written by Amazon) takes the raw
string that the underlying Lambda system passes to your function and
decodes it as JSON before invoking your code. It doesn't _have_ to end up
being a Python dictionary so robust code would do some checking of what the
object actually is to avoid errors.

I only played with lambdas for a little while a long time ago but I'm sure
that there's a way to build a Lambda without using any Amazon code - for
example if you wanted to write your Lambda in a language that Amazon
doesn't provide a runtime for, although that appears to be relatively
undocumented.

On Wed, Sep 13, 2023 at 2:41 PM Todd Walton <tdwalton at gmail.com> wrote:

> 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
> >
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>


-- 
Jeff Ollie
The majestik møøse is one of the mäni interesting furry animals in Sweden.


More information about the Cialug mailing list