本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
将 Lambda 函数作为组件导入(Amazon CLI)
使用CreateComponentVersion操作从 Lambda 函数创建组件。调用此操作时,指定 lambdaFunction 导入 Lambda 函数。
第 1 步:定义 Lambda 函数配置
-
创建一个名为
lambda-function-component.json的文件,然后将以下 JSON 对象复制到该文件中。将lambdaArn替换为要导入的 Lambda 函数的 ARN。{ "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1" } }重要
您必须指定一个 ARN,其中包含要导入的函数的版本。您不能使用像
$LATEST这样的版本别名。 -
(可选)指定组件的名称(
componentName)。如果省略此参数,则使用 Lambda 函数的名称 Amazon IoT Greengrass 创建组件。{ "lambdaFunction": { "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda" } } -
(可选)指定组件的版本(
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" } } -
(可选)指定此 Lambda 函数支持的平台。每个平台都包含一个标识平台的属性映射。所有核心设备都具有操作系统(
os)和架构(architecture)属性。 Amazon IoT Greengrass Core 软件可能会添加其他平台属性。向核心设备部署 Greengrass Nucleus 组件时,您可以指定自定义平台属性。执行以下操作:-
将平台列表(
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": [ ] } } -
将每个支持的平台添加到列表中。每个平台都有一个便于识别的
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" } } ] } }
-
-
(可选)为您的 Lambda 函数指定组件依赖关系。部署 Lambda 函数组件时,部署中会包含这些依赖关系,以便函数运行。
重要
要导入您创建的在 Amazon IoT Greengrass V1 上运行的 Lambda 函数,必须为函数使用的功能(例如机密、本地阴影和流管理器)定义各个组件依赖关系。将这些组件定义为硬依赖关系,以便您的 Lambda 函数组件在依赖关系更改状态时重新启动。有关更多信息,请参阅 导入 V1 Lambda 函数。
执行以下操作:
-
将组件依赖关系映射(
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": { } } } -
将每个组件依赖关系添加到映射中。将组件名称指定为键,并使用以下参数指定一个对象:
-
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" } } } } -
-
-
(可选)配置用于运行 Lambda 函数的参数。您可以配置环境变量、消息事件源、超时和容器设置等选项。执行以下操作:
-
将 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": { } } } -
(可选)指定 Lambda 函数订阅工作消息的事件源。您可以指定事件源来订阅此函数以订阅本地发布/订阅消息和 Amazon IoT Core MQTT 消息。Lambda 函数在收到来自事件源的消息时会被调用。
注意
要将此函数订阅到来自其他 Lambda 函数或组件的消息,请在部署此 Lambda 函数组件时部署旧版订阅路由器组件。部署旧版订阅路由器组件时,请指定 Lambda 函数使用的订阅。
执行以下操作:
-
将事件源列表(
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": [ ] } } } -
将每个事件源添加到列表中。每个事件源都具有以下参数:
-
topic– 订阅消息的主题。 -
type– 事件源的类型。从以下选项中进行选择:-
PUB_SUB– 订阅本地发布/订阅消息。如果您使用 Greengrass Nucleus v2.6.0 或更高版本以及 Lambda 管理器 v2.2.5 或更高版本,则可以在指定此类型时在
topic中使用 MQTT 主题通配符(+和#)。 -
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" } ] } } } -
-
-
-
(可选)在 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 函数来使用每条消息。默认值为 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 } } } -
-
(可选)为 Lambda 函数配置容器设置。默认情况下,Lambda 函数在 Amazon IoT Greengrass 核心软件内部的隔离运行时环境中运行。您也可以选择将 Lambda 函数作为一个进程运行,而不进行任何隔离。如果您在容器中运行 Lambda 函数,则需要配置容器的内存大小以及 Lambda 函数可用的系统资源。执行以下操作:
-
将 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": { } } } } -
(可选)指定 Lambda 函数是否在容器中运行。将
isolationMode参数添加到进程参数对象中,然后从以下选项中进行选择:-
GreengrassContainer– Lambda 函数在容器中运行。 -
NoContainer– Lambda 函数作为常规 Linux 进程运行,没有任何隔离。
默认为
GreengrassContainer。 -
-
(可选)如果您在容器中运行 Lambda 函数,则可以配置内存量和系统资源(例如卷和设备),以供容器使用。执行以下操作:
-
将容器参数对象 (
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": { } } } } } -
(可选)添加
memorySizeInKB参数,指定容器内存大小。默认值为 16384 KB(16 MB)。 -
(可选)添加
mountROSysfs参数,指定容器是否可以从设备的/sys文件夹中读取信息。默认为false。 -
(可选)配置容器化 Lambda 函数可以访问的本地卷。定义卷时, Amazon IoT Greengrass Core 软件会将源文件挂载到容器内的目标位置。执行以下操作:
-
将卷列表 (
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": [ ] } } } } } -
将每个卷添加到列表中。每个卷都具有以下参数:
-
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 } ] } } } } } -
-
-
(可选)配置容器化 Lambda 函数可以访问的本地系统设备。执行以下操作:
-
将系统设备列表 (
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": [ ] } } } } } -
将每台系统设备添加到列表中。每台系统设备都具有以下参数:
-
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 } ] } } } } } -
-
-
-
-
(可选)为组件添加标签 (
tags)。有关更多信息,请参阅 标记您的 Amazon IoT Greengrass Version 2 资源。
第 2 步:创建 Lambda 函数组件
-
输入以下命令,从
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,以便在下一步中检查组件的状态。 -
创建组件后,其状态为
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 组件部署到设备。