0

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

The policy attached to the lambda function is as follows:

 - PolicyName: CoreFunctionality
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: "*"
              - Effect: Allow
                Action:
                  - s3:GetObject
                  - s3:PutObject
                  - s3:DeleteObject
                  - s3:ListBucket
                Resource:
                  - !Sub "${S3Bucket.Arn}/*"
                  - !Sub "${S3Bucket.Arn}"

I've omitted the secrets manager and obtaining the secret value, to be safe.

  • Hmm, try changing `logging.debug` to `logging.error` since that's more appropriate here. Also debug logs don't get sent to Cloudwatch, at least by default. – rv.kvetch Oct 26 '21 at 14:18
  • Where, other than the exception handler, are you writing the process output so that it can be captured? – Parsifal Oct 26 '21 at 16:24
  • What version of python are you using for your runtime in lambda? The capture_output option is only available in 3.7 or higher. You could try subprocess(check_output) to see if you get a different result. – Chris Lindseth Oct 27 '21 at 14:02

0 Answers0