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
已提问 3 年前364 查看次数
2 回答
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

已回答 3 年前
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
已回答 3 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则