将 Lambda 函数作为组件导入 () Amazon CLI - Amazon IoT Greengrass
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 Lambda 函数作为组件导入 () Amazon CLI

使用CreateComponentVersion操作从 Lambda 函数创建组件。当您调用此操作时,请指定导lambdaFunction入 Lambda 函数。

步骤 1:定义 Lambda 函数配置

  1. 创建一个名为的文件lambda-function-component.json,然后将以下 JSON 对象复制到该文件中。将替换为lambdaArn要导入的 Lambda 函数的 ARN。

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

    您必须指定包含要导入的函数版本的 ARN。您不能使用像 $LATEST 这样的版本别名。

  2. (可选)指定组件的名称 (componentName)。如果省略此参数,则使用 Lambda 函数的名称Amazon IoT Greengrass创建组件。

    { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda" } }
  3. (可选)指定组件的版本 (componentVersion)。如果省略此参数,则使用 Lambda 函数版本作为有效语义版本Amazon IoT Greengrass创建组件。例如,如果您的函数版本是 3,则组件版本将变为 3.0.0

    注意

    您上传的每个组件版本都必须是唯一的。请务必上传正确的组件版本,因为上传后无法对其进行编辑。

    Amazon IoT Greengrass使用组件的语义版本。语义版本遵循 major.minor.patch 编号系统。例如,版本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) 的属性。C Amazon IoT Greengrass ore 软件可能会添加其他平台属性。在将 Greengrass nucleus 组件部署到核心设备时,也可以指定自定义平台属性。执行以下操作:

    1. 向中的 Lambda 函数添加平台列表 (componentPlatforms)。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 函数组件时,部署中会包含这些依赖项,供您的函数运行。

    重要

    要导入您创建的在 Amazon 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 样式的语义版本约束,用于标识组件依赖关系的兼容版本。您可以指定单个版本或一系列版本。有关语义版本限制的更多信息,请参阅 npm sem ver 计算器。

      • 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 函数订阅的工作消息的事件源。您可以指定事件源来订阅此函数以订阅本地发布/订阅消息和 Amazon 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 nucleus v2.6.0 或更高版本以及 Lambda manager v2.2.5 或更高版本,则可以在指定此类型时在中使用 MQTT 主题通配符(和)。+ # topic

          • IOT_CORE – 订阅 Amazon IoT Core MQTT 消息。

            当您指定此类型topic时,可以在中使用 MQTT 主题通配符(+#)。

          以下示例在与主题筛选条件匹配的主题上订阅 Amazon IoT Core MQTT。hello/world/+

          { "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 函数在启动时Amazon IoT Greengrass启动,并在自己的容器中继续运行。

        • 非固定(或按需)Lambda 函数仅在收到工作项目时启动,并在指定的最大空闲时间内保持空闲状态后退出。如果该函数有多个工作项,Amazon IoT Greengrass Core 软件将创建该函数的多个实例。

        maxIdleTimeInSeconds用于设置函数的最大空闲时间。

      • timeoutInSeconds— Lambda 函数在超时之前可以运行的最大时间(以秒为单位)。默认值为 3 秒。

      • statusTimeoutInSeconds— Lambda 函数组件向 Lambda 管理器组件发送状态更新的间隔(以秒为单位)。此参数仅适用于固定函数。默认值为 60 秒。

      • maxIdleTimeInSeconds— 在Amazon IoT Greengrass核心软件停止其进程之前,非固定 Lambda 函数可以空闲的最大时间(以秒为单位)。默认值为 60 秒。

      • maxInstancesCount— 非固定 Lambda 函数可以同时运行的最大实例数。默认值为 100 个实例。

      • maxQueueSize— Lambda 函数组件的消息队列的最大大小。Amazon 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 函数在Amazon 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 (16 MB)。

        3. (可选)添加mountROSysfs参数以指定容器是否可以读取设备/sys文件夹中的信息。默认值为 false

        4. (可选)配置容器化 Lambda 函数可以访问的本地卷。定义卷时,Amazon 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)。有关更多信息,请参阅 标记 Amazon 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。然后,Amazon 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": "Amazon Lambda", "status": { "componentState": "DEPLOYABLE", "message": "NONE", "errors": {} }, "platforms": [ { "name": "Linux x86", "attributes": { "architecture": "x86", "os": "linux" } } ] }

    组件完成后DEPLOYABLE,您可以将 Lambda 函数部署到您的核心设备。有关更多信息,请参阅 将Amazon IoT Greengrass组件部署到设备