將 Lambda 函數匯入為元件 (AWS CLI) - AWS IoT Greengrass

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將 Lambda 函數匯入為元件 (AWS CLI)

使用此CreateComponentVersion作業從 Lambda 函數建立元件。呼叫此作業時,請指定匯lambdaFunction入 Lambda 函數。

步驟 1:定義 Lambda 函數組態

  1. 建立名為的檔案lambda-function-component.json,然後將下列 JSON 物件複製到檔案中。以要匯入的 Lambda 函數的 ARN 取代。lambdaArn

    { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1" } }
    重要

    您必須指定包含要匯入之函數版本的 ARN。您不能使用 $LATEST 之類的版本別名。

  2. (選擇性) 指定元件的名稱 (componentName)。如果您省略此參數,請使用 Lambda 函數的名稱AWS IoT Greengrass建立元件。

    { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda" } }
  3. (選擇性) 指定元件的版本 (componentVersion)。如果您省略此參數,則AWS IoT Greengrass會建立具有 Lambda 函數版本作為有效語意版本的元件。例如,如果您的函數版本為 3,則元件版本會變成 3.0.0

    注意

    您上傳的每個元件版本都必須是唯一的。請務必上傳正確的元件版本,因為上傳後就無法編輯它。

    AWS IoT Greengrass使用語義版本的組件。語義版本遵循一個主要的。 未成年人修補程式編號系統。例如,版本1.0.0代表元件的第一個主要發行版本。如需詳細資訊,請參閱語意版本規格

    { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0" } }
  4. (選擇性) 指定此 Lambda 函數支援的平台。每個平台都包含可識別平台的屬性對映。所有核心裝置都具有作業系統 (os) 和架構 (architecture) 的屬性。AWS IoT Greengrass核心軟體可能會新增其他平台屬性。當您將 Greengrass 核心元件部署到核心裝置時,您也可以指定自訂平台屬性。請執行下列操作:

    1. 將平台清單 (componentPlatforms) 新增至中的 Lambda 函數lambda-function-component.json

      { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ ] } }
    2. 將每個支援的平台新增至清單。每個平台都有一name個友好的識別它和屬性映射。下列範例會指定此函數支援執行 Linux 的 x86 裝置。

      { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } }

      lambda-function-component.json可能包含類似下列範例的文件。

      { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ] } }
  5. (選擇性) 指定 Lambda 函數的元件相依性。部署 Lambda 函數元件時,部署會包含這些相依性供您的函數執行。

    重要

    若要匯入您建立要在 AWS IoT Greengrass V1 上執行的 Lambda 函數,您必須針對函數使用的功能 (例如秘密、本機陰影和串流管理員) 定義個別元件相依性。將這些元件定義為硬式相依性,以便 Lambda 函數元件在相依性變更狀態時重新啟動。如需詳細資訊,請參閱 匯入 V1 Lambda 函數

    請執行下列操作:

    1. 將元件相依性 (componentDependencies) 的對應新增至中的 Lambda 函數lambda-function-component.json

      { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { } } }
    2. 將每個組件依賴關係添加到映射中。指定元件名稱做為索引鍵,並使用下列參數指定物件:

      • versionRequirement— 識別元件相依性的相容版本的 npm-style 語意版本條件約束。您可以指定單一版本或版本範圍。如需語意版本條件約束的詳細資訊,請參閱 npm semver 計算器。

      • dependencyType— (選用) 相依性的類型。請選擇下列項目:

        • SOFT— 如果相依性變更狀態,Lambda 函數元件不會重新啟動。

        • HARD— 如果相依性變更狀態,Lambda 函數元件會重新啟動。

        預設值為 HARD

      下列範例會指定此 Lambda 函數依賴串流管理員元件第一個主要版本中的任何版本。Lambda 函數元件會在串流管理員重新啟動或更新時重新啟

      { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }

      lambda-function-component.json可能包含類似下列範例的文件。

      { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } } } }
  6. (選擇性) 設定用來執行函數的 Lambda 函數參數。您可以設定環境變數、訊息事件來源、逾時和容器設定等選項。請執行下列操作:

    1. 將 Lambda 參數物件 (componentLambdaParameters) 新增至中的 Lambda 函數lambda-function-component.json

      { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { } } }
    2. (選擇性) 指定 Lambda 函數訂閱工作訊息的事件來源。您可以指定事件來源,將此函數訂閱到本機發佈/訂閱訊息和 AWS IoT Core MQTT 訊息。當接收來自事件來源的訊息時,就會呼叫 Lambda 函數。

      注意

      若要訂閱此函數以接收來自其他 Lambda 函數或元件的訊息,請在部署此 Lambda 函數元件時部署舊版訂閱路由器元件。部署舊版訂閱路由器元件時,請指定 Lambda 函數使用的訂閱。

      請執行下列操作:

      1. 將事件來源清單 (eventSources) 新增至 Lambda 函數參數。

        { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ ] } } }
      2. 將每個事件來源新增至清單。每個事件來源都有下列參數:

        • topic— 要訂閱訊息的主題。

        • type— 事件來源的類型。您可以從以下選項中選擇:

          • PUB_SUB - 訂閱本機發佈/訂閱訊息。

            如果您使用 Greengrass 核心 v2.6.0 或更新版本和 Lambda 管理員 v2.2.5 或更新版本,則可以在指定此類型時,在中使用 MQTT 主題萬用字元 (和)。+ # topic

          • IOT_CORE – 訂閱 AWS IoT Core MQTT 訊息。

            topic當您指定此類型時,您可以在中使用 MQTT 主題萬用字元 (+#)。

          下列範例會針對符合主題篩選hello/world/+器的主題訂閱 AWS IoT Core MQTT。

          { "topic": "hello/world/+", "type": "IOT_CORE" }

          您的外觀lambda-function-component.json可能類似於下列範例。

          { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ] } } }
    3. (選擇性) 在 Lambda 函數參數物件中指定下列任一參數:

      • environmentVariables— Lambda 函數執行時可用的環境變數對應。

      • execArgs— 執行時傳遞給 Lambda 函數的引數清單。

      • inputPayloadEncodingType— Lambda 函數支援的有效負載類型。您可以從以下選項中選擇:

        • json

        • binary

        預設:json

      • pinned— 無論 Lambda 函數是否已固定。預設值為 true

        • 固定(或長壽命)Lambda 函數在啟動時AWS IoT Greengrass啟動,並在自己的容器中繼續運行。

        • 非釘選 (或隨選) Lambda 函數只會在收到工作項目時啟動,並在閒置超過指定的最大閒置時間後結束。如果此函數具有多個工作項目,AWS IoT Greengrass Core 軟體會建立此函數的多個執行個體。

        maxIdleTimeInSeconds於設定函數的最大閒置時間。

      • timeoutInSeconds— Lambda 函數在逾時之前可以執行的時間上限 (以秒為單位)。預設為 3 秒。

      • statusTimeoutInSeconds— Lambda 函數元件將狀態更新傳送至 Lambda 管理員元件的間隔 (秒)。此參數僅適用於固定的函數。預設值為 60 秒。

      • maxIdleTimeInSeconds— AWS IoT Greengrass 核心軟體停止處理程序之前,非釘選 Lambda 函數可閒置的最大時間 (以秒為單位)。預設值為 60 秒。

      • maxInstancesCount— 非固定 Lambda 函數可同時執行的執行個體數目上限。預設值為 100 個執行個體。

      • maxQueueSize— Lambda 函數元件的訊息佇列大小上限。AWS IoT Greengrass核心軟體會將訊息儲存在 FIFO (first-in-first-out) 佇列中,直到它可以執行 Lambda 函數來使用每個訊息為止。預設值為 1,000 則訊息。

      lambda-function-component.json可能包含類似下列範例的文件。

      { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ], "environmentVariables": { "LIMIT": "300" }, "execArgs": [ "-d" ], "inputPayloadEncodingType": "json", "pinned": true, "timeoutInSeconds": 120, "statusTimeoutInSeconds": 30, "maxIdleTimeInSeconds": 30, "maxInstancesCount": 50, "maxQueueSize": 500 } } }
    4. (選擇性) 設定 Lambda 函數的容器設定。根據預設,Lambda 函數會在AWS IoT Greengrass核心軟體內的隔離執行階段環境中執行。您也可以選擇在沒有任何隔離的情況下以程序的形式執行 Lambda 函數。如果您在容器中執行 Lambda 函數,則需要設定容器的記憶體大小,以及 Lambda 函數可使用的系統資源。請執行下列操作:

      1. 將 Linux 程序參數物件 (linuxProcessParams) 新增至中的 Lambda 參數物件lambda-function-component.json

        { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ], "environmentVariables": { "LIMIT": "300" }, "execArgs": [ "-d" ], "inputPayloadEncodingType": "json", "pinned": true, "timeoutInSeconds": 120, "statusTimeoutInSeconds": 30, "maxIdleTimeInSeconds": 30, "maxInstancesCount": 50, "maxQueueSize": 500, "linuxProcessParams": { } } } }
      2. (選擇性) 指定 Lambda 函數是否在容器中執行。將isolationMode參數新增至流程參數物件,然後從下列選項中進行選擇:

        • GreengrassContainer-Lambda 函數在容器中運行。

        • NoContainer-Lambda 函數作為一個過程運行,沒有任何隔離。

        預設值為 GreengrassContainer

      3. (選擇性) 如果您在容器中執行 Lambda 函數,您可以設定可供容器使用的記憶體數量和系統資源 (例如磁碟區和裝置)。請執行下列操作:

        1. 將容器參數物件 (containerParams) 新增至中的 Linux 程序參數物件lambda-function-component.json

          { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ], "environmentVariables": { "LIMIT": "300" }, "execArgs": [ "-d" ], "inputPayloadEncodingType": "json", "pinned": true, "timeoutInSeconds": 120, "statusTimeoutInSeconds": 30, "maxIdleTimeInSeconds": 30, "maxInstancesCount": 50, "maxQueueSize": 500, "linuxProcessParams": { "containerParams": { } } } } }
        2. (選擇性) 新增memorySizeInKB參數以指定容器的記憶體大小。預設值為 16,384 KB。

        3. (選擇性) 新增mountROSysfs參數以指定容器是否可以從設備資/sys料夾讀取資訊。預設值為 false

        4. (選擇性) 設定容器化 Lambda 函數可存取的本機磁碟區。在您定義磁碟區時,AWS IoT Greengrass Core 軟體會將來源檔案掛載至容器內的目的地。請執行下列操作:

          1. 將磁碟區清單 (volumes) 新增至容器參數。

            { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ], "environmentVariables": { "LIMIT": "300" }, "execArgs": [ "-d" ], "inputPayloadEncodingType": "json", "pinned": true, "timeoutInSeconds": 120, "statusTimeoutInSeconds": 30, "maxIdleTimeInSeconds": 30, "maxInstancesCount": 50, "maxQueueSize": 500, "linuxProcessParams": { "containerParams": { "memorySizeInKB": 32768, "mountROSysfs": true, "volumes": [ ] } } } } }
          2. 將每個磁碟區新增至清單。每個磁碟區都有下列參數:

            • sourcePath— 核心裝置上來源資料夾的路徑。

            • destinationPath— 容器中目標資料夾的路徑。

            • permission— (選用) 從容器存取來源資料夾的權限。您可以從以下選項中選擇:

              • ro— Lambda 函數對來源資料夾具有唯讀存取權。

              • rw— Lambda 函數具有來源資料夾的讀寫存取權限。

              預設值為 ro

            • addGroupOwner— (選用) 是否新增執行 Lambda 函數元件的系統群組,做為來源資料夾的擁有者。預設值為 false

            lambda-function-component.json可能包含類似下列範例的文件。

            { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ], "environmentVariables": { "LIMIT": "300" }, "execArgs": [ "-d" ], "inputPayloadEncodingType": "json", "pinned": true, "timeoutInSeconds": 120, "statusTimeoutInSeconds": 30, "maxIdleTimeInSeconds": 30, "maxInstancesCount": 50, "maxQueueSize": 500, "linuxProcessParams": { "containerParams": { "memorySizeInKB": 32768, "mountROSysfs": true, "volumes": [ { "sourcePath": "/var/data/src", "destinationPath": "/var/data/dest", "permission": "rw", "addGroupOwner": true } ] } } } } }
        5. (選擇性) 設定容器化 Lambda 函數可存取的本機系統裝置。請執行下列操作:

          1. 將系統裝置 (devices) 清單新增至容器參數。

            { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ], "environmentVariables": { "LIMIT": "300" }, "execArgs": [ "-d" ], "inputPayloadEncodingType": "json", "pinned": true, "timeoutInSeconds": 120, "statusTimeoutInSeconds": 30, "maxIdleTimeInSeconds": 30, "maxInstancesCount": 50, "maxQueueSize": 500, "linuxProcessParams": { "containerParams": { "memorySizeInKB": 32768, "mountROSysfs": true, "volumes": [ { "sourcePath": "/var/data/src", "destinationPath": "/var/data/dest", "permission": "rw", "addGroupOwner": true } ], "devices": [ ] } } } } }
          2. 將每個系統裝置新增至清單。每個系統設備具有以下參數:

            • path— 核心裝置上系統裝置的路徑。

            • permission— (選用) 從容器存取系統裝置的權限。您可以從以下選項中選擇:

              • ro— Lambda 函數對系統裝置具有唯讀存取權限。

              • rw-Lambda 函數具有對系統設備的讀寫訪問權限。

              預設值為 ro

            • addGroupOwner— (選用) 是否新增以系統裝置擁有者身分執行 Lambda 函數元件的系統群組。預設值為 false

          lambda-function-component.json可能包含類似下列範例的文件。

          { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "componentPlatforms": [ { "name": "Linux x86", "attributes": { "os": "linux", "architecture": "x86" } } ], "componentDependencies": { "aws.greengrass.StreamManager": { "versionRequirement": "^1.0.0", "dependencyType": "HARD" } }, "componentLambdaParameters": { "eventSources": [ { "topic": "hello/world/+", "type": "IOT_CORE" } ], "environmentVariables": { "LIMIT": "300" }, "execArgs": [ "-d" ], "inputPayloadEncodingType": "json", "pinned": true, "timeoutInSeconds": 120, "statusTimeoutInSeconds": 30, "maxIdleTimeInSeconds": 30, "maxInstancesCount": 50, "maxQueueSize": 500, "linuxProcessParams": { "containerParams": { "memorySizeInKB": 32768, "mountROSysfs": true, "volumes": [ { "sourcePath": "/var/data/src", "destinationPath": "/var/data/dest", "permission": "rw", "addGroupOwner": true } ], "devices": [ { "path": "/dev/sda3", "permission": "rw", "addGroupOwner": true } ] } } } } }
  7. (選擇性) 為元件加入標籤 (tags)。如需詳細資訊,請參閱 標記您的 AWS IoT Greengrass Version 2 資源

