<p>Here is a helper Bash script which uses <a href="https://docs.aws.amazon.com/cli/latest/reference/ssm/send-command.html" rel="noreferrer"><code>aws ssm send-command</code></a> with <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/tutorial_run_command.html" rel="noreferrer"><code>--output-s3-bucket-name</code></a> parameter to run the command and the result is stored in the S3 bucket, then displayed to the standard output.</p>

<pre><code>#/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 ] &amp;&amp; { echo "Usage: $0 instance_id bucket_name command"; exit 1; }
aws s3 ls ${bucketName} &gt; /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 &amp;&amp; aws s3 cp --quiet s3://${bucketName}/${outputPath}/stderr.txt /dev/stderr
aws s3 cp --quiet s3://${bucketName}/${outputPath}/stdout.txt /dev/stdout
</code></pre>

<p>Example:</p>

<pre><code>./run_ec2_ps_cmd_s3.sh i-0xyz my-bucket-name 'While ($i -le 10) {(Invoke-WebRequest -UseBasicParsing -Uri http://example.com).Content; $i++}'
</code></pre>

CC BY-SA 3.0
2018-02-21T15:06:08.223Z
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
CC BY-SA 3.0
2018-02-21T15:09:10.960Z
@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 `Pending`as 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
CC BY-SA 3.0
2018-02-21T15:12:37.403Z
That's just what I have observed for my part with run shell script for linux instances :)
Tensibai
CC BY-SA 3.0
2018-02-21T15:15:37.287Z
According to this [`list-command-invocations` link](https://docs.aws.amazon.com/cli/latest/reference/ssm/list-command-invocations.html), the possible values for Status are `InProgress, Success, TimedOut, Cancelled, Failed`, so no pending as far as I could find.
kenorb
CC BY-SA 3.0
2018-02-21T15:23:19.790Z
Aww, indeed, I'm using [`list-commands`](https://docs.aws.amazon.com/cli/latest/reference/ssm/list-commands.html) as I target multiples instances, this command has a "Pending" status also.
Tensibai
CC BY-SA 3.0
2018-02-21T15:32:35.423Z
BTW from the link you gave, there's more statuses then those you quoted, I'm pretty sure I saw teh same as you on first click so I don't exactly get why I don't find it the same, the reference seems to come from: https://docs.aws.amazon.com/systems-manager/latest/userguide/monitor-about-status.html
Tensibai
CC BY-SA 3.0
2018-02-21T13:52:37.890Z
2018-02-21T13:58:17.803Z
2018-02-21T13:58:17.803Z
kenorb
<p>If someone is still running into this issue another option is to gzip the output at source and decompress after pulling it back.</p>
<p>Following is an example in Python running command in Linux EC2 instance -</p>
<pre><code>command = 'set -o pipefail; cat my_large_file.txt | gzip | base64 -w0'
ssm_client.send_command(
    InstanceIds=[ec2_instance_id],
    DocumentName=&quot;AWS-RunShellScript&quot;,
    Parameters={'commands': [command]}, )

cmd_out = self._ssm_client.get_command_invocation(
    CommandId=command_id,
    InstanceId=ec2_instance_id,
)

# Decompress gzipped output 
result =  zlib.decompress(base64.b64decode(cmd_out['StandardOutputContent']), 16 + zlib.MAX_WBITS).decode('utf-8')
</code></pre>
<p>The limit is 24k chars as mentioned <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ssm/client/get_command_invocation.html" rel="nofollow noreferrer">here</a> in AWS Boto documentation.</p>

CC BY-SA 4.0
2024-05-24T15:59:01.343Z
this sounds like a good suggestion that might get the job done, can you add an example of doing it to make it a better looking answer?
Peter Turner
CC BY-SA 4.0
2024-05-24T14:37:29.403Z
2024-05-25T23:00:12.447Z
2024-05-25T23:00:12.447Z
webh
<p>According to this <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/tutorial_run_command.html" rel="noreferrer">ssm tutorial</a>, SSM send-command output is truncated at 2500 characters.  To work around that, you must send your output to an S3 bucket using the <code>--output-s3-bucket-name</code> parameter.</p>

CC BY-SA 3.0
2018-02-20T18:56:49.043Z
2018-02-20T18:56:49.043Z
swysocki
<p>I've got the following AWS CLI commands to run the PowerShell's <code>Invoke-WebRequest</code> command:</p>

<pre><code>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
</code></pre>

<p>However, the output is truncated:</p>

<pre><code>...
StatusCode        : 200
StatusDescription : OK
Content           : &lt;!doctype html&gt;
                    &lt;html&gt;
                    &lt;head&gt;
                        &lt;title&gt;Example Domain&lt;/title&gt;

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

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

<aws-cli>
How to avoid output being truncated when running AWS SSM command?
kenorb
# How to avoid output being truncated when running AWS SSM command?

(Asked by: username_0 on 2018-02-19)

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           : 
                    
                    
                        Example Domain

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

**Tags:** aws-cli


---

## 3 answers

### Answer by username_4 on 2018-02-20. Score: 6 | Accepted answer

According to this [ssm tutorial](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/tutorial_run_command.html), 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.


---

### Answer by username_0 on 2018-02-21. Score: 5

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++}'

> **Comments:**
> **username_1**: 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  
> **username_0**: @username_1 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 `Pending`as 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.  
> **username_1**: That's just what I have observed for my part with run shell script for linux instances :)  
> **username_0**: According to this [`list-command-invocations` link](https://docs.aws.amazon.com/cli/latest/reference/ssm/list-command-invocations.html), the possible values for Status are `InProgress, Success, TimedOut, Cancelled, Failed`, so no pending as far as I could find.  
> **username_1**: Aww, indeed, I'm using [`list-commands`](https://docs.aws.amazon.com/cli/latest/reference/ssm/list-commands.html) as I target multiples instances, this command has a "Pending" status also.  
> **username_1**: BTW from the link you gave, there's more statuses then those you quoted, I'm pretty sure I saw teh same as you on first click so I don't exactly get why I don't find it the same, the reference seems to come from: https://docs.aws.amazon.com/systems-manager/latest/userguide/monitor-about-status.html  

---

### Answer by username_2 on 2024-05-24. Score: 1

If someone is still running into this issue another option is to gzip the output at source and decompress after pulling it back.
Following is an example in Python running command in Linux EC2 instance -
command = 'set -o pipefail; cat my_large_file.txt | gzip | base64 -w0'
ssm_client.send_command(
    InstanceIds=[ec2_instance_id],
    DocumentName="AWS-RunShellScript",
    Parameters={'commands': [command]}, )

cmd_out = self._ssm_client.get_command_invocation(
    CommandId=command_id,
    InstanceId=ec2_instance_id,
)

# Decompress gzipped output 
result =  zlib.decompress(base64.b64decode(cmd_out['StandardOutputContent']), 16 + zlib.MAX_WBITS).decode('utf-8')

The limit is 24k chars as mentioned here in AWS Boto documentation.

> **Comments:**
> **username_3**: this sounds like a good suggestion that might get the job done, can you add an example of doing it to make it a better looking answer?  

---

