python - 使用 greengrass 在本地设备上执行 lambda 函数

标签 python amazon-web-services aws-lambda aws-iot aws-iot-greengrass

我正在尝试学习 AWS greengrass,因此我正在学习本教程 https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-gs.html其中逐步解释了如何在树莓派上设置 greengrass 并使用 lambda 函数发布一些消息。

一个简单的 lambda 函数如下:

import greengrasssdk
import platform
from threading import Timer
import time


# Creating a greengrass core sdk client
client = greengrasssdk.client('iot-data')

# Retrieving platform information to send from Greengrass Core
my_platform = platform.platform()


def greengrass_hello_world_run():
    if not my_platform:
        client.publish(topic='hello/world', payload='hello Sent from Greengrass Core.')
    else:
        client.publish(topic='hello/world', payload='hello Sent from Greengrass Core running on platform: {}'.format(my_platform))

    # Asynchronously schedule this function to be run again in 5 seconds
    Timer(5, greengrass_hello_world_run).start()


# Execute the function above
greengrass_hello_world_run()


# This is a dummy handler and will not be invoked
# Instead the code above will be executed in an infinite loop for our example
def function_handler(event, context):
    return

这里可行,但我试图通过使用 lambda 函数来做一些额外的工作(例如打开文件并写入文件)来更好地理解它。

我将greengrass_hello_world_run()函数修改如下

def greengrass_hello_world_run():
    if not my_platform:
        client.publish(topic='hello/world', payload='hello Sent from Greengrass Core.')
    else:
        stdout = "hello from greengrass\n"
        with open('/home/pi/log', 'w') as file:
            for line in stdout:
                file.write(line)
        client.publish(topic='hello/world', payload='hello Sent from Greengrass Core running on platform: {}'.format(my_platform))

我期望在部署时,在本地 pi 上运行的守护进程应该在给定目录中创建该文件,因为我相信 greengrass 核心会尝试在本地设备上运行此 lambda 函数。但是它不会创建任何文件,也不会发布任何内容,因为我相信这段代码可能会被破坏。但不知道如何,我尝试查看 cloudwatch,但没有看到报告任何事件或错误。

对此的任何帮助将不胜感激, 干杯!

最佳答案

对此的一些想法...

如果您在 GG 组设置中打开本地日志记录,它将开始在您的 PI 上本地写入日志。设置为:

enter image description here

日志位于:/greengrass/ggc/var/log/system

如果您 tail python_runtime.log,您可以看到 lambda 执行中的任何错误。

如果您想访问本地资源,您需要在 GG 组定义中创建资源。然后,您可以授予对卷的访问权限,您可以在其中写入文件。

完成此操作后,您确实需要部署您的组才能使更改生效。

关于python - 使用 greengrass 在本地设备上执行 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48428838/

相关文章:

python - body 是什么? `from fastapi import Body`

.net - Cloudformation给出错误 "Requires capabilities : [CAPABILITY_AUTO_EXPAND]"

c# - AWS lambda 不在 CloudWatch 中写入日志

amazon-web-services - 当 MySQL (RDS) 表创建/更新行时,可以自动调用 Lambda 函数吗?

python - 通过基于 "_"拆分文本来替换 Pandas 列

python - Jupyter notebook 无法识别 Gensim 库

python - Qt:曲线编辑器或类似的东西?

reactjs - 使用 React 和 Ant Design Pro/UmiJS 实现 AWS Amplify Authenticator

java - 使用前缀、后缀或正则表达式在 S3 存储桶中搜索键?

amazon-web-services - 如何在 AWS EKS 上托管的 pod 内挂载外部 Windows 文件共享?