1 Answer
- Newest
- Most votes
- Most comments
0
If you are starting a new deployment on Greengrass V2 I would advice to use Generic components and not Lambda Functions components.
A generic component is simpler to write and debug and can more easily access devices resources if needed (such as cameras, etc).
All you need to do to trigger some logic on the reception of a message is to create a subscription to the pubsub topic and write the corresponding handler. You can refer to this sample code (from https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-publish-subscribe.html#ipc-publish-subscribe-examples)
import time import traceback from awsiot.greengrasscoreipc.clientv2 import GreengrassCoreIPCClientV2 from awsiot.greengrasscoreipc.model import ( SubscriptionResponseMessage, UnauthorizedError ) def main(): topic = 'my/topic'; try: ipc_client = GreengrassCoreIPCClientV2() # Subscription operations return a tuple with the response and the operation. _, operation = ipc_client.subscribe_to_topic(topic=topic, on_stream_event=on_stream_event, on_stream_error=on_stream_error) print('Successfully subscribed to topic: ' + topic) # Keep the main thread alive, or the process will exit. try: while True: time.sleep(10) except InterruptedError: print('Subscribe interrupted.') # To stop subscribing, close the stream. operation.close() except UnauthorizedError: print('Unauthorized error while subscribing to topic: ' + topic, file=sys.stderr) traceback.print_exc() exit(1) except Exception: print('Exception occurred', file=sys.stderr) traceback.print_exc() exit(1) def on_stream_event(event: SubscriptionResponseMessage) -> None: try: message = str(event.binary_message.message, 'utf-8') topic = event.binary_message.context.topic print('Received new message on topic %s: %s' % (topic, message)) except: traceback.print_exc() def on_stream_error(error: Exception) -> bool: print('Received a stream error.', file=sys.stderr) traceback.print_exc() return False # Return True to close stream, False to keep stream open. if __name__ == '__main__': main()
Relevant content
- Accepted Answerasked 3 years ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 6 months ago
I agree with the recommendation in the answer below to use native components, however if you still want help with the lambda, please provide the full logs from Greengrass and your component's log that show the error.
Log pipe being blocked is not an error that should impact your lambda's execution in any way.
Interesting I see both comments stating we should use native components. I'm not against it for reasons posted below but inheriting a previous design.
Is it not recommended because lambda support is not as mature and generally causes more problems? Or just specifically to subscribing to a topic and responding to events on the topic per the use case I'm having trouble with.
We do use lambdas in several other use cases and I'm concerned that perhaps, this part of the GreenGrass eco-system is not as robust and should be avoided for more traditional approaches.
I recommend native components because there's less "magic" involved. Lambda tries to do a lot of things for you, but then if anything goes poorly you can't do anything about it. Native components on the other hand can have much better performance, debuggability, and less actual complexity (though it may seem more complex initially).