What causes the "event" argument to be nil when a Lambda is triggered on an SNS event

Hello,

I followed the instructions here (SNS Events - Jets Ruby Serverless Framework) to create a subscription to a Lambda for an existing SNS topic. Everything was created perfectly the first time through. Publishing an event through the console and the CLI works fine. The Lambda is triggered and the logs are in CloudWatch.

The problem: The event is always nil. What causes the event to be nil? I’ve made sure the timeout is short, I’m adding the “default” variable, etc.

Thanks for any guidance.

FYI, I just tested with a generated SNS topic. The setup was perfect again. The Lambda is called when I publish an event. But, the event is nil again.

Just tested with DynamoDB. Streams are “on”. Lambda is triggered. Event is again nil.

If I set up the DynamoDB trigger myself outside of the Jets project with a Lambda created in the console, that works fine.

Tested it and getting the event payload :thinking:

Test SNS Event

Test CLI commands:

aws sns publish --topic-arn arn:aws:sns:us-west-2:112233445566:hello-topic --message '{"default": "test message1"}'
aws sns publish --topic-arn arn:aws:sns:us-west-2:112233445566:hello-topic --message '{"default": "test message2"}'

CloudWatch Logs:

# first request
2023-09-09 14:46:31 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa START RequestId: a777eec7-de7e-4275-a9a3-142d20abc504 Version: $LATEST
2023-09-09 14:46:31 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa dig event {"Records":[{"EventSource":"aws:sns","EventVersion":"1.0","EventSubscriptionArn":"arn:aws:sns:us-west-2:112233445566:hello-topic:b8fcbeba-6505-4383-bb88-b615b3294324","Sns":{"Type":"Notification","MessageId":"315282ac-41c3-51b7-9df7-dfdac644375c","TopicArn":"arn:aws:sns:us-west-2:112233445566:hello-topic","Subject":null,"Message":"{\"default\": \"test message1\"}","Timestamp":"2023-09-09T14:46:31.048Z","SignatureVersion":"1","Signature":"K+3NIpBmAUG5J1Fia952DdMytVUbwHvJLwm7wYVzrP5F9NXIEuKLxeCvjNq8CBxBhHeju6bu1E1cKmHfUOi+jBLT+H9x8wYJ11dr6OVLHt+Yw/IplBR+I4xK0klu+TaWyFYNSMVF3KMANMbRg8D/UUUGhKZQpEPLAyASBUI1bU1lkagsW1bG6jBsa48xn1oKEpPJG7Y32iDaZVBUIixRT31RIo3QEI/jPd+IDiDyJc5EmhHFMlaPnbceGhF0iYNZLcjvHj9E86yhlFgBjcm24QzcnjYQc1zVFGebe1GkgfSgwFVrO7qQMEyvEJWhS2D9Zz2S7cx6W6ypGKkEJRdJGw==","SigningCertUrl":"https://sns.us-west-2.amazonaws.com/SimpleNotificationService-01d088a6f77103d0fe307c0069e40ed6.pem","UnsubscribeUrl":"https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:112233445566:hello-topic:b8fcbeba-6505-4383-bb88-b615b3294324","MessageAttributes":{}}}]}
2023-09-09 14:46:31 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa END RequestId: a777eec7-de7e-4275-a9a3-142d20abc504
2023-09-09 14:46:31 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa REPORT RequestId: a777eec7-de7e-4275-a9a3-142d20abc504     Duration: 2.52 ms      Billed Duration: 3 ms   Memory Size: 1536 MB    Max Memory Used: 137 MB
# second request
2023-09-09 14:46:54 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa START RequestId: 0c371d69-a40c-4880-8ed4-a796c1db8333 Version: $LATEST
2023-09-09 14:46:54 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa dig event {"Records":[{"EventSource":"aws:sns","EventVersion":"1.0","EventSubscriptionArn":"arn:aws:sns:us-west-2:112233445566:hello-topic:b8fcbeba-6505-4383-bb88-b615b3294324","Sns":{"Type":"Notification","MessageId":"34c5d5ed-aba5-5c2d-a7db-889b2c3b9901","TopicArn":"arn:aws:sns:us-west-2:112233445566:hello-topic","Subject":null,"Message":"{\"default\": \"test message2\"}","Timestamp":"2023-09-09T14:46:54.443Z","SignatureVersion":"1","Signature":"qSrwBypYZkrcP/qKg3RAc4RvK8qPI1/NmWb1PF4x2W+XppBdQYvBx/sM1vLu2zAocvXDoNxdW34PV3MKNTNNIrTVVRvP0Yn1g5770mmHK1UHryr48DGvhZt/U6WboZmDdNZbYeatqFBfFiun2HBvOAWUeZqG3vI7ZDLXwC6L8VbgujqpMZrf7Xl05gi5BVSnqlUsPe/dZWmQdf+24Meha02Fg1tJGybgW8BG/oSMUXUGr1sf21CzDB2RtZlcRjmVg9GGHd/p5Y0AaX386vgbVhEZmG3LJ/9gD6vwljTeHpGbyUlWBJGfkrzpPWdjbug3ENCcrh4cpSro5S7YNESqzg==","SigningCertUrl":"https://sns.us-west-2.amazonaws.com/SimpleNotificationService-01d088a6f77103d0fe307c0069e40ed6.pem","UnsubscribeUrl":"https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:112233445566:hello-topic:b8fcbeba-6505-4383-bb88-b615b3294324","MessageAttributes":{}}}]}
2023-09-09 14:46:54 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa END RequestId: 0c371d69-a40c-4880-8ed4-a796c1db8333
2023-09-09 14:46:54 UTC 2023/09/09/[$LATEST]845b67d7a11540678e9f52c610d25ffa REPORT RequestId: 0c371d69-a40c-4880-8ed4-a796c1db8333     Duration: 2.86 ms      Billed Duration: 3 ms   Memory Size: 1536 MB    Max Memory Used: 137 MB

