我正在尝试学习AWS greengrass,所以我遵循了这个教程https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-gs.html,它一步一步地解释了如何在覆盆子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内核会尝试在本地设备上运行这个lambda函数。但是,它既没有创建任何文件,也没有发布任何内容,因为我认为这段代码可能会被破坏。不知道是怎么回事,我试着查看cloudwatch,但我没有看到任何事件或错误被报告。
在这方面的任何帮助都将非常感谢,干杯!
发布于 2018-01-27 03:10:02
我想我找到了答案,我们必须在lambda环境中创建资源,并确保授予lambda读写访问该资源的权限。默认情况下,lambda只能访问/tmp文件夹。
以下是指向文档的链接
https://docs.aws.amazon.com/greengrass/latest/developerguide/access-local-resources.html
发布于 2018-05-10 22:35:39
关于这一点有几点想法...
如果您在您的GG组中打开本地日志,它将开始在您的PI上本地写入日志。设置为:

日志位于:/greengrass/ggc/var/log/system
如果您对python_runtime.log执行tail,您可以看到任何来自lambda执行的错误。
如果您想要访问本地资源,则需要在GG组定义中创建一个资源。然后,您可以将此访问权限授予一个卷,您可以在该卷中写入文件。
完成此操作后,您需要部署您的组,以使更改生效。
https://stackoverflow.com/questions/48428838
复制相似问题