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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