2

I've got the following AWS CLI commands to run the PowerShell's Invoke-WebRequest command:

instanceId=i-0xyz # Change this.
cmdId=$(aws ssm send-command --instance-ids "$instanceId" --document-name "AWS-RunPowerShellScript" --query "Command.CommandId" --output text --parameters commands='"While ($i -le 10) {Invoke-WebRequest -UseBasicParsing -Uri http://example.com; $i += 1}"')
sleep 1
aws ssm list-command-invocations --command-id "$cmdId" --details --query "CommandInvocations[*].CommandPlugins[*].Output[]" --output text

However, the output is truncated:

...
StatusCode        : 200
StatusDescription : OK
Content           : <!doctype html>
                    <html>
                    <head>
                        <title>Example Domain</title>

                        <meta charset="utf-8" />
                        <meta http-equiv="Content-type" content="text/html; 
                    charset=utf-8" />
                        <meta name="viewport" conten...
------------------Output truncated-------------------

How I can run aws ssm list-command-invocations command, so the output won't be truncated?

3

According to this ssm tutorial, SSM send-command output is truncated at 2500 characters. To work around that, you must send your output to an S3 bucket using the --output-s3-bucket-name parameter.

0

Here is a helper Bash script which uses aws ssm send-command with --output-s3-bucket-name parameter to run the command and the result is stored in the S3 bucket, then displayed to the standard output.

#/usr/bin/env bash -xe
# Script to run PowerShell script on the Windows instance, then uploads the output to S3 bucket.
instanceId="$1"
bucketName="$2"
bucketDir="Output"
[ $# -le 2 ] && { echo "Usage: $0 instance_id bucket_name command"; exit 1; }
aws s3 ls ${bucketName} > /dev/null
cmdId=$(aws ssm send-command --instance-ids "$instanceId" --document-name "AWS-RunPowerShellScript" --query "Command.CommandId" --output text  --output-s3-bucket-name "$bucketName" --output-s3-key-prefix "$bucketDir" --parameters commands="'${@:3}'")
while [ "$(aws ssm list-command-invocations --command-id "$cmdId" --query "CommandInvocations[].Status" --output text)" == "InProgress" ]; do sleep 1; done
outputPath=$(aws ssm list-command-invocations --command-id "$cmdId" --details --query "CommandInvocations[].CommandPlugins[].OutputS3KeyPrefix" --output text)
aws s3 ls s3://${bucketName}/${outputPath}/stderr.txt && aws s3 cp --quiet s3://${bucketName}/${outputPath}/stderr.txt /dev/stderr
aws s3 cp --quiet s3://${bucketName}/${outputPath}/stdout.txt /dev/stdout

Example:

./run_ec2_ps_cmd_s3.sh i-0xyz my-bucket-name 'While ($i -le 10) {(Invoke-WebRequest -UseBasicParsing -Uri http://example.com).Content; $i++}'
  • Few remarks: 1. Why the first s3 ls ? this sounds an api query for nothing. 2. Testing InProgress is brittle, ssm commands can be in Pending state and your script will fail. 3. The availability of the files on S3 can be delayed for a few seconds, doing a retry loop is usually usefull to get the content back – Tensibai Feb 21 '18 at 15:06
  • @Tensibai 1. To make sure the bucket exists, otherwise no point in executing the command. 2. So far I've noticed only InProgress, will check if I got Pendingas well, 3. If SSM command has been completed, I think we can assume the files are already there. Didn't have problems so far, althought still testing this script heavily. – kenorb Feb 21 '18 at 15:09
  • That's just what I have observed for my part with run shell script for linux instances :) – Tensibai Feb 21 '18 at 15:12
  • According to this list-command-invocations link, the possible values for Status are InProgress, Success, TimedOut, Cancelled, Failed, so no pending as far as I could find. – kenorb Feb 21 '18 at 15:15
  • Aww, indeed, I'm using list-commands as I target multiples instances, this command has a "Pending" status also. – Tensibai Feb 21 '18 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.