{"id":4018,"date":"2015-12-09T11:46:16","date_gmt":"2015-12-09T11:46:16","guid":{"rendered":"http:\/\/blog.cloudthat.com\/?p=4018"},"modified":"2024-06-25T11:13:06","modified_gmt":"2024-06-25T11:13:06","slug":"introducing-run-command-in-ec2","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/introducing-run-command-in-ec2","title":{"rendered":"Introducing Run Command in EC2"},"content":{"rendered":"<p>In order to perform updates, patches, restart a particular process or running a particular powershell script in a Windows based EC2 instance we need to login to the machine and then make the required changes. Sometimes in a huge production environment this tends to be a cumbersome job for managing large fleet of machines. Therefore in order to make our lives easier AWS has introduced a new add-on feature for EC2 called as Command.<\/p>\n<h2>Features of Run Command<\/h2>\n<p>Using this feature we can perform system administrator tasks on Windows based EC2 machines. Currently the following actions are supported in Run Command.<\/p>\n<ul>\n<li>Configuring Cloud Watch<\/li>\n<li>Configuring Windows Update<\/li>\n<li>Install an Application<\/li>\n<li>Install PowerShell Module<\/li>\n<li>Join an EC2 instance to Directory Service Domain<\/li>\n<li>Run a PowerShell script<\/li>\n<li>Update EC2 config<\/li>\n<\/ul>\n<h2>Security of Run Command<\/h2>\n<p>Since Run Command runs from the AWS console and no username and password is required to access the instances, questions may arise as to how safe this feature is? Run Command\u00a0incorporates with IAM policies and roles. Each and every command which is run using Run Command is stored in CloudTrail and also remains in the Console for 30 days.<\/p>\n<p>Run Command shows the output in the console for only 2500 characters and the rest of the output is truncated.\u00a0In order to keep track of all the commands and their detailed output\u00a0we can integrate it with S3 and store the output in form of logs in an S3 bucket.<\/p>\n<h2>Using Run Command to run a PowerShell Script<\/h2>\n<p>We shall see how we can use the Run Command feature to run a PowerShell script on an EC2 instances.<\/p>\n<h4>Pre-Requisites<\/h4>\n<p>In order to setup the EC2 instance to user Run command these are the pre-requisites needed.<\/p>\n<ol>\n<li>Sign into the AWS Management Console and open <strong>IAM<\/strong>.<\/li>\n<li>In the left pane, choose <strong>Policies<\/strong>.<\/li>\n<li>Beside create your own policy click on <strong>Select<\/strong> button.<\/li>\n<li>Enter a Policy name (runcommand-policy) and description.<\/li>\n<li>Write the following policy in the Policy Document field\n<pre class=\"lang:js decode:true\" title=\"runcommand-policy\">{\r\n    \"Version\": \"2012-10-17\",\r\n    \"Statement\": [\r\n        {\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"ssm:DescribeAssociation\",\r\n                \"ssm:GetDocument\",\r\n                \"ssm:ListAssociations\",\r\n                \"ssm:UpdateAssociationStatus\",\r\n                \"ssm:UpdateInstanceInformation\"\r\n            ],\r\n            \"Resource\": \"*\"\r\n        },\r\n        {\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"ec2messages:AcknowledgeMessage\",\r\n                \"ec2messages:DeleteMessage\",\r\n                \"ec2messages:FailMessage\",\r\n                \"ec2messages:GetEndpoint\",\r\n                \"ec2messages:GetMessages\",\r\n                \"ec2messages:SendReply\"\r\n            ],\r\n            \"Resource\": \"*\"\r\n        },\r\n        {\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"ec2:DescribeInstanceStatus\"\r\n            ],\r\n            \"Resource\": \"*\"\r\n        },\r\n        {\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"logs:CreateLogGroup\",\r\n                \"logs:CreateLogStream\",\r\n                \"logs:DescribeLogGroups\",\r\n                \"logs:DescribeLogStreams\",\r\n                \"logs:PutLogEvents\"\r\n            ],\r\n            \"Resource\": \"*\"\r\n        },\r\n        {\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"s3:PutObject\",\r\n                \"s3:GetObject\",\r\n                \"s3:AbortMultipartUpload\",\r\n                \"s3:ListMultipartUploadParts\",\r\n                \"s3:ListBucketMultipartUploads\"\r\n            ],\r\n            \"Resource\": \"*\"\r\n        }\r\n    ]\r\n}<\/pre>\n<p>&nbsp;<\/li>\n<li>Choose <strong>Validate Policy\u00a0<\/strong>and if everything went fine and no error occurs. Click on <strong>Create Policy<\/strong>.<\/li>\n<li>In the same way as mentioned above create a <strong>runcommand-trust <\/strong>policy for the user so that run command can view the instances.\n<pre class=\"lang:js decode:true\" title=\"trust_policy\">{\r\n  \"Version\": \"2012-10-17\",\r\n  \"Statement\": [\r\n    {\r\n      \"Effect\": \"Allow\",\r\n      \"Action\": [\r\n        \"ssm:*\",\r\n        \"ec2:DescribeInstanceStatus\"\r\n      ],\r\n      \"Resource\": \"*\"\r\n    }\r\n  ]\r\n}<\/pre>\n<p>&nbsp;<\/li>\n<li>Attach the User policy <strong>runcommand-trust <\/strong>policy to the\u00a0required IAM user.<\/li>\n<\/ol>\n<h4>Create the Instance Role<\/h4>\n<p>In this task we shall create a role using which the Run Command can access the EC2 instance.<\/p>\n<ol>\n<li>From the <strong>IAM <\/strong>dashboard, Select <strong>Roles\u00a0<\/strong>&gt; <strong>Create Role.<\/strong><\/li>\n<li>On the <strong>Set Role Name<\/strong> page enter a relevant role name and choose <strong>Next Step<\/strong><\/li>\n<li>On the <strong>Select Role Type <\/strong>page, choose the <strong>Next <\/strong>button beside<strong>\u00a0Amazon EC2<\/strong><\/li>\n<li><span style=\"color: #444444;\">On the\u00a0<\/span><strong><span class=\"guilabel\" style=\"color: #444444;\">Attach Policy<\/span><\/strong><span style=\"color: #444444;\">\u00a0page, select the\u00a0runc<\/span><span class=\"italic\" style=\"color: #444444;\">ommand-<\/span><span style=\"color: #444444;\">policy you created earlier.Choose\u00a0<\/span><span class=\"guilabel\" style=\"font-weight: bold; color: #444444;\">Next Step<\/span><\/li>\n<li>Review the role information and click\u00a0<strong>Create Role<\/strong><\/li>\n<\/ol>\n<h4>Launch an EC2 instance<\/h4>\n<p>Launch and EC2 instance and make sure you attach the IAM Role we created in the previous steps. Refer the Diagram and launch an EC2 instance.<br \/>\n<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/1118.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4028\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/1118.png\" alt=\"1\" width=\"940\" height=\"523\" \/><\/a><\/p>\n<h2>Configuring Run Command<\/h2>\n<p>Open the Amazon Management Console and click on <strong>Commands<\/strong>\u00a0in the navigation pane as shown in the figure below.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/210.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4029\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/210-1024x566.png\" alt=\"2\" width=\"940\" height=\"519\" \/><\/a><\/p>\n<ol>\n<li>Click on <strong>Run a command.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/1312.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4030\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/1312.png\" alt=\"3\" width=\"940\" height=\"521\" \/><\/a><\/strong><\/li>\n<li>You will get the following page. <strong>Click\u00a0<\/strong>on the drop down menu in <strong>Command Document\u00a0<\/strong>and select <strong>AWS-RunPowerShellScript.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/44.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4031\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/44.png\" alt=\"4\" width=\"940\" height=\"158\" \/><\/a><\/strong><\/li>\n<li>In <strong>Target Instances\u00a0<\/strong>select the instances which were launched using the <strong>IAM role of Run-Command\u00a0<\/strong><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/53.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4032\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/53.png\" alt=\"5\" width=\"940\" height=\"235\" \/><\/a><\/li>\n<li>In the Commands text\u00a0field type the following code to install IIS.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/165.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4033\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/165.png\" alt=\"6\" width=\"940\" height=\"213\" \/><\/a><\/li>\n<li>Give the appropriate S3 bucket name and the S3 key prefix for the folder inside the bucket to keep the log files in. (Note: S3 bucket and Run Command should be run in the same region) and click on <strong>Run <\/strong>button.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/75.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4034\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/75.png\" alt=\"7\" width=\"940\" height=\"280\" \/><\/a><\/li>\n<li>If everything is configured properly you will get the following screen. Click on view result.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/85.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4036\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/85.png\" alt=\"8\" width=\"940\" height=\"349\" \/><\/a><\/li>\n<li>You can see that there are two commands which have been executed in both of the instances.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/96.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4037\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/96-1024x159.png\" alt=\"9\" width=\"940\" height=\"145\" \/><\/a><\/li>\n<li>Click on one of the commands and click on the output tab.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/104.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4038\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/104-1024x189.png\" alt=\"10\" width=\"940\" height=\"173\" \/><\/a><\/li>\n<li>Click on view output to see the output after running this command.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/119.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4040\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/119.png\" alt=\"11\" width=\"940\" height=\"215\" \/><\/a><\/li>\n<li>The following output from the PowerShell command prompt is displayed in the console.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/126.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4039\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/126-1024x513.png\" alt=\"12\" width=\"940\" height=\"470\" \/><\/a><\/li>\n<li>You can also see that the output is stored in S3 Bucket as well with both stdout and stderr.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/136.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4041\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/136-1024x540.png\" alt=\"13\" width=\"940\" height=\"495\" \/><\/a><\/li>\n<li>Ping the Public IP of one of the EC2 instance and you can see the IIS Webserver installed.(Make sure port 80 is open to see the IIS Webserver).<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/144.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4042\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/144-1024x577.png\" alt=\"14\" width=\"940\" height=\"529\" \/><\/a><\/li>\n<\/ol>\n<h2>Regions<\/h2>\n<p>The Run Command is available only in the following region.<\/p>\n<ul>\n<li>North Virginia<\/li>\n<li>Oregon<\/li>\n<li>Ireland<\/li>\n<\/ul>\n<h2>Limitations<\/h2>\n<p>There are also a few limitations to the EC2 run command as follows<\/p>\n<p>The Run Command<\/p>\n<ul>\n<li>Works only on Windows based EC2 instances.<\/li>\n<li>Works only on instances launched with the Run Command Role<\/li>\n<li>Does not work on Linux based instances.<\/li>\n<\/ul>\n<h2>Pricing<\/h2>\n<p>Run Command does not\u00a0have any charge\u00a0beyond the standard usage charges for Amazon EC2, Amazon S3, and other AWS services\u00a0that are used with this feature.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":219,"featured_media":0,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3606,3607,3665,3818],"user_email":"prarthitm@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/4018"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/219"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=4018"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/4018\/revisions"}],"predecessor-version":[{"id":45695,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/4018\/revisions\/45695"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=4018"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=4018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}