Greengrass SDK for Python - Configuration section of Recipe not reading correctly.

0

Hello, I am exploring Greengrass and tried a "HelloWorld" sample following the ReadMe.md provided in the aws-greengrass-labs-iot-pubsub-sdk-for-python repository.

Everything appears to be working except where it reads the configuration information that is passed in as a command line argument to the main.py module.

I have this line from the template. Only modified for Windows OS instead of Linux.

        "Run": {
          "Script": "py -3 -u {artifacts:decompressedPath}/src/main.py '{configuration:/GGV2PubSubSdkConfig}'",
          "RequiresPrivilege": "false"
        }

This is supposed to substitute the "GGV2PubSubSdkConfig" tag from the recipe.json file which looks like this... (naturally I have replaced the placeholder values with the values from my HelloWorld example).

      "GGV2PubSubSdkConfig": {
        "base-pubsub-topic" : "COMPONENT_NAME",
        "ipc-subscribe-topics" : ["ipc/my-app/broadcast", "ipc/my-app/error"],
        "mqtt-subscribe-topics" : ["mqtt/my-app/broadcast", "mqtt/my-app/error"]
      }

These lines in main.py don't seem to process this information correctly...

        ggv2_component_config = sys.argv[1]
        ggv2_component_config = json.loads(ggv2_component_config)

sys.argv[1] evaluates to...

"{base-pubsub-topic:COMPONENT_NAME:[ipc/my-app/broadcast,ipc/my-app/error],mqtt-subscribe-topics:[mqtt/my-app/broadcast,mqtt/my-app/error]}"

...which isn't properly formed JSON (missing the double quotes around tags and string values), therefore the json.loads() method fails.

I've confirmed by hardcoding a correctly formatted json string that json.loads() will work properly. I just can't figure out how this configuration substitution is supposed to work as the template code doesn't appear to be correct and I can't debug the part of the process that performs the actual substitution (not sure if this happens in the Nucleus code or somewhere in the deployment process).

Can anyone help clarify how you pass a JSON object from the recipe (which includes arrays) as a configuration path to the main.py via the command line?

Dan
已提問 3 個月前檢視次數 119 次
2 個答案
2
已接受的答案

Hello,

I'd suggest you use Greengrass IPC to read the configuration instead of using command line: https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-component-configuration.html#ipc-operation-getconfiguration.

Failing that, use environment variables instead of the command line. Windows commandline is going to remove quotes or otherwise complicate your life no matter what, so you do really need to avoid passing complex configurations that way.

  run:
    script: py -3 -u {artifacts:decompressedPath}/src/main.py
    setEnv:
      MyKey: "{configuration:/GGV2PubSubSdkConfig}"

Cheers,

Michael

AWS
專家
已回答 3 個月前
profile picture
專家
已審閱 2 個月前
  • Thank you Michael. I was able to solve my problem following your suggestion to use an environment variable instead.

0

Providing the solution I implemented based on Michaels answer above....

I used your suggestion of utilizing an environment variable for the substitution to avoid the double quotes getting removed from the command line substitution. My modified recipe manifest looks like this now...

        "Run": {
          "Script": "py -3 -u {artifacts:decompressedPath}/src/main.py GGV2PubSubSdkConfig",
          "SetEnv": {
            "GGV2PubSubSdkConfig": "{configuration:/GGV2PubSubSdkConfig}"
          },
          "RequiresPrivilege": "false"

Note: I still included a single command line parameter that indicates the name of the environment variable. This allowed me to avoid hard-coding the name of the variable in main.py.

Below if the change I made to the template main.py...

        ggv2_component_config = os.environ[sys.argv[1]]
        ggv2_component_config = json.loads(ggv2_component_config)

This solved my issue and the no other changes were required to get the template code running.

Dan
已回答 3 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南