Amazon EC2 Run Command

Spread the love


Hi,

In this post of devOpsJourney , you will see how to setup and use “Amazon EC2 Run Command”.

Now you can  run commands on EC2 instances using Commands page available on AWS EC2 Console.

You can also  execute commands using the CLI or programmatically using the SSM (Simple System Manager) API.

What commands you can run ?

In this article we’ll show you the simple bash commands but you can run all mentioned below :

runcommand-0

We are going to use AWS-RunShellScript in this post to demonstrate Command Feature.

Let’s go step by step.

Step 1:  Provision an EC2 instance with below mentioned userdata and IAM role. User data will install the SSM Agent which is required to execute commands from AWS Console or API. You can also pre-baked the SSM agent in your AMI as per your ease.

 

User Data :

#!/bin/bash
cd /tmp
curl https://amazon-ssm-us-east-1.s3.amazonaws.com/latest/linux_amd64/amazon-ssm-agent.rpm -o amazon-ssm-agent.rpm
yum install -y amazon-ssm-agent.rpm

 

Click on create role in IAM and then select Amazon EC2 role for Simple Systems Manager

runcommand-0-1

and then select below policy (AmazonEC2RoleforSSM).

runcommand-0-2

IAM role is required so that you can provide access to SSM to execute commands on EC2.

 

Step 2 : Click on Run a Command from EC2 console.

runcommand-1

 

Step 3: Select Command document “AWS-RunShellScript”.

runcommand-2

 

Step 4 : Select your EC2 instance on which you want to run the command. If EC2 instance is not visible it means SSM Agent or IAM role or both are not configured properly.

runcommand-3

 

Step 5 : Write your shell script you want to execute.

runcommand-4

 

 

Step 6 : Mention the working directory where command should execute and command timeout.

runcommand-5

Step 7 : Execute the command. Verify the execution by logging into EC2 instance or the way as per your ease.

runcommand-6

Step 8 : You can  verify the Command execution in Command History Section of AWS Console too.

runcommand-7

So In this way , we can setup and use “AWS Run Command”.

 

This was all about our post. Let us know if you have any questions, suggestions, feedback. We ‘ll surely look into this.




Thanks.

— Ishant
Share with your network.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *