AWS systems manager to run operations

Abhishek Jakhotiya
3 min readFeb 18, 2023

We host Ecommerce stores on our EC2 instances. A lot of times we need to make modifications to our servers. Logging in to each instance and doing operations is tedious. Imagin having to login to 20+ servers and changing into directories and running same commands over and over. Tedious right! Systems Manager and Shell scripts to rescue.

Consider following problem. I want to run a query on all stores and store the output on s3.

This is a two part problem.

  1. One EC2 contains multiple websites, so change into each directory and run the query
  2. There are 20+ servers, we need to run step 1 on all of those servers.

We will solve first problem one with following shell script

#!/bin/bash

## Use following snippets to run queries on all servers using systems manager

## LOOP OVER ALL STORE DIRECTORIES

for dir in /www/*/public/; do
cd $dir/;
echo "Running in $dir";
if [[ -e "bin/magento" ]]; then
# example mysql query below
mysql -u user -p dbname 'SELECT value FROM `table_name`;';
fi
done;

Now as to our second problem, we are going to run this script on our selected servers using AWS Systems Manager

You can go to Systems Manager -> Run Command menu

Systems manager -> Run command screen

Click on “Run command” and find option for “AWSRunShellScript”

Scroll down and paste your bash script in the white text box

Now select the instances manually or using using instance tags

Now just hit the “Run” button at the bottom.

You will be redirected to screen as following, where you can see the output

You can also check command run history

Remember, each command in the shell script is executed as a root user. So if you want to run the commands as some other user, remember to prefix your command with sudo -u <youruser> yourcommand`

You can read more about AWS Systems Manager RunShellCommand

https://docs.aws.amazon.com/systems-manager/latest/userguide/run-command.html

--

--