Subscriber implementation

Creating a component

  1. Run the following command in the terminal to create a folder of artifacts and an empty script file
mkdir -p ~/environment/GreengrassCore/artifacts/com.example.Subscriber/1.0.0 && cd ~/environment/GreengrassCore/artifacts/com.example.Subscriber/1.0.0
  1. Open the created empty script file and paste the following source code respectively.

# Copyright, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import time
import json
import awsiot.greengrasscoreipc
import awsiot.greengrasscoreipc.client as client
from awsiot.greengrasscoreipc.model import (


ipc_client = awsiot.greengrasscoreipc.connect()

class StreamHandler(client.SubscribeToTopicStreamHandler):
    def __init__(self):

    def on_stream_event(self, event: SubscriptionResponseMessage) -> None:
        message_string = event.json_message.message
        with open('/tmp/Greengrass_Subscriber.log', 'a') as f:
            print(message_string, file=f)

    def on_stream_error(self, error: Exception) -> bool:
        return True

    def on_stream_closed(self) -> None:

topic = "my/topic"

request = SubscribeToTopicRequest()
request.topic = topic
handler = StreamHandler()
operation = ipc_client.new_subscribe_to_topic(handler)
future = operation.activate(request)
while True:

  1. Run the following command in the terminal to create a recipe folder and an empty file
touch ~/environment/GreengrassCore/recipes/com.example.Subscriber-1.0.0.json 
  1. Open the created empty file and paste the following contents


  "RecipeFormatVersion": "2020-01-25",
  "ComponentName": "com.example.Subscriber",
  "ComponentVersion": "1.0.0",
  "ComponentDescription": "A component that subscribes to messages.",
  "ComponentPublisher": "Amazon",
  "ComponentConfiguration": {
    "DefaultConfiguration": {
      "accessControl": {
        "aws.greengrass.ipc.pubsub": {
          "com.example.Subscriber:pubsub:1": {
            "policyDescription": "Allows access to publish to all topics.",
            "operations": [
            "resources": [
  "Manifests": [
      "Lifecycle": {
        "Install": "pip3 install awsiotsdk",
        "Run": "python3 {artifacts:path}/"
  1. Run the following command to deploy the component
sudo /greengrass/v2/bin/greengrass-cli deployment create \
  --recipeDir ~/environment/GreengrassCore/recipes \
  --artifactDir ~/environment/GreengrassCore/artifacts \
  --merge "com.example.Subscriber=1.0.0"
  1. Execute the following command, and if the received message continues to be displayed, it is successful (deployment can take up to a minute).
tail -f /tmp/Greengrass_Subscriber.log

If you encounter issue such as below:

tail -f /tmp/Greengrass_Subscriber.log
tail: cannot open '/tmp/Greengrass_Subscriber.log' for reading: No such file or directory
tail: no files remaining

The local development tools can take up to a minute to deploy. You can run the following command to check the status of the deployment. Replace GreengrassQuickStartCore-xxxxx with the name of your core device.

aws greengrassv2 list-effective-deployments --core-device-thing-name GreengrassQuickStartCore-xxxxx

Which component is running already? check out -> Operation Tip 1 and to re-start any component check this out

Having issues? -> More details can be found in official Troubleshooting section here