Invoking Lambda with Kinesis locally with AWS SAM

Portuguese version: https://medium.com/@gusiol/disparando-lambda-atrav%C3%A9s-do-kinesis-localmente-com-aws-sam-e535e40e795d

SAM (AWS Serverless Application Model) is an open-source framework build on top of AWS infrastructure as code (IaC) service CloudFormation specific for serverless applications. One of its advantages is providing a way to run your serverless applications locally for debugging and testing without charges. In this tutorial I will show how to use SAM to invoke a Lambda function locally with a custom Kinesis Data Stream event.

SAM Template

SAM template

In this template I’m specifying a MyLambda resource of type AWS::Serverless::Function which references a Lambda function. This resource has many properties that define its behavior. In this case a main file on the bin path written in Go with 128 Mb of memory.

Lambda Function Code

Lambda code

The above code is the Lambda function used as example in this tutorial. It basically receives a Kinesis event and print the record data to the console. Full repository here.

Run Lambda locally

sam local invoke MyLambda

This command invokes the function without an event. To add an event trigger you need to specify the path to a JSON file with the event to the --event (-e) parameter.

sam local invoke MyLambda -e event.json

Each Lambda function event source (Kinesis, SQS, SNS…) has its own structure. You can check the documentation to find out the structure of the desired event source or generate a sample event.

Generating Events

sam local generate-event kinesis get-records

Here is the output of the above command:

Output of generate-event command

This event can contain many Records where the content is stored in the data property as a base64-encoded string (https://docs.aws.amazon.com/kinesis/latest/APIReference/API_Record.html). This output can be directly passed to the invoke function.

sam local generate-event kinesis get-records | sam local invoke -e — MyLambda

The -e value - inputs JSON via stdin

Bellow is the invocation output in the console showing the default data decoded value “Hello, this is a test 123.”.

Lambda invocation with default data

Generating Events with Custom Data

sam local generate-event kinesis get-records --data “My custom content”

Generally the data is a JSON string but using this format in the command line is not productive. My solution is to minify and stringify the content of the JSON event file using the popular command line JSON processor jq and pass the output to the --data parameter.

sam local generate-event kinesis get-records --data “$(jq -c < event.json)” | sam local invoke -e - MyLambda

This way you can quickly make changes in the JSON file and rerun the command with the updated event data. Bellow is the invocation output in the console showing the data decoded value as a JSON string.

Lambda invocation with custom JSON data

Extra

Makefile code

Conclusion

Sou o vice-treco do sub-troço. GitHub: https://github.com/guizoxxv

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store