Test DynamoDB Stream Event

I noticed that you’re also testing DynamoDB Stream Events. Tested it also:

Test CLI commands:

aws dynamodb put-item --table-name test-table --item '{"id": {"S": "id-1"}}' 
aws dynamodb put-item --table-name test-table --item '{"id": {"S": "id-2"}}' 

CloudWatch Logs:

# first request
2023-09-09 14:59:31 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a START RequestId: 4f727df5-0684-4f1a-b8f9-1e77375f1fd8 Version: $LATEST
2023-09-09 14:59:31 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a event {"Records":[{"eventID":"aed79e5ddbbd94df113234e8590b9d5d","eventName":"INSERT","eventVersion":"1.1","eventSource":"aws:dynamodb","awsRegion":"us-west-2","dynamodb":{"ApproximateCreationDateTime":1694271571.0,"Keys":{"id":{"S":"id-1"}},"NewImage":{"id":{"S":"id-1"}},"SequenceNumber":"1200000000045765994168","SizeBytes":12,"StreamViewType":"NEW_AND_OLD_IMAGES"},"eventSourceARN":"arn:aws:dynamodb:us-west-2:112233445566:table/test-table/stream/2023-09-09T14:50:49.988"}]}
2023-09-09 14:59:31 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a END RequestId: 4f727df5-0684-4f1a-b8f9-1e77375f1fd8
2023-09-09 14:59:31 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a REPORT RequestId: 4f727df5-0684-4f1a-b8f9-1e77375f1fd8   Duration: 2.98 ms       Billed Duration: 3 ms   Memory Size: 1536 MB    Max Memory Used: 138 MB
# second request
2023-09-09 14:59:42 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a START RequestId: 586fd3d5-b4ec-4a69-b1fd-d4fff78dec0f Version: $LATEST
2023-09-09 14:59:42 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a event {"Records":[{"eventID":"5cc3ec73ef40d27e1ea4d8e780d0dc66","eventName":"INSERT","eventVersion":"1.1","eventSource":"aws:dynamodb","awsRegion":"us-west-2","dynamodb":{"ApproximateCreationDateTime":1694271582.0,"Keys":{"id":{"S":"id-2"}},"NewImage":{"id":{"S":"id-2"}},"SequenceNumber":"1200000000045258722137","SizeBytes":12,"StreamViewType":"NEW_AND_OLD_IMAGES"},"eventSourceARN":"arn:aws:dynamodb:us-west-2:112233445566:table/test-table/stream/2023-09-09T14:50:49.988"}]}
2023-09-09 14:59:42 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a END RequestId: 586fd3d5-b4ec-4a69-b1fd-d4fff78dec0f
2023-09-09 14:59:42 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a REPORT RequestId: 586fd3d5-b4ec-4a69-b1fd-d4fff78dec0f   Duration: 2.23 ms       Billed Duration: 3 ms   Memory Size: 1536 MB    Max Memory Used: 138 MB

Interestingly, notice that a put-item call with the same DynamoDB partition_id will not fire off an event at all unless the item has been modified. Here’s an example of modifying the item.

aws dynamodb put-item --table-name test-table --item '{"id": {"S": "id-2"}, "name": {"S": "name-2"}}'

And logs:

2023-09-09 15:02:22 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a START RequestId: 06201a46-b724-4330-b489-7de0b98000a2 Version: $LATEST
2023-09-09 15:02:22 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a event {"Records":[{"eventID":"451d80a6ac0c682eedc934cb9482a579","eventName":"MODIFY","eventVersion":"1.1","eventSource":"aws:dynamodb","awsRegion":"us-west-2","dynamodb":{"ApproximateCreationDateTime":1694271741.0,"Keys":{"id":{"S":"id-2"}},"NewImage":{"name":{"S":"name-2"},"id":{"S":"id-2"}},"OldImage":{"id":{"S":"id-2"}},"SequenceNumber":"1300000000045258978099","SizeBytes":28,"StreamViewType":"NEW_AND_OLD_IMAGES"},"eventSourceARN":"arn:aws:dynamodb:us-west-2:112233445566:table/test-table/stream/2023-09-09T14:50:49.988"}]}
2023-09-09 15:02:22 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a END RequestId: 06201a46-b724-4330-b489-7de0b98000a2
2023-09-09 15:02:22 UTC 2023/09/09/[$LATEST]87ae50548e364123865c478b0dd9956a REPORT RequestId: 06201a46-b724-4330-b489-7de0b98000a2   Duration: 2.42 ms       Billed Duration: 3 ms   Memory Size: 1536 MB    Max Memory Used: 138 MB

Followup

Unsure why you’re seeing nil :face_with_monocle:

I wonder if you can try modifying the live lambda function handler at the beginning with the AWS Console. The handler or “entry point” to the lambda function should be in the deployed handlers folder. Here’s an example of how it would look like:

handlers/jobs/hard_job.rb

require "bundler/setup"
require "jets"
Jets.once  # runs once in lambda execution context

def dig(event:, context:)
  puts "showing event at the very beginning event: #{JSON.dump(event)}" # ADDED THIS LINE
  Jets.process(event, context, "handlers/jobs/hard_job.dig")
end

It would be useful to see if the event coming into the handler at the very beginning is nil or not.

Related