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!