One of the pain points of developing against lambd...
# general
s
One of the pain points of developing against lambdas is knowing the event payload format. Is there an opportunity for the SST Console to help with this?
r
it’s a WIP but handy
s
Yes! I was just googling around trying to find this very site
f
do u want to know the event payload when you need to test a function real quick?
I guess my question is when do u find ur self needing to build an event payload?
s
I have a lambda that is invoked via an integration with Kafka (MSK), and I'm constantly forgetting the shape of that payload
when I open the SST console and want to invoke that function manually (it's easier than setting up the full MSK integration), I need to go digging around for the format of the payload
I imagine the same would be true for SQS, SNS, etc.
I was also trying out the AWS Lambda Powertools logger. Apparently it has features that extract data from the context, which I was not sending to the handler in my SST console request
f
Gotcha! It seems being able to load up a previous saved payload would help.
Apparently it has features that extract data from the context, which I was not sending to the handler in my SST console request
By
context
, do you mean you needed to access data from the 2nd argument of the Lambda handler, like log group arn, memory setting, etc? ie.
Copy code
handler(event, context) { ... }
s
Thats right, Frank. Although, now that I think of if, this is a separate issue than the format of the event payload. I provide the event payload via the SST console, so it's my responsibility to format the event as my Lambda function expects (e.g. a batched SQS event, an APIGateway integration, etc). However, the context should be provided directly by Lambda. This probably isn't something SST console should be involved with
Although, I'm not exactly sure how SST console is invoking my Lambda when I click on the API route
f
SST console is just calling the Lambda AWS SDK with
InvocationType
being
Event
.
Which field in the context r u looking for?
I will check if SST is not passing that down to ur local, or is it a “bug” as mentioned in the tweet.
s
I was attempting to use the AWS Lambda Powertools for Python Logger module. I can decorate my handler as follows
Copy code
@logger.inject_lambda_context
def handler(event, context):
   // do stuff here
and the module will log elements from the context to cloudwatch. The fields it's expecting are documented here. This isn't blocking my work, it's just a functionality I was testing out. When it didn't work, I simply removed the
@logger.inject_lambda_context
decorator, but it made me wonder how SST console was invoking the API
f
According to the doc, it’s looking for these context variables
But according to the AWS doc, , apart from
function_name
, other context names don’t match up.
Oh wait.. i think in the former screenshot, those are context names that are being logged, not what are read from the
context
.
Can I see the error message you get when running locally with the
@logger.inject_lambda_context
 decorator enabled?
I wonder if we can figure out which context variables are missing.
s
The error was around 'function_name'
here's the full output
Copy code
==========================
 Starting Live Lambda Dev
==========================

SST Console: <https://console.serverless-stack.com/kas/sgeoghegan/local>
Debug session started. Listening for requests...
eeacd8f6-1ac8-4bf6-9aa1-7685e6fd38ad REQUEST sgeoghegan-kas-kas-apiLambdaGET2D5CB7A7-spZCO4MI9530 [functions/index.handler] invoked
Traceback (most recent call last):
  File "/Users/sgeoghegan/dev/knock-attribution-service/node_modules/@serverless-stack/core/src/runtime/shells/bootstrap.py", line 77, in <module>
    result = handler(event, context)
  File "/Users/sgeoghegan/dev/knock-attribution-service/src/.venv/lib/python3.9/site-packages/aws_lambda_powertools/logging/logger.py", line 339, in decorate
    lambda_context = build_lambda_context_model(context)
  File "/Users/sgeoghegan/dev/knock-attribution-service/src/.venv/lib/python3.9/site-packages/aws_lambda_powertools/logging/lambda_context.py", line 52, in build_lambda_context_model
    "function_name": context.function_name,
AttributeError: 'Context' object has no attribute 'function_name'
eeacd8f6-1ac8-4bf6-9aa1-7685e6fd38ad ERROR AttributeError: 'Context' object has no attribute 'function_name'
and the function
Copy code
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.logging.logger import Logger

logger = Logger(service="payment")
@logger.inject_lambda_context
def handler(event, context: LambdaContext):
    return {
        "statusCode": 200,
        "headers": {"Content-Type": "text/plain"},
        "body": "You Lambda is live!"
    }
f
This is fixed in v0.66.2. Added
function_name
and some other missing contexts.
s
This works great Frank, thank you!