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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With