How to run commands remotely on an EC2 Instance with AWS Systems Manager
Tags: ec2, systemsmanager, iam, role
Published: 2024-03-07 23:14:13

**Systems Manager** is a good choice when you need to view operation data for groups of resources, automate operational actions, understand and control the current state of your resources, manage hybrid environments, and maintain security and compliance.
- Using the run command, one of the automation features of Systems Manager, you can simplify management tasks by eliminating the need to use bastion hosts, SSH, or remote PowerShell.

In this article, I am going to show you how to update the packages on an EC2 instance. At first, you will create an Identity and Access Management (IAM) role, enable an agent on your instance that communicates with Systems Manager, then follow best practices by running the AWS-UpdateSSMAgent document to upgrade your Systems Manager Agent, and finally use Systems Manager to run a command on your instance.

Please visit my **[GitHub Repository for EC2 articles](https://github.com/awsmine/EC2_projects)** on various topics being updated on constant basis.

_**Let’s get started!**_


# Objectives:

**1.** Create an Identity and Access Management (IAM) role

**2.** Create an EC2 instance

**3.** Update the Systems Manager Agent

**4.** Run a remote shell script

# Pre-requisites:
- AWS user account with admin access, not a root account.
- Create an IAM role


# Resources Used:
**[IAM](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)**
**[EC2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)**
**[AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-state.html)**


# Steps for implementation to this project:

## 1. Create an Identity and Access Management (IAM) role


**1**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3x1j0087pnydt7klw3b.png)

**2**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7m03ux08ovgcpyaawuz.png)

**3**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcxyarodxcc9f0o7ibq9.png)
- Next

**4**




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tflsykcnavp2kds2kkyq.png)


**5**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhh44vu95aiztb085esd.png)



**6**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afpes1ewa2ybf1pctyow.png)



- Create role

## 2. Create an EC2 instance

Attach a Systems Manager role to Amazon Elastic Compute Cloud (Amazon EC2) instances to make them managed nodes.

**1**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c578sdb8f03nbdpucz4.png)

**2**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/117w29q5fr1k4nwkp59t.png)


**3**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auhm5vg3i74mstunpnk3.png)



**4**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qdquvp5os3xztudkaa4.png)

**5**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20o7p5510v572stiu4ws.png)

**6**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jg3uszkexcpiu3l0dc4.png)

**7**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5txgpvpg3z9bf3td3nfk.png)






## 3. Update the Systems Manager Agent

Now that you have an EC2 instance running the Systems Manager agent, you can automate administration tasks and manage the instance. In this step, you run a pre-packaged command, called a document, that will upgrade the agent. It is best practice to update the Systems Manager Agent when you create a new instance.

**1**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l68cj11kq5u42hz2w16u.png)

**2**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ymx8qt5nhoedmbhg17y.png)

**3**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pabt0pqx75v28tkm13mu.png)


**4**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxquls5mzkfd8vci60lh.png)

**5**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yin8cdl0k8xrwirri72j.png)

**6**
- type in AWS-UpdateSSMAgent
- then press Enter
- select the radio button on the left of AWS-UpdateSSMAgent. 
- This document will upgrade the Systems Management agent on the instance

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/051vdytnid7yg55mzkw3.png)

**7**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzk68e9dlf8fvdm2pfj7.png)
- Run

**8**
- you will see a page documenting your running command, and then overall success in green.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omau2i6m2z05wiekh7hh.png)

**9**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/745g0hp16rjdxexh2949.png)



## 4. Run a remote shell script

Now that your EC2 instance has the latest Systems Manager Agent, you can upgrade the packages on the EC2 instance. 
- In this step, you will run a shell script through Run Command.

**1**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfihkzqbw8mon3hkj7ip.png)

**2**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1eqjnupfxpt3cwn10en.png)

**3**

- On the Run a command page, click in the search bar and 
- select, Document name prefix, 
- then click on Equals, 
- then type in AWS-RunShellScript
- then press Enter

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wiave23irp0772z3yufo.png)

**4**

- On the Command Parameters panel and insert the following command in the Commands text box:

`sudo yum update`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxb8oxkrk7hf0044k088.png)



**5**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fltlqqrll92046tlq7xg.png)
- Run

**6**

- While your script is running remotely on the managed EC2 instance, the Overall status will be `In Progress`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mxb3h3rpt3sgvzvyr8nv.png)

**7**

- Then the Overall status will turn to `Success`. 
- When it does, scroll down to the `Targets and outputs panel` and select the Instance ID of your instance. 
- Your `Instance ID` will be different than the one pictured.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3buvnibc050acqdrb6h.png)

**8**

- select the header of the Output panel to view the output of the update command from the instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vynycu45sw51kkw8gje1.png)


# Cleanup
Delete EC2 Instance

# What we have done so far

Successfully created an EC2 instance and remotely run a command using AWS Systems Manager. You first set up a correct role/permissions through IAM. Next you launched an Amazon Linux instance that was preinstalled with the Systems Manager agent. Finally, you used Run Command to update the agent and remotely perform a yum update.