rails-lambda / lambda-console

☁️🐚 A CLI to run shell commands or interact with your application on Lambda.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Basic JavaScript Runner

metaskills opened this issue · comments

Here is a simple handler I cooked up in a node project.

import env from "./env.js";
env.rollbar;
import { execSync } from "child_process";

export const handler = async (event) => {
  const cwd = process.env.LAMBDA_TASK_ROOT || process.cwd();
  const command = event?.X_LAMBDA_CONSOLE?.run;
  if (command) {
    try {
      const options = {
        cwd,
        env: process.env,
        stdio: ["ignore", "pipe", "pipe"], // stdin, stdout, stderr
        encoding: "utf-8",
      };
      const stdout = execSync(command, options);
      return { statusCode: 0, headers: {}, body: stdout };
    } catch (error) {
      const stderr = error.stderr.toString();
      return { statusCode: 1, headers: {}, body: error.stderr.toString() };
    }
  } else {
    return {
      statusCode: 1,
      headers: {},
      body: `Command not found\n`,
    };
  }
};