Using System Manager to run commands on EC2 instance AWS Systems Manager gives you visibility and control of your infrastructure on AWS. Systems Manager provides a unified user interface so you can view operational data from multiple AWS services and enables you to automate operational tasks across your AWS resources. In this project, we will be going on a journey that will take you through your first AWS Systems Manager deployment, and it is an honor to be accompanying you. In this project, we will be going on a journey that will take you through your first AWS Systems Manager deployment, and it is an honor to be accompanying you.
Step 1 : IAM Role creation.
Okay lets get started, for us to be able to use Systems Manager to perfom operations we first need to create an IAM Role Profile for the EC2 instances.
Head over to AWS management console click the search box and type IAM, on the IAM console page at the left side you will see Roles under Access management.
Here click the create Role at the right top corner. Under Trusted entity type select AWS service, scroll down to use cases and select EC2. Now in the permissions policies search for “AmazonEC2RoleforSSM” There is a message displaying under when you select the policy saying the policy will soon be deprecated. So we are told to use “AmazonSSMManagedInstanceCore” policy instead. Now go back to the permissions policy and search for “AmazonSSMManagedInstanceCore” click the check box at the left to select it. Now give your Role a name and description. Verify that it’s the correct policy that is selected, now scroll down and click “create role”.
STEP 2 : EC2 Instance creation Here we launch instances that will be updated using systems manager.
Click launch instance.
Name your instance Select an operating system for your instance, leave the default AMI and Architecture
Under instance type select the “Free tier eligible” ones, under keypair login select “ Proceed without a key pair”.
Under Network settings click “ Select existing security group”.
Select your default security group.
Scroll down to “Advance Details” under “IAM instance profile” click and select the IAM role you created earlier.
Scroll down to “user data” and paste the following bash script from the Bash script file in the directory
Note: This script is as a result of the operating system I selected which is Redhat as a result system manager agent is not pre-installed in it.
So the script downloads and installs the systems manager agent and starts it.
Now in number of instance type 3 click launch instance.
Great !!! you have successfully launch your instances.
STEP 3: USING SYSTEMS MANAGER TO RUN COMMANDS ON YOUR INSTANCE
Now head over to the Systems Manager, click Fleet Manager at the left side of the screen.
Here your instances with Systems Manager Agent installed will be displayed. Select all three instances.
NOTE: If your instances are not displaying here that means the Systems Manager Agents was not successfully installed, so conect to the instance using SSH, and install the Systems Manager Agent for each instances.
After selecting your instances click node action, select “Tools” and click “ Execute run command”.
In the search bar type in the type of command you want to excute.
For this project we will be using “AWS-RunShellScript” command, so type in the command select it from drop down list.
Now scroll down to the command pallete,here we will writing a command to update the instances. Type “sudo yum update -y” .
Scroll down to target selection and select the instances that you want the command to be executed on.
After selecting your targets click “Run”.
Your command status should show “ In progress”.
If successful the overall status changes to “ Success”
Congratulations you have successfully used system manager to update a set of instances.
STEP 4
You head over to EC2, select one of the instances and connect using session manager, run the command “sudo yum update -y” you should a message saying “Nothing to do”.
Now you head over to step x in the command pallete type in “sudo shutdown” scroll down and select your targets which are the instances you want to shutdown, scroll and click run. Wait for the overall status to display success.
Congratulations you heard over to the EC2 console to terminate your instances.