V2: Use Thing name in MQTT topic for triggering Lambda

1

I want to be able to send messages to specific Things and have them trigger a Lambda on the Thing. I can get it working for a single Thing by hardcoding the Thing name in the YAML when I create the component:

lambdaFunction:  
  ...  
  componentLambdaParameters:  
    eventSources:  
      - topic: print-job/MyThing  
        type: IOT_CORE  

But I haven't been able to find a way to make the topic dynamic. I thought print-job/${AWS_IOT_THING_NAME} was working when I first tried it, but I think I just misunderstood how components' configurations are updated and it wasn't actually applied because I didn't have reset: \[""] in the deployment. I also tried print-job/{system:thingName} without any luck.

Is this possible to do this without using wildcards or creating a separate deployment for each Thing? And feel free to tell me if what I'm trying to do doesn't actually make sense.

I'd rather not use a wildcard in the topic because each Thing only needs to receive the messages sent to the topic for that Thing. It could just discard the other messages, but that seems like a waste of resources.

If there's no way to do this using LambdaManager, I can use a pinned Lambda and have it subscribe to the topic. I just wanted to check to see if there's an easier/cleaner way first.

knad
gefragt vor 3 Jahren364 Aufrufe
2 Antworten
0

Kinda trying the same here: https://forums.aws.amazon.com/thread.jspa?threadID=336516&tstart=0

I don't think there is a way unless your lambda is pinned and "manually" subscribes to its corresponding IoT Core topic

beantwortet vor 3 Jahren
0

Hi knad,

Thank you for posting your question.
Unfortunately there isn't an easier way to use a dynamic topic as you described.
You could try either of the following:

  1. Use a wildcard in the topic.
  2. Use a pinned lambda and subscribe to it's corresponding topic.
  3. You could also explore creating a custom component https://docs.aws.amazon.com/greengrass/v2/developerguide/create-components.html instead of a lambda. You could then leverage the environment variables available to the component https://docs.aws.amazon.com/greengrass/v2/developerguide/component-environment-variables.html

Please let me know if either of these options work for you.

Thanks,
Navya.

AWS
beantwortet vor 3 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen