Created
October 15, 2018 08:16
-
-
Save LouisAmon/8f482fe506dc99f1a133a3c2ef1cc4a3 to your computer and use it in GitHub Desktop.
Run a system command in a Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import logging | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
def lambda_handler(event, context): | |
""" | |
Runs a shell command and returns the output code | |
STDOUT & STDERR are logged into Cloudwatch | |
""" | |
cmd = event['cmd'] | |
logger.info('Executing command: {}'.format(cmd)) | |
ret = subprocess.run( | |
args=cmd, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
shell=True, | |
) | |
if ret.stdout: | |
logger.info(ret.stdout) | |
if ret.stderr: | |
logger.error(ret.stderr) | |
return ret.returncode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment