Skip to content

How to automatically execute a command on EC2 in AWS?

How to automatically execute a command on EC2 in AWS

If you are using the AWS cloud, you will be pleased to know that you can automatically execute commands on EC2 machines without logging into them. If you haven’t used automation tools like Ansible or Cheff yet, then no problem. With AWS System Manager, you can install services, execute commands, and automate your work.

aws system manager - run command

Permissions for AWS System Manager

To automatically execute commands on your EC2 machines, you need to add the appropriate permissions to them. For a description of how to add permissions that will allow AWS Systems Manager to execute commands, see this article.

Run commands on EC2 machines

Once you have assigned the appropriate role to your machines, you can proceed to automatic command run on EC2😉.

Open AWS System Manager and select “Run Command” from the left menu.

AWS - system manager
aws run command

1) Command document

Now you can click Run Command and select “AWS-RunShellScript” from the command list.

aws run command 2021
aws run shell script

2) Command parameters

Below are 2 examples that should make it easier for you to understand what is happening.

Example 1 – CentOS

In this example, a run command will be added to perform a CentOS update and install Apache web server.

aws run shell script install apache web server

To make it easier, I paste the code so that you don’t have to rewrite it 😉

sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd

Example 2 – Ubuntu

In the second example, the CloudWatch agent will be automatically installed on the Ubuntu machine and its config will be created. Finally, the agent will start up and be able to start sending metrics to CloudWatch. If you are interested in this topic, I have described it in detail in another article. How to monitor RAM usage on AWS EC2.

aws run shell script install cloudWatch agent

To make it easier, I paste the code so that you don’t have to rewrite it 😉

sudo wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
sudo cat <<'EOF' > /opt/aws/amazon-cloudwatch-agent/bin/config.json
{
   "metrics":{
      "metrics_collected":{
         "mem":{
            "measurement":[
               "mem_used_percent"
            ],
            "metrics_collection_interval":320
         }
      },
      "append_dimensions": {
        "InstanceId": "${aws:InstanceId}"
      }
   }
}
EOF

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

3) Targets

Now you select the EC2 machines on which you want to run the commands. You can select them by TAG. However, for the first run, I recommend that you select the instance manually and check that everything starts correctly.

aws run command targets

4) Other parameters

If you need, you can add additional parameters here. Usually there is no need for this with simple commands.

run command other parameters

5) Rate control

Here you can define how many EC2 machines should run the commands simultaneously. Additionally, it is possible to order interrupting the execution of commands with a certain number of errors. Usually you don’t need to change anything here.

run command rate control

6) Output options

If we want, we can write the result to S3. If not, please uncheck “Enable an S3 bucket”.

run command output options

7) SNS notifications

Here you can enable SNS notifications. You don’t need to change anything here if you don’t want to receive them.

run command SNS notifications

8) AWS command line interface command

Here you will find a command that you can copy and execute with the CLI. It contains everything you have set so far. It has been written so that it can be run in the CLI.

run command CLI

Now you have gone through all the “Run a command” settings in AWS. When you click RUN, the commands in the Command parameters section will be executed on the EC2 machines.

Re-executing commands on EC2 machines

If you want to run the same command again and don’t want to type everything from scratch, go to “Command history”. There you will find the commands you run. Select the one you need and click “Copy to new”.

aws command history

Summary

In summary, if you are not yet automating your work, you can use AWS System Manager to make up for it. AWS System Manager will allow automatic execution of commands and installation of services on EC2 machines. Thanks to automation, you can save yourself a lot of repetitive work and time needed for manual execution.

At this link you will find AWS documentation regarding the system manager.