我正在尝试学习AWS greengrass,因此我遵循了本教程https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-gs.html,其中一步一步地说明了如何在raspberry pi上设置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 core试图在本地设备上运行这个lambda函数。但是它没有创建任何文件,也没有发布任何内容,因为我相信这段代码可能会被破坏。不知道是怎么回事,我试着调查了一下cloudwatch,但我没有看到任何事件或错误被报告。
如果能帮上忙,我将不胜感激,
干杯!

最佳答案

关于这个的一些想法。。。
如果在GG组设置中打开本地日志,它将开始在PI上本地写入日志。设置如下:
python - 使用greengrass在本地设备上执行lambda函数-LMLPHP
日志位于:/greengrass/ggc/var/log/system
如果tailthepython_runtime.log可以看到lambda执行中的任何错误。
如果要访问本地资源,则需要在GG组定义中创建资源。然后,您可以将此访问权授予一个卷,在该卷上您可以写入文件。
完成此操作后,您确实需要部署组才能使更改生效。

09-13 09:40