0

Please note, we are using aws-lambda to execute a php script using python subprocess popen function. We used the following code to call the file in python popen:

import json
import subprocess

# if the script doesn't need output.

def lambda_handler(event, context):
    cmd = ["/usr/bin/php74/", "/home/admin/web/path/post.php"]
    proc = subprocess.Popen(cmd, shell=True,stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    script_response = proc.stdout.read(); 

It displays a message indicating that data has been successfully logged, but it fails to execute the script. The script is simple. It is about inserting a row to a table. What shall we do?

Update: Here is the message i'm getting on testing the lambda function:

Response:
null

Request ID:
"37e74321-4232-4d1f-aea8-3d5a82444de2"

Function logs:
START RequestId: 37e74321-4232-4d1f-aea8-3d5a82444de2 Version: $LATEST
END RequestId: 37e74321-4232-4d1f-aea8-3d5a82444de2
REPORT RequestId: 37e74321-4232-4d1f-aea8-3d5a82444de2  Duration: 43.42 ms  Billed Duration: 44 ms  Memory Size: 128 MB Max Memory Used: 53 MB  Init Duration: 113.16 ms    

My verified php script goes here

<?php
$con1 = mysqli_connect("endpoint","###","###","database");
if(mysqli_query($con1,"insert into ex_inbox(time) values ('Done')") or die("the eror ".mysqli_error($con1)));
?>
  • Shouldn't you get an exception with errno 2 or 126? Remove `shell=True` and fix the path for the binary. (It's certainly not a directory.) – mario Dec 24 '20 at 13:33
  • Does this code work on your local machine? – Jarvis Dec 24 '20 at 13:35
  • Basically my environment is php based, i don't have python installed in my local machine. Since aws lambda does not not have php runtime, i'm forced to use python to execute the php script. So no idea whether it works in local machine – husaindevelop Dec 24 '20 at 13:41
  • You are running a Python Lambda and want to run a PHP script. I would think that won't work, because the Python Lambda runtime does not contain the PHP runtime. There should be no `/usr/bin/php74/` and your script `post.php` would not be available in `/home/admin/web/path`. Since the query is extremely simple, why not write it in Python? – Jens Dec 24 '20 at 14:45
  • Does this answer your question? [encountered "sh: php: command not found" error while running os.system module in lambda](https://stackoverflow.com/questions/65445455/encountered-sh-php-command-not-found-error-while-running-os-system-module-in) – Jens Dec 25 '20 at 13:46

1 Answers1

0

AWS lambda environment will expire as soon as the handler method you defined returns, thus running background scripts/processes in your lambda environment directly is not possible. You can work around this issue by manually waiting for some time after you call the PHP script:

proc = subprocess.Popen(cmd, shell=True,stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# wait for your subprocess to complete
time.sleep(10)
script_response = proc.stdout.read(); 
Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • Ok, ran your code. Encountered error "Task timed out after 3.00 second". Still does not insert row in my table. – husaindevelop Dec 24 '20 at 13:49
  • Is this error from your PHP script or python code? @husaindevelop, also try running without `shell=True` – Jarvis Dec 24 '20 at 13:50
  • No, php script is intact, have checked it. Removed the "shell=true", it displays "errorMessage": "[Errno 2] No such file or directory: '/usr/bin/php74/'", – husaindevelop Dec 24 '20 at 13:52
  • You have to increase the timeout parameter of your lambda from its configuration as well (maximum limit is 15 minutes). @husaindevelop – Jarvis Dec 24 '20 at 13:54
  • Thanks for continued replies. Changed it to 14 minutes and 3 seconds in basic configuration setting in lambda. Now, no error shows up, but still no row gets inserted in mysql table. – husaindevelop Dec 24 '20 at 14:02
  • Consider verifying your PHP script and it's connection with the database then. How do you know the script is correct? Are you printing any debugging messages in your PHP script? @husaindevelop – Jarvis Dec 24 '20 at 14:07
  • Verified, you can check the above code. No bragging, but i run hundreds of php queries daily, so i don't think i'm making a mistake there. – husaindevelop Dec 24 '20 at 14:14
  • I don't have experience with PHP but I would suggest putting something like a debug message at the end of PHP script to ensure the script ran completely or keep on increasing the sleep duration in case that is not enough. Or you can try using the `os` module. – Jarvis Dec 24 '20 at 14:20
  • what's os module. Could you give me an example. – husaindevelop Dec 24 '20 at 14:33