步驟 2:建立 Lambda 函數元件

  1. 執行下列命令以從中建立 Lambda 函數元件lambda-function-component.json

    aws greengrassv2 create-component-version --cli-input-json file://lambda-function-component.json

    如果要求成功,回應看起來會類似下列範例。

    { "arn": "arn:aws:greengrass:region:123456789012:components:com.example.HelloWorldLambda:versions:1.0.0", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "creationTimestamp": "Mon Dec 15 20:56:34 UTC 2020", "status": { "componentState": "REQUESTED", "message": "NONE", "errors": {} } }

    arn從輸出複製,以在下一個步驟中檢查組件的狀態。

  2. 當您建立元件時,其狀態為REQUESTED。然後,AWS IoT Greengrass驗證元件是否可部署。您可以執行下列命令來查詢元件狀態,並確認元件是否可部署。arn使用上一個步驟中的 ARN 取代。

    aws greengrassv2 describe-component \ --arn "arn:aws:greengrass:region:account-id:components:com.example.HelloWorldLambda:versions:1.0.0"

    如果組件驗證,則響應指示組件狀態為DEPLOYABLE

    { "arn": "arn:aws:greengrass:region:account-id:components:com.example.HelloWorldLambda:versions:1.0.0", "componentName": "com.example.HelloWorldLambda", "componentVersion": "1.0.0", "creationTimestamp": "2020-12-15T20:56:34.376000-08:00", "publisher": "AWS Lambda", "status": { "componentState": "DEPLOYABLE", "message": "NONE", "errors": {} }, "platforms": [ { "name": "Linux x86", "attributes": { "architecture": "x86", "os": "linux" } } ] }

    在元件完成之後DEPLOYABLE,您可以將 Lambda 函數部署到核心裝置。如需更多詳細資訊,請參閱 將AWS IoT Greengrass元件部署到裝置