How to import/share a lib/class between component in ggv2

0

Hi there,

I am just wondering is there a way that I can reuse my code between the component, kind of like lambda layer

eg:

foo
-- src
---- send_mqtt

foo2
-- src
---- send_mqtt

So how can i use request the send_mqtt ?

Thanks for your help

gefragt vor 2 Jahren234 Aufrufe
1 Antwort
0
Akzeptierte Antwort

Hi,

you could create a component which deploys send_mqtt. Then you create your foo and foo2 components with a dependency to send_mqtt.

A particular recipes example could look like:

  • send_mqtt
{
  "RecipeFormatVersion": "2020-01-25",
  "ComponentName": "my.send_mqtt",
  "ComponentVersion": "1.0.0",
  "ComponentDescription": "Send messages via MQTT",
  "ComponentPublisher": "Myself",
  "Manifests": [
    {
      "Platform": {
        "os": "linux"
      },
      "Lifecycle": {
        "Install": "pip3 install --user -r {artifacts:decompressedPath}/send_mqtt/requirements.txt"
      },
      "Artifacts": [
        {
          "URI": "s3://MY_BUCKET/my.send_mqtt/1.0.0/send_mqtt.zip",
          "Unarchive": "ZIP"
        }
      ]
    }
  ]
}
  • foo recipe
{
  "RecipeFormatVersion": "2020-01-25",
  "ComponentName": "my.foo",
  "ComponentVersion": "1.0.0",
  "ComponentDescription": "Foo",
  "ComponentPublisher": "Myself",
  "ComponentDependencies": {
    "my.send_mqtt": {
      "VersionRequirement": "^1.0.0"
    }
  },
  "Manifests": [
    {
      "Platform": {
        "os": "linux"
      },
      "Lifecycle": {
        "Run": "export PYTHONPATH=$PYTHONPATH:{my.send_mqtt:artifacts:decompressedPath}/send_mqtt; python3 {artifacts:path}/foo.py"
      },
      "Artifacts": [
        {
          "URI": "s3://MY_BUCKET/my.foo/1.0.0/foo.py"
        }
      ]
    }
  ]
}

In foo.py you can then import send_mqtt.

KR,

Philipp

AWS
EXPERTE
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen