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
asked 3 years ago357 views
2 Answers
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

answered 3 years ago
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
answered 3 years ago

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