将 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)。如果忽略此参数,Amazon IoT Greengrass 会使用 Lambda 函数的名称创建组件。

    { "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda" } }
  3. (可选)指定组件的版本(componentVersion)。如果忽略此参数,Amazon IoT Greengrass 会使用 Lambda 函数版本作为有效语义版本创建组件。例如,如果您的函数版本是 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)属性。Amazon IoT Greengrass Core 软件可能会添加其他平台属性。向核心设备部署 Greengrass Nucleus 组件时,您可以指定自定义平台属性。执行以下操作:

    1. 将平台列表(componentPlatforms)添加到 lambda-function-component.json 中的 Lambda 函数中。

      { "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-function-component.json 中的 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": { } } }
    2. 将每个组件依赖关系添加到映射中。将组件名称指定为键,并使用以下参数指定一个对象:

      • versionRequirement – npm 样式的语义版本约束,用于标识组件依赖关系的兼容版本。您可以指定一个版本或一系列版本。有关语义版本约束的更多信息,请参阅 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-function-component.json 中的 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": { } } }
    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 管理器 v2.2.5 或更高版本,则可以在指定此类型时在 topic 中使用 MQTT 主题通配符(+#)。

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

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

          以下示例在与 hello/world/+ 主题筛选条件匹配的主题上订阅 Amazon 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 函数在 Amazon IoT Greengrass 启动后启动并在自己的容器中保持运行。

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

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

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

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

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

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

      • maxQueueSize – Lambda 函数组件的消息队列的最大大小。Amazon IoT Greengrass Core 软件将消息存储在 FIFO(先进先出)队列中,直到它可以运行 Lambda 函数来消耗每条消息。默认值为 1000 条消息。

      您的 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 Core 软件内的隔离运行时环境中运行。您也可以选择将 Lambda 函数作为一个进程运行,而不进行任何隔离。如果您在容器中运行 Lambda 函数,则需要配置容器的内存大小以及 Lambda 函数可用的系统资源。执行以下操作:

      1. 将 Linux 进程参数对象 (linuxProcessParams) 添加到 lambda-function-component.json 中的 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": [ { "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 函数作为常规 Linux 进程运行,没有任何隔离。

        默认为 GreengrassContainer

      3. (可选)如果您在容器中运行 Lambda 函数,则可以配置内存量和系统资源(例如卷和设备),以供容器使用。执行以下操作:

        1. 将容器参数对象 (containerParams) 添加到 lambda-function-component.json 中的 Linux 进程参数对象中。

          { "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 参数,指定容器内存大小。默认值为 16384 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-function-component.json 创建 Lambda 函数组件。

    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 组件部署至设备