Python os module doesn't work in lambda execution

0

Hello everyone!

I'm currently using Python 3.7 for lambda's development. I want to use the os module from python
in order to execute some commands like reboot, wget or ping but it doesn't do anything. I have also tried with subprocess module, but it neither works.

I don't know whether I'm missing something in the configuration before to do the deployment to my device, or if I need to use a different module for it.

Any idea?

asked 4 years ago2257 views
3 Answers
0

Hi Berssain,

You'll need to change the containerization for your lambda. You want to run it in no container mode. Depending on what system changes you're making you may need the lambda to run as root.
https://docs.aws.amazon.com/greengrass/latest/developerguide/lambda-group-config.html#lambda-containerization-considerations

Thanks,
KR-AWS

AWS
KR-AWS
answered 4 years ago
0

I have tested it and I still have one issue.

In one hand I could successfully reboot the device (a RPi) by changing some parameters:
UID and GID = 0
Containerization = No container (always)

But on the other hand, I'm still not being able of executing wget url. What I want to do is to download a file
from a url and to save it on a specific path. I followed this guide https://docs.aws.amazon.com/greengrass/latest/developerguide/lra-console.html

I tried 4 different things:
1- Doing the same as the first case (without container) and without "Resources" configuration.
2- I added a "Resources" configuration, but the deploy failed with the message: "... function: refers to resource of type 'Local.Volume' that is not allowed in IsolationMode = NoContainer)."
3- Then I set the lambda into a Greengrass container without "Resources" configuration.
4- Finally, with the same Greengrass container, I made a configuration in the "Resources" section.

None of the 4 combinations worked.

The thing is that os module worked when I put the lambda out of a container. But I want to access to local resources, and to do this the lambda needs one greengrass container.

What do you think?

** When I say "Resources configuration", I mean to specify a Resource name, Resource type, Source path, Destination path, Group owner file access permission, and Lambda function affiliations.

Edited by: berssain on Oct 29, 2019 5:40 PM

answered 4 years ago
0

Hi guys, I found one solution.

It's to use requests module instead of os, for downloading files in python3.7.

As I wrote in my last reply, when you configure a lambda to have access to local resources on
the device, it (the lambda) must be embedded into a Greengrass container.
For some actions like rebooting the device, it works setting the lambda out of a container and using
the os module.
But in this case, since I needed special permissions for read/write, it's easier to use the "Resources"
configurations that Greengrass has, and just changing the library/module that you use to do it.

Thanks to KR-AWS for the advice and recommendation. I hadn't taken in consideration the container importance.

I wanted to use os module because it's a little easier. Just by writting: os.system("wget url"),
python emulates a terminal and executes any command. Well, it works fine in a normal desktop, but in
a greengrass container it seems that doesn't work.

Edited by: berssain on Oct 30, 2019 9:50 AM

answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions