Migrating Lambda from Greengrass v1 to Greengrass v2

0

Hello, we're in the process of migrating a long running Lambda function from Greengrass v1 to v2.

So far it all works well and we eventually managed to get it to run in Greengrass v2 without exceptions.

With Greengrass v1 based on a subscription all changes made to the shadow led to calls to the Lambda handler with the JSON delta as a parameter. With Greengrass v2 we're not getting these calls anymore. I guess I could explicitly subscribe for the delta in my pinned Lambda function but that feels wrong as it would make the handler obsolete. Can you please help with the best approach to get to process the shadow deltas in Greengrass v2?

For the record, the ShadowManager configuration is

"aws.greengrass.ShadowManager": {
  "componentVersion": "2.3.5",
  "configurationUpdate": {
    "merge": "{\"synchronize\":{\"coreThing\":{}}}"
  }
}

and my understanding is that both direction = betweenDeviceAndCloud and coreThing.classic = true are defaults.

asked 4 months ago326 views
1 Answer
1
Accepted Answer

Hello,

How did you configure the Lambda's event source? You need to configure the lambda with an event source on the delta topic for shadow as shown here: https://docs.aws.amazon.com/greengrass/v2/developerguide/interact-with-shadows-in-components.html#react-shadow-events.

Cheers, Michael

AWS
EXPERT
answered 4 months ago
  • Thank you for pointing me in the right direction. For the record in my lambda config I've added

      "eventSources": [
        {
          "type": "PUB_SUB",
          "topic": "$aws/things/ThingName/shadow/update/delta"
        }
      ],
    

    and in the access control section of the deployment recipe I have

    "aws.greengrass.ipc.pubsub": {
      "ComponentName:pubsub:1": {
        "policyDescription": "Allows access to shadow pubsub topics",
        "operations": [
          "aws.greengrass#SubscribeToTopic"
        ],
        "resources": [
          "$aws/things/{iot:thingName}/shadow/get/accepted"
        ]
      }
    }
    

    The lambda handler does indeed get now called although I still have a little issue with parsing the input as I get Failed to parse input payload: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_OBJECT token.

    My handler is RequestHandler<String, Unit> (this is Kotlin, in Java it would be Void)

    May I ask what type the input is and please point me to some documentation for the conversion?

  • With an event source you do not need any access control to subscribe. Lambda will subscribe for you.

    If you set your lambda's inputPayloadEncodingType to JSON instead of BINARY then the input will be a map

  • Thank you very much for your help. It's much appreciated. I finally got it to work by setting inputPayloadEncodingType to binary and implementing RequestHandler<InputStream, Unit>.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions