简体   繁体   中英

I have enabled subprocess output in my AWS lambda - why is this log output not being pushed to CloudWatch?

I have a piece of code which is failing and I am trying to debug using AWS Cloudwatch logs. The piece of code, with logging enabled is as follows:

def run_command(args, cwd):
try:
        proc = subprocess.run(args,
            cwd=cwd,
            timeout=720,
            check=True,
            capture_output=True)
        stdout = proc.stdout
        stderr = proc.stderr
    except subprocess.TimeoutExpired as e:
        logging.debug(proc.stdout)
        print(e.output)
        print(e)

I expect the subprocess output to be displayed in my AWS Cloudwatch logs. However, here is the output:

START RequestId: 1234567 Version: $LATEST
Command '['my command']' returned non-zero exit status 1.
END RequestId: 1234567

I am running the function like: run_command('ls',cwd) . I am expecting more information on the second line, as I added the capture_output=True statement. Why is the output of the subprocess command not being logged to my Cloudwatch group, and is it possible to do this?

Thanks

Do quick test and ensure that your Lambda function has been assigned the AWSLambdaBasicExecutionRole.

Also, There's a very good article from this guy named Paul Singman. He explains why we shouldn't be using the print to log. Check here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM