本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
将 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
) 的组件。如果省略此参数,Amazon IoT Greengrass使用 Lambda 函数的名称创建组件。{ "lambdaFunction": { "lambdaArn": "arn:aws:lambda:
region
:account-id
:function:HelloWorld:1", "componentName": "com.example.HelloWorldLambda
" } } -
(可选)指定版本 (
componentVersion
),用于组件。如果省略此参数,Amazon IoT Greengrass使用 Lambda 函数版本创建组件作为有效语义版本。例如,如果您的函数版本是3
,则组件版本将变为3.0.0
。注意 您上传的每个组件版本都必须是唯一的。请确保上传正确的组件版本,因为在上传后无法对其进行编辑。
Amazon IoT Greengrass对组件使用语义版本。语义版本遵循重大的.未成年人.补丁数字系统。例如,版本
1.0.0
表示组件的第一个主要发行版。有关更多信息,请参阅 。Semantic vers. { "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核心软件可能会添加其他平台属性。您还可以在部署时指定自定义平台属性Greengrass 核组件到核心设备。执行以下操作:-
添加平台列表 (
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": [ ] } } -
将每个支持的平台添加到列表中。每个平台都有一个友好的
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 函数组件时,部署包含这些依赖项,供您的函数运行。
重要 导入您创建的要在上运行的 Lambda 函数Amazon IoT GreengrassV1 中,您必须为函数使用的功能(例如 secrets、局部阴影和流管理器)定义单个组件依赖关系。将这些组件定义为硬依赖项以便在依赖项更改状态时重新启动 Lambda 函数组件。有关更多信息,请参阅 导入 V1 Lambda 函数。
执行以下操作:
-
添加组件依赖关系映射 (
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": { } } } -
将每个组件依赖项添加到映射中。将组件名称指定为键,并指定一个具有以下参数的对象:
-
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 函数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": { } } } -
(可选)指定 Lambda 函数订阅工作消息的事件源。您可以指定事件源以将此函数订阅到本地发布/订阅消息,Amazon IoT CoreMQTT 消息。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 核v2.6.0 或更高版本和Lambda 经理v2.2.5 或更高版本中,您可以使用 MQTT 主题通配符 (
+
和#
) 在topic
当您指定此类型时。 -
IOT_CORE
— 订阅Amazon IoT CoreMQTT 消息。你可以使用 MQTT 主题通配符 (
+
和#
) 在topic
当您指定此类型时。
以下示例订阅了Amazon IoT CoreMQTT 的主题与
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 GreengrassCore 软件创建该函数的多个实例。
使用
maxIdleTimeInSeconds
来设置函数的最大空闲时间。 -
-
timeoutInSeconds
— Lambda 函数在超时之前可以运行的最长时间(以秒为单位)。默认值为 3 秒。 -
statusTimeoutInSeconds
— Lambda 函数组件向 Lambda 管理器组件发送状态更新的时间间隔(以秒为单位)。此参数仅适用于固定的函数。默认值为 60 秒。 -
maxIdleTimeInSeconds
— 非固定 Lambda 函数在空闲之前可以空闲的最长时间(以秒为单位)Amazon IoT Greengrass核心软件停止其进程。默认值为 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 } } } -
-
(可选)为 Lambda 函数配置容器设置。默认情况下,Lambda 函数在独立运行时环境中运行Amazon IoT GreengrassCore 软件。您也可以选择在没有任何隔离的情况下将 Lambda 函数作为进程运行。如果您在容器中运行 Lambda 函数,则需要配置容器的内存大小以及 Lambda 函数可用的系统资源。执行以下操作:
-
添加 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": { } } } } -
(可选)指定 Lambda 函数是否在容器中运行。添加
isolationMode
参数转换为流程参数对象,然后从以下选项中进行选择:-
GreengrassContainer
— Lambda 函数在容器中运行。 -
NoContainer
— Lambda 函数作为进程运行,没有任何隔离。
默认为
GreengrassContainer
。 -
-
(可选)如果您在容器中运行 Lambda 函数,则可以配置容器可用的内存量和系统资源(如卷和设备)。执行以下操作:
-
添加容器参数对象 (
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": { } } } } } -
(可选)添加
memorySizeInKB
参数以指定容器的内存大小。默认值为 16,384 KB (16 MB)。 -
(可选)添加
mountROSysfs
参数指定容器是否可以从设备的/sys
folder。默认为false
。 -
(可选)配置容器化 Lambda 函数可以访问的本地卷。定义体积块时,Amazon IoT Greengrass核心软件将源文件挂载到容器内的目标。执行以下操作:
-
添加卷列表 (
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 函数组件
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
从输出中,在下一步中检查组件的状态。 -
创建组件时,其状态为
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组件部署到设备。