{"id":7850,"date":"2019-06-19T08:56:42","date_gmt":"2019-06-19T00:56:42","guid":{"rendered":"http:\/\/rmohan.com\/?p=7850"},"modified":"2019-06-21T08:50:54","modified_gmt":"2019-06-21T00:50:54","slug":"aws-simple-systems-manager-ssm","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7850","title":{"rendered":"AWS : SIMPLE SYSTEMS MANAGER (SSM)"},"content":{"rendered":"\n<p>\n\nWhat is Simple Systems Manager<\/p>\n\n\n\n<p>Amazon EC2 Simple Systems Manager (SSM) is an Amazon Web Services tool that allows us to automatically configure virtual servers in a cloud or in on-premises data center.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"187\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/SSM-Commands.png\" alt=\"\" class=\"wp-image-7851\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SSM-Commands.png 515w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SSM-Commands-300x109.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SSM-Commands-150x54.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SSM-Commands-400x145.png 400w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/figure>\n\n\n\n<p>We can use scripts, commands or the Elastic Compute Cloud (EC2) console to manage EC2 instances, virtual machines (VMs) or servers hosted on other clouds, or within local environments such as Windows.<\/p>\n\n\n\n<p>Granting user account access to Systems Manager<\/p>\n\n\n\n<p>Our user account must be configured to communicate with the SSM API.<\/p>\n\n\n\n<p>We need to use the following the procedure to attach a managed AWS Identity and Access Management (IAM) policy to our user account that grants us full access to SSM API actions.<\/p>\n\n\n\n<p>To create the IAM policy for our user account:<\/p>\n\n\n\n<ol><li>Open the IAM console at&nbsp;<a href=\"https:\/\/console.aws.amazon.com\/iam\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/console.aws.amazon.com\/iam\/<\/a>.<\/li><li>In the navigation pane, choose&nbsp;<strong>Policies<\/strong>.<\/li><li>In the&nbsp;<strong>Filter<\/strong>&nbsp;field, type AmazonSSMFullAccess and press Enter.<\/li><li>Select the check box next to AmazonSSMFullAccess and then choose&nbsp;<strong>Policy Actions<\/strong>,&nbsp;<strong>Attach<\/strong>.<\/li><li>On the&nbsp;<strong>Attach Policy<\/strong>&nbsp;page, choose the user account and then choose&nbsp;<strong>Attach Policy<\/strong>.<\/li><\/ol>\n\n\n\n<ul><li><\/li><\/ul>\n\n\n\n<p>AWS Identity and Access Management (IAM)<\/p>\n\n\n\n<p>We must configure an AWS Identity and Access Management (IAM) instance profile role for Systems Manager.<\/p>\n\n\n\n<p>The\u00a0<strong>AmazonEC2RoleforSSM<\/strong>\u00a0role should be attached to an Amazon EC2 instance. Let&#8217;s create it first:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"371\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/SetRoleName-Review.png\" alt=\"\" class=\"wp-image-7852\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SetRoleName-Review.png 729w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SetRoleName-Review-300x153.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SetRoleName-Review-150x76.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SetRoleName-Review-400x204.png 400w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"488\" height=\"177\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/RoleCreated.png\" alt=\"\" class=\"wp-image-7853\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/RoleCreated.png 488w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/RoleCreated-300x109.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/RoleCreated-150x54.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/RoleCreated-400x145.png 400w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/figure>\n\n\n\n<p> Attach the role while the instance is being created: <\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"41\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/AmazonEC2RoleforSSM.png\" alt=\"\" class=\"wp-image-7854\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/AmazonEC2RoleforSSM.png 444w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/AmazonEC2RoleforSSM-300x28.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/AmazonEC2RoleforSSM-150x14.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/AmazonEC2RoleforSSM-400x37.png 400w\" sizes=\"(max-width: 444px) 100vw, 444px\" \/><\/figure>\n\n\n\n<p>This role enables the instance to communicate with the Systems Manager API.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"397\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/1_P1V5a3iC14yL1kkpCo5UTQ-1.png\" alt=\"\" class=\"wp-image-7863\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_P1V5a3iC14yL1kkpCo5UTQ-1.png 800w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_P1V5a3iC14yL1kkpCo5UTQ-1-300x149.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_P1V5a3iC14yL1kkpCo5UTQ-1-768x381.png 768w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_P1V5a3iC14yL1kkpCo5UTQ-1-150x74.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_P1V5a3iC14yL1kkpCo5UTQ-1-400x199.png 400w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"327\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/1_ju3GQygCf5gXPbGnx3EqqA.png\" alt=\"\" class=\"wp-image-7864\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_ju3GQygCf5gXPbGnx3EqqA.png 800w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_ju3GQygCf5gXPbGnx3EqqA-300x123.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_ju3GQygCf5gXPbGnx3EqqA-768x314.png 768w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_ju3GQygCf5gXPbGnx3EqqA-150x61.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/1_ju3GQygCf5gXPbGnx3EqqA-400x164.png 400w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>Install the SSM Agent (Linux)<\/p>\n\n\n\n<p>The SSM agent processes Run Command requests and configures the instances that are specified in the request. The agent is installed, by default, on Windows instance. However, we must manually install the agent on Linux. The following procedure describes how to install the agent on Ubuntu:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd \/tmp\t\t\t\n$ wget https:\/\/s3.amazonaws.com\/ec2-downloads-windows\/SSMAgent\/latest\/debian_amd64\/amazon-ssm-agent.deb\n$ sudo dpkg -i amazon-ssm-agent.deb\n$ sudo systemctl enable amazon-ssm-agent\n<\/pre>\n\n\n\n<p>We can use&nbsp;<strong>User data<\/strong>&nbsp;instead:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\ncd \/tmp\t\t\t\nwget https:\/\/s3.amazonaws.com\/ec2-downloads-windows\/SSMAgent\/latest\/debian_amd64\/amazon-ssm-agent.deb\nsudo dpkg -i amazon-ssm-agent.deb\nsudo start amazon-ssm-agent\n<\/pre>\n\n\n\n<p>We can check if the agent is running on the instance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ps -ef|grep agent | grep -v grep\nroot      1723     1  0 01:13 ?        00:00:00 \/usr\/bin\/amazon-ssm-agent\n<\/pre>\n\n\n\n<p><strong>SSM Agent Installation<\/strong><\/p>\n\n\n\n<ol><li>Access the EC2 instance you have created with the SSH key for the one time SSM agent configuration.<\/li><li>Execute the commands below after you login(sudo) as root.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"># mkdir \/tmp\/ssm<br># cd \/tmp\/ssm<br># yum install -y <a rel=\"noreferrer noopener\" href=\"https:\/\/s3.amazonaws.com\/ec2-downloads-windows\/SSMAgent\/latest\/linux_amd64\/amazon-ssm-agent.rpm\" target=\"_blank\">https:\/\/s3.amazonaws.com\/ec2-downloads-windows\/SSMAgent\/latest\/linux_amd64\/amazon-ssm-agent.rpm<\/a><br># systemctl enable amazon-ssm-agent<br># systemctl start amazon-ssm-agent<br><br><br> <br>Make sure that SSM agent version is 2.3.630 or above.<br><br>[root@ip-172-31-28-88 tmp]# rpm -qa | grep ssm<br> amazon-ssm-agent-2.3.634.0-1.x86_64<br> [root@ip-172-31-28-88 tmp]# <\/pre>\n\n\n\n<p>Sending a Command Using the EC2 Console<\/p>\n\n\n\n<p>We can use the following steps to list all services running on the instance by using Run Command from the Amazon EC2 console.<\/p>\n\n\n\n<p>To execute a command using Run Command from the EC2 console:<\/p>\n\n\n\n<ol><li>In the navigation pane, choose&nbsp;<strong>Run Command<\/strong>:<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"154\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/SMS-Menu-Run-Command.png\" alt=\"\" class=\"wp-image-7855\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SMS-Menu-Run-Command.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/SMS-Menu-Run-Command-146x150.png 146w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/figure>\n\n\n\n<ol><li>Choose&nbsp;<strong>Run a command<\/strong>:<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"281\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/Run-a-command.png\" alt=\"\" class=\"wp-image-7856\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/Run-a-command.png 640w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/Run-a-command-300x132.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/Run-a-command-150x66.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/Run-a-command-400x176.png 400w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<ol><li>For&nbsp;<strong>Command document<\/strong>, choose&nbsp;<strong>AWS-RunPowerShellScript<\/strong>&nbsp;for Windows instances, and&nbsp;<strong>AWS-RunShellScript<\/strong>&nbsp;for Linux instances.<\/li><li>For&nbsp;<strong>Target instances<\/strong>, choose the instance we created. If we don&#8217;t see the instance, verify that we are currently in the same region as the instance we created. Also verify that we configured the IAM role and trust policies as described earlier.<\/li><li>For&nbsp;<strong>Commands<\/strong>, type&nbsp;<strong>Get-Service<\/strong>&nbsp;for Windows, or&nbsp;<strong>ps -aux | less<\/strong>&nbsp;for Linux.<\/li><li>(Optional) For&nbsp;<strong>Working Directory<\/strong>, specify a path to the folder on our EC2 instances where we want to run the command.<\/li><li>(Optional) For&nbsp;<strong>Execution Timeout<\/strong>, specify the number of seconds the EC2Config service or SSM agent will attempt to run the command before it times out and fails.<\/li><li>For&nbsp;<strong>Comment<\/strong>, providing information is recommended so that it will help us identify this command in our list of commands.<\/li><li>For&nbsp;<strong>Timeout (seconds)<\/strong>, type the number of seconds that Run Command should attempt to reach an instance before it is considered unreachable and the command execution fails.<\/li><li>Choose&nbsp;<strong>Run<\/strong>&nbsp;to execute the command. Run Command displays a status screen. Choose View result.<\/li><li>To view the output, choose the command invocation for the command, choose the&nbsp;<strong>Output<\/strong>&nbsp;tab.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"439\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/OutputConsole.png\" alt=\"\" class=\"wp-image-7857\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/OutputConsole.png 621w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/OutputConsole-300x212.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/OutputConsole-150x106.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/OutputConsole-400x283.png 400w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/figure>\n\n\n\n<ol><li>Then choose&nbsp;<strong>View Output<\/strong>.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"673\" height=\"292\" src=\"https:\/\/rmohan.com\/wp-content\/uploads\/2019\/06\/ViewOutput.png\" alt=\"\" class=\"wp-image-7858\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/ViewOutput.png 673w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/ViewOutput-300x130.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/ViewOutput-150x65.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2019\/06\/ViewOutput-400x174.png 400w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/figure>\n\n\n\n<p>\n\nSending a Command via AWS CLI<\/p>\n\n\n\n<p>We must either have administrator privileges on the instances we want to configure or we must have been granted the appropriate permission in IAM.<\/p>\n\n\n\n<p>The following command returns a list of Linux and Windows documents:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm list-documents\nDOCUMENTIDENTIFIERS\tCommand\t1\tAWS-ApplyPatchBaseline\tAmazon\t1.2\nPLATFORMTYPES\tWindows\nPLATFORMTYPES\tLinux\nDOCUMENTIDENTIFIERS\tCommand\t1\tAWS-ConfigureAWSPackage\tAmazon\t2.0\nPLATFORMTYPES\tWindows\nPLATFORMTYPES\tLinux\n...\n<\/pre>\n\n\n\n<p>To check if an instance is ready to receive commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm describe-instance-information --output text --query \"InstanceInformationList[*]\"\n2.0.796.0\tip-172-31-38-206\t172.31.38.206\ti-0698042a954420857\tTrue\t1496457091.34\tOnline\tUbuntu\tLinux\t16.04\tEC2Instance\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Using Run Command and the AWS-RunShellScript document, we can execute any command or script on an EC2 instance as if we were logged on locally.<\/p>\n\n\n\n<p>To view the description and available parameters, we can use the following command to view a description of the Systems Manager JSON document:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm describe-document --name \"AWS-RunShellScript\" --query \"[Document.Name,Document.Description]\"\nAWS-RunShellScript\tRun a shell script or specify the commands to run.\n<\/pre>\n\n\n\n<p>We can use the following command to view the available parameters and details about those parameters:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm describe-document --name \"AWS-RunShellScript\" --query \"Document.Parameters[*]\"\n\t(Required) Specify a shell script or a command to run.\tcommands\tStringList\n\t(Optional) The path to the working directory on your instance.\tworkingDirectory\tString\n3600\t(Optional) The time in seconds for a command to complete before it is considered to have failed. Default is 3600 (1 hour). Maximum is 28800 (8 hours).\texecutionTimeout\tString\n<\/pre>\n\n\n\n<p>We may want to use the following command to get IP information for an instance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm send-command --instance-ids \"i-0698042a954420857\" --document-name \"AWS-RunShellScript\" --comment \"IP config\" --parameters commands=ifconfig --output text\nCOMMAND\te4d8a901-34b7-480d-9e47-f0a71179be64\tIP config\t0\tAWS-RunShellScript\t0\t1496465253.78\t50\t0\t\t1496458053.78\t\tPending\tPending\t1\nINSTANCEIDS\ti-0698042a954420857\nNOTIFICATIONCONFIG\t\t\nCOMMANDS\tifconfig\n<\/pre>\n\n\n\n<p>The following command uses the Command ID that was returned from the previous command to get the details and response data of the command execution. The system returns the response data if the command completed. If the command execution shows &#8220;Pending&#8221; we will need to execute this command again to see the response data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm list-command-invocations --command-id \"e4d8a901-34b7-480d-9e47-f0a71179be64\" --details\n<\/pre>\n\n\n\n<p>The following command displays the default user account running the commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sh_command_id=$(aws ssm send-command --instance-ids \"i-0698042a954420857\" --document-name \"AWS-RunShellScript\" --comment \"Demo run shell script on Linux Instance\" --parameters commands=whoami --output text --query \"Command.CommandId\")\n<\/pre>\n\n\n\n<p>The following command uses the Command ID to get the status of the command execution on the instance. This example uses the Command ID that was returned in the previous command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm list-commands  --command-id $sh_command_id\nCOMMANDS\t136b1a05-6724-45f1-a23b-f98062fca64d\tDemo run shell script on Linux Instance\t1\tAWS-RunShellScript\t0\t1496465641.83\t50\t0\t\t\t1496458441.83\t\tSuccess\tSuccess\t1\nINSTANCEIDS\ti-0698042a954420857\nNOTIFICATIONCONFIG\t\t\nCOMMANDS\twhoami\n<\/pre>\n\n\n\n<p>The following command uses the Command ID from the previous command to get the status of the command execution on a per instance basis:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ aws ssm list-command-invocations --command-id $sh_command_id --details<\/pre>\n","protected":false},"excerpt":{"rendered":"\n<p> What is Simple Systems Manager<\/p>\n<p>Amazon EC2 Simple Systems Manager (SSM) is an Amazon Web Services tool that allows us to automatically configure virtual servers in a cloud or in on-premises data center.<\/p>\n<p>We can use scripts, commands or the Elastic Compute Cloud (EC2) console to manage EC2 instances, virtual machines (VMs) or [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7850"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7850"}],"version-history":[{"count":4,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7850\/revisions"}],"predecessor-version":[{"id":7878,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7850\/revisions\/7878"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}