Skip to content

Instantly share code, notes, and snippets.

@LouisAmon
Created October 15, 2018 08:16
Show Gist options
  • Save LouisAmon/8f482fe506dc99f1a133a3c2ef1cc4a3 to your computer and use it in GitHub Desktop.
Save LouisAmon/8f482fe506dc99f1a133a3c2ef1cc4a3 to your computer and use it in GitHub Desktop.
Run a system command in a Lambda
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