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

asked 2 years ago230 views
1 Answer
0
Accepted Answer

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
EXPERT
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions