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 :
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 :
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
and then select below policy (AmazonEC2RoleforSSM).
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.
Step 3: Select Command document “AWS-RunShellScript”.
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.
Step 5 : Write your shell script you want to execute.
Step 6 : Mention the working directory where command should execute and command timeout.
Step 7 : Execute the command. Verify the execution by logging into EC2 instance or the way as per your ease.
Step 8 : You can verify the Command execution in Command History Section of AWS Console too.
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.
Share with your network.