# AWS SSM - Run Script On Remote EC2

> For the complete documentation index, see [llms.txt](https://iqbalaissaoui.com/llms.txt).

To run a script on a remote EC2 instance that has the SSM agent installed, you can use the AWS Systems Manager Run Command feature:

```SHELL
export SCRIPT_CONTENT=$(cat ec2_script.sh)

aws ssm send-command --profile "$AWS_PROFILE" --document-name "AWS-RunShellScript" \
--targets "Key=InstanceIds,Values=$INSTANCE_ID" \
--parameters "commands=['
export VAR_1=VAR_1_VALUE;
export VAR_2=VAR_2_VALUE;
cat > /path/at/destination/ec2/ec2_script.sh << \"EOF\"
$SCRIPT_CONTENT
EOF
chmod +x /path/at/destination/ec2/ec2_script.sh
/path/at/destination/ec2/ec2_script.sh']"
```

