3

I'm running ExecuteCommandCommand successfully using the AWS JavaScript SDK v3, but I'm unable to find out how to log the shell output. The ExecuteCommandCommandOutput interface does not include anything that would point to that, and by logging it after a successful execution I indeed to not see the results.

1 Answer 1

6

I was able to accomplish this in Node using npm packages 'ssm-session' and 'ws'.

First, executing the command using ECS ExecuteCommandCommand:

    const { ECSClient, ListTasksCommand, ExecuteCommandCommand } = require("@aws-sdk/client-ecs");
    const ecs = new ECSClient({ region })
    const executeCommand = new ExecuteCommandCommand( {cluster, interactive: true, command, task})
    const response = await ecs.send(executeCommand)
    const { streamUrl, tokenValue } = response.session

Then, you can use the following snippet to log the output using the streamUrl and tokenValue connected above.

    const WebSocket = require("ws");
    const { ssm } = require("ssm-session");
    const util = require("util");

    const textDecoder = new util.TextDecoder();
    const textEncoder = new util.TextEncoder();

    const termOptions = {
        rows: 34,
        cols: 197,
    };

    const connection = new WebSocket(streamUrl);

    process.stdin.on("keypress", (str, key) => {
        if (connection.readyState === connection.OPEN) {
            ssm.sendText(connection, textEncoder.encode(str));
        }
    });

    connection.onopen = () => {
        ssm.init(connection, {
            token: tokenValue,
            termOptions: termOptions,
        });
    };

    connection.onerror = (error) => {
        console.log(`WebSocket error: ${error}`);
    };

    connection.onmessage = (event) => {
        var agentMessage = ssm.decode(event.data);
        ssm.sendACK(connection, agentMessage);
        if (agentMessage.payloadType === 1) {
            process.stdout.write(textDecoder.decode(agentMessage.payload));
        } else if (agentMessage.payloadType === 17) {
            ssm.sendInitMessage(connection, termOptions);
        }
    };

    connection.onclose = () => {
        console.log("websocket closed");
    };

I hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

This solution is great, but a little out of date. I'm on Node v20, and this worked for me for receiving individual keypresses ``` process.stdin.setRawMode(true); // This will prevent the process for exiting until we explicitly call process.exit() process.stdin.resume(); process.stdin.setEncoding("utf8"); process.stdin.on("data", (key: string) => { if (key === "\u0003") { debug("Killing process with ctrl-c"); process.exit(); } else if (connection.readyState === connection.OPEN) { ssm.sendText(connection, textEncoder.encode(key)); } }); ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.