{"id":307,"date":"2022-05-19T04:43:02","date_gmt":"2022-05-19T04:43:02","guid":{"rendered":"http:\/\/checkmateq.com\/blog\/?p=307"},"modified":"2024-10-19T17:01:01","modified_gmt":"2024-10-19T17:01:01","slug":"aws-cli-commands","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/aws-cli-commands","title":{"rendered":"AWS CLI commands to get ec2 instance list"},"content":{"rendered":"<p>There are more than one ways to use amazon web services Web console, boto3 (aws sdk for python), AWS Command Line Interface (AWS CLI).<\/p>\n<p><a href=\"https:\/\/www.checkmateq.com\/aws-cloud\">AWS<\/a> CLI enables users to connect and interact with amazon web services using commands from their terminal, with minimal configuration steps one can start running commands to implement functionalities equivalent to that provided by Web console.<\/p>\n<p>Steps to install the latest version of <a href=\"https:\/\/www.checkmateq.com\/cloud\">AWS Cloud<\/a> Command Line Interface on Linux operating system. if you are installing CLI on Linux 64bit use below link to download, copy and paste it in your terminal<\/p>\n<pre>curl \"https:\/\/awscli.amazonaws.com\/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"<\/pre>\n<p>above link will download a compressed installer file, use &#8220;unzip&#8221; or any other similar command to unzip the\u00a0 installer package.<\/p>\n<pre>unzip awscliv2.zip<\/pre>\n<p>above command will unzip the installer and will create directory named &#8220;aws&#8221; in the present working directory, run the installation command below after unzipping the installer,\u00a0 this command uses a file\u00a0 &#8220;install&#8221; from the newly created aws directory, all the files are installed to\u00a0 &#8220;\/usr\/local\/aws-cli&#8221; .<\/p>\n<pre>sudo .\/aws\/install<\/pre>\n<p>&nbsp;<\/p>\n<p>please check the installation by using the command &#8220;aws &#8211;version&#8221; should get output similar to snapshot below if properly installed<img loading=\"lazy\" class=\" wp-image-310 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-ver-300x30.jpeg\" alt=\"\" width=\"720\" height=\"72\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-ver-300x30.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-ver-1024x101.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-ver-768x76.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-ver-1536x151.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-ver-1200x118.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-ver.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now to configure the AWS CLI installation you need to have Access key ID, Secret access key, these details can be obtained by creating a IAM user ,follow below steps to create a IAM user.<\/p>\n<p>Login to your aws console , search for iam in search bar and click on IAM as shown in the below image<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-311 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-1-300x153.jpeg\" alt=\"\" width=\"871\" height=\"444\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-1-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-1-1024x522.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-1-768x392.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-1-1536x783.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-1-1200x612.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>on the left pane click on users under access management<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-312 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-2-300x153.jpeg\" alt=\"\" width=\"776\" height=\"396\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-2-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-2-1024x524.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-2-768x393.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-2-1536x786.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-2-1200x614.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-2.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In step-1 add the user and select the access type as required <img loading=\"lazy\" class=\" wp-image-313 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-4-300x153.jpeg\" alt=\"\" width=\"736\" height=\"375\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-4-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-4.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In next step click on <strong> Attach existing policies directly\u00a0<\/strong>and select policy as per the requirement.<\/p>\n<p>Note: By selecting a policy you are giving a set of permissions to the user ,so always grant permissions only which are required to complete a specific task.<\/p>\n<p>click on Next:Tags button on the right side of footer<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-317\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-9-300x152.jpeg\" alt=\"\" width=\"707\" height=\"358\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-9-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-9-1024x519.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-9-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-9-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-9-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-9.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In step-3 add tags if required, tags are use full to organize, track or control access to users<img loading=\"lazy\" class=\"alignnone wp-image-316\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-7-300x153.jpeg\" alt=\"\" width=\"727\" height=\"371\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-7-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-7-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-7-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-7-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-7-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-7.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Review the details and click on the <strong>Ceate user\u00a0<\/strong>button<img loading=\"lazy\" class=\"alignnone wp-image-318\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-10-300x153.jpeg\" alt=\"\" width=\"725\" height=\"370\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-10-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-10-1024x522.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-10-768x392.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-10-1536x783.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-10-1200x612.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-10.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In the last step click on the Download.csv save the file, copy Access key id and close.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-319\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-11-300x152.jpeg\" alt=\"\" width=\"722\" height=\"366\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-11-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-11-1024x519.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-11-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-11-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-11-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/iam-11.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now to configure AWS CLI installation run aws configure and enter Access Key Id and secret Access Key\u00a0 from the csv file downloaded from last step of creating IAM user, enter the\u00a0 region\u00a0 \u00a0 and output format json\/yaml\/text\/table.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-322\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-2-300x52.jpeg\" alt=\"\" width=\"692\" height=\"120\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-2-300x52.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-2-1024x176.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-2-768x132.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-2-1536x264.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-2-1200x206.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-2.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step-1:How to get details of all the instances ?<\/strong><\/p>\n<pre>aws ec2 describe-instances<\/pre>\n<p>above command gives information of all the instances available<\/p>\n<p><strong><span class=\"pre\">describe-instances<\/span><\/strong>\u00a0is a paginated operation,\u00a0by default the AWS CLI automatically makes multiple calls to return all possible results to create pagination.<\/p>\n<p>Using AWS CLI commands you can get information of a particular\u00a0 instance\u00a0 by specifying instance ID or you can get information of a set of instances belonging to a criteria by specifying filters.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step-2: aws cli command to get details of a specific instance using instance id<\/strong><\/p>\n<pre>aws ec2 describe-instances --instance-id i-0238c02f55e024cb5<\/pre>\n<p>This command gives information of a specific instance with instance &#8220;id i-0238c02f55e024cb5&#8221;<br \/>\n<img loading=\"lazy\" class=\"alignnone wp-image-323\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-3-300x132.jpeg\" alt=\"\" width=\"729\" height=\"321\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-3-300x132.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-3-1024x450.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-3-768x337.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-3-1536x674.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-3-1200x527.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/aws-3.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Let&#8217;s understand how to use filters<\/strong><\/p>\n<div>\n<p>Filter <strong>Name<\/strong> and <strong>Values<\/strong>\u00a0 can be used to return a specific list of information from a describe instance operation.\u00a0 Here <strong>Name<\/strong> is a string , <strong>Values\u00a0<\/strong>is list of strings and can be more than one for a single Name, both the Name and Value are case sensitive.<\/p>\n<p>you can use multiple filters in a single command\u00a0 they are\u00a0 joined with an AND (&amp;&amp;)operation , and the command request returns only results that match all of the specified filters.<\/p>\n<div>\n<p>\u00a0If you specify multiple values for single\u00a0 a filter, the values are joined with an OR(||) , and the request returns all results that match any of the specified values.<\/p>\n<p>Syntax: <strong><span style=\"font-family: Consolas, Monaco, monospace;\">Name=string,Values=string,string &#8230;<\/span><\/strong><\/p>\n<\/div>\n<\/div>\n<p>Below commands gives information of instances with a specific <strong>security group<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to get details of ec2 instances associated with a specific &#8220;security group name<\/strong> &#8221;<\/p>\n<p>In this command &#8220;instance.group-name&#8221; is a filter Name which should be used when you want to get information of instances with a specific security group name and here we are using &#8220;web-ssh&#8221;(security gropu name) as a Value for the filter &#8220;instance.group-name&#8221;.<\/p>\n<pre>aws ec2 describe-instances --filters \"Name=instance.group-name,Values=web-ssh\"<\/pre>\n<p>This command gives information of instances with security group name &#8220;web-ssh&#8221;<\/p>\n<h4><img loading=\"lazy\" class=\" wp-image-397 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-1-300x165.jpeg\" alt=\"\" width=\"731\" height=\"402\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-1-300x165.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-1-1024x563.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-1-768x422.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-1-1536x844.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-1-1200x659.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-1.jpeg 1880w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/h4>\n<p>&nbsp;<\/p>\n<p><strong>How to use a<\/strong><strong>rgument\u00a0 &#8211;query to get specific information of a\u00a0 ec2 instance<\/strong><\/p>\n<p>The\u00a0 another argument that you can use with describe instances command is <strong>query.<\/strong><\/p>\n<p>some times you may need only specific necessary information of instances, &#8220;&#8211;query&#8221;\u00a0 will extract specific information of instances as per the query parameters given in the command<\/p>\n<p>Lets understand how to\u00a0 use &#8220;&#8211;query&#8221;.<\/p>\n<p>please observe previous output snapshots to get an idea of output format of &#8220;describe-instances&#8221;\u00a0 it has\u00a0 &#8220;Groups&#8221;: instance groups and\u00a0 &#8220;Instances&#8221;: individual instances under the &#8220;Reservations&#8221;, argument query will extract the required information from results of &#8220;describe-instances&#8221; as per the parameters specified in the command<\/p>\n<pre>aws ec2 describe-instances --query \"Reservations[*].Instances[*].{Instance:InstanceId}\"<\/pre>\n<p>This command\u00a0 will search for the information in all(*) the Reservations and in all the Instances and parameter &#8220;InstanceId&#8221; is used to get only\u00a0 instance Id&#8217;s of all the instances.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"wp-image-398 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-2-300x119.jpeg\" alt=\"\" width=\"693\" height=\"275\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-2-300x119.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-2-1024x407.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-2-768x305.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-2-1536x610.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-2-1200x476.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-2.jpeg 1879w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to use\u00a0 both &#8211;filter and &#8211;query in a single command<\/strong><\/p>\n<p>using both &#8220;&#8211;filter&#8221; and &#8220;&#8211;query&#8221; helps you to get specific information of a instance for every particular filter you use.<\/p>\n<p>In this command a filter &#8220;instance.group-id&#8221; is used which describes instances with a specific security group-id &#8220;sg-0ae6105546164429c&#8221;,additionally parameter &#8220;query&#8221; is used to get specific information of all the instances like instance-id, Subnet-Id,<br \/>\nSecurity-group-name.<\/p>\n<pre>aws ec2 describe-instances \\\r\n--filter \"Name=instance.group-id,Values=sg-0ae6105546164429c\" \\\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId,Vpc:VpcId,SecurityGroups:SecurityGroups[0].GroupName}\"\r\n<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-401 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-3-300x112.jpeg\" alt=\"\" width=\"747\" height=\"279\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-3-300x112.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-3-1024x382.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-3-768x287.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-3-1536x573.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-3-1200x448.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-3.jpeg 1825w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Using filter &#8220;tag&#8221; to get details of instances associated with specific tags.<\/strong><\/p>\n<p>Tags can be given to every resource in aws\u00a0 every tag will have a Key and a Value for each key.<\/p>\n<\/div>\n<\/div>\n<p>Lets see some commands to get the information of instances using &#8220;key\/value&#8221; combination of a &#8220;tag&#8221;<\/p>\n<p><strong>Using both key and value of a tag<\/strong><\/p>\n<pre>aws ec2 describe-instances                                                    \r\n--filters \"Name=tag:web-servers,Values=server1\"\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,Tags:Tags}\"<\/pre>\n<p>here &#8220;web-server&#8221; is a key and &#8220;server1&#8221; is a value in a tag , this command will give &#8220;InstanceId&#8221;\u00a0 and all the tags of the instances with tag-key\u00a0 webservers and tag-value server1.<\/p>\n<p>Note: &#8220;value&#8221; for a &#8220;key&#8221; in tags and &#8220;Values&#8221; for a &#8220;Name&#8221; in filters are different.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-403 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-4-300x110.jpeg\" alt=\"\" width=\"801\" height=\"294\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-4-300x110.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-4-1024x376.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-4-768x282.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-4-1536x565.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-4-1200x441.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-4.jpeg 1779w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>You\u00a0 can use filter &#8220;tag&#8221;\u00a0 with any one of its parameters either with tag-key or tag-value<\/p>\n<p><strong>With tag-key regardless of tag value:<\/strong><\/p>\n<pre>aws ec2 describe-instances \\\r\n--filters \"Name=tag-key,Values=web-servers\" \\\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId}\"\r\n<\/pre>\n<p>This command will give a output with &#8220;InstanceId&#8221; and &#8220;subnetId&#8221; whose<br \/>\ntag-key is web-servers<br \/>\n<img loading=\"lazy\" class=\" wp-image-404 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-5-300x105.jpeg\" alt=\"\" width=\"762\" height=\"267\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-5-300x105.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-5-1024x359.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-5-768x269.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-5-1536x538.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-5-1200x421.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-5.jpeg 1658w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>With tag-value regardless of tag key:<\/strong><\/p>\n<pre>aws ec2 describe-instances \\\r\n--filters \"Name=tag-value,Values=server2\" \\\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId,Vpc:VpcId}\"\r\n<\/pre>\n<p>This command will give instance-Id,subnet-id and vpc-id of instances with tag-value<br \/>\nserver2<br \/>\n<img loading=\"lazy\" class=\" wp-image-405 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-6-300x71.jpeg\" alt=\"\" width=\"959\" height=\"227\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-6-300x71.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-6-1024x241.jpeg 1024w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to get information of instances using &#8220;instance types&#8221;<\/strong><\/p>\n<p>To get details of instances using instance type<\/p>\n<pre>aws ec2 describe-instances \\\r\n--filters \"Name=instance-type,Values=t2.nano\" \\\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId,Vpc:VpcId,SecurityGroups:SecurityGroups[0].GroupName}\"<\/pre>\n<p>above command gives instance-id, subnet-id, vpc-id, security group name of instances whose instance type is &#8220;t2.nano&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-406 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-7-300x125.jpeg\" alt=\"\" width=\"692\" height=\"288\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-7-300x125.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-7-1024x427.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-7-768x320.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-7-1536x640.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-7-1200x500.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-7.jpeg 1898w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><b>Using filter &#8220;vpc-id&#8221; to get information of instances which are in a specific vpc<\/b><\/p>\n<p>To get the information of instances using filter &#8220;vpc-id&#8221;<\/p>\n<pre>aws ec2 describe-instances \\\r\n--filters \"Name=vpc-id,Values=vpc-053a656302897f3de\" \\\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId}\"\r\n<\/pre>\n<p>This command gives instance id&#8217;s and subnet id&#8217;s of the instances launched<br \/>\nin a specific vpc with id &#8220;vpc-053a656302897f3de&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-407 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-9-300x164.jpeg\" alt=\"\" width=\"678\" height=\"371\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-9-300x164.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-9-1024x561.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-9-768x421.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-9-1536x842.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-9-1200x658.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-9.jpeg 1836w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<h4><\/h4>\n<p><strong>How to get list of instances based on &#8220;state of the instance&#8221;<\/strong><\/p>\n<p>To get list of <strong>Stopped instances<\/strong><\/p>\n<pre>aws ec2 describe-instances \r\n--filters \"Name=instance-state-name,Values=stopped\"\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,State:State.Name}\"\r\n<\/pre>\n<p>Here we are using filter &#8220;instance-state-name&#8221; and &#8220;stopped&#8221; as a value for it,<br \/>\nthis command results in all the instances with &#8220;state&#8221; stopped.<br \/>\n&#8220;InstanceId&#8221;, &#8220;State.Name&#8221; are given as parameters for argument query which gives<br \/>\nfinal output with instance-id&#8217;s of all the stopped instances<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-409\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-10-300x91.jpeg\" alt=\"\" width=\"900\" height=\"273\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-10-300x91.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-10-1024x310.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-10-768x233.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-10-1536x466.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-10-1200x364.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-10.jpeg 1897w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>To get list of<strong>\u00a0 Terminated instances<\/strong><\/p>\n<pre>aws ec2 describe-instances \\\r\n--filters \"Name=instance-state-name,Values=terminated\" \\\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,LaunchTime:LaunchTime,State:State.Name}\"\r\n<\/pre>\n<p>In the above command, filter &#8220;instance-state-name&#8221; and value &#8220;terminated&#8221; is used<br \/>\nfor it and &#8220;InstanceId&#8221;, &#8220;LaunchTime&#8221; are given as parameters to argument &#8220;&#8211;query&#8221;.<\/p>\n<p>This commands gives information all theof terminated instance launch time<br \/>\nand instance id.<br \/>\n<img loading=\"lazy\" class=\" wp-image-411 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-13-300x65.jpeg\" alt=\"\" width=\"882\" height=\"191\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-13-300x65.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-13-1024x222.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-13-768x167.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-13-1536x333.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-13-1200x260.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-13.jpeg 1895w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<h3><\/h3>\n<p><strong>How to change\u00a0 output format of aws cli commands<\/strong><\/p>\n<p>Aws command line interface supports different types of output formats, you can easily change the output format of your cli command using option &#8211;output .<\/p>\n<ul>\n<li>json<\/li>\n<li>text<\/li>\n<li>table<\/li>\n<li>yaml<\/li>\n<\/ul>\n<p>Use option &#8220;&#8211;output&#8221; to get the output in required format\u00a0 along with type of\u00a0 output format you want.<\/p>\n<p><strong>Using option &#8220;&#8211;output&#8221; to get output in yaml\u00a0<\/strong><\/p>\n<pre>aws ec2 describe-instances \r\n--filters \"Name=instance-state-name,Values=running\"\r\n--query \"Reservations[*].Instances[*].{Instance:InstanceId,LaunchTime:LaunchTime,State:State.Name,VPC:VpcId,SecurityGroups:SecurityGroups[0].GroupId}\" \r\n--output yaml\r\n<\/pre>\n<p>In this command filter &#8220;instance-state-name&#8221; for &#8220;Name&#8221; and &#8220;running&#8221; as a &#8220;Value&#8221;<br \/>\nare used and output format &#8220;yaml&#8221; are used.<\/p>\n<p>output in yaml<br \/>\n<img loading=\"lazy\" class=\"wp-image-414 aligncenter\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-15-300x88.jpeg\" alt=\"\" width=\"1081\" height=\"317\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-15-300x88.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-15-1024x300.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-15-768x225.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-15-1536x450.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-15-1200x352.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/cli-15.jpeg 1891w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Please <a href=\"https:\/\/www.checkmateq.com\/contact-us\" target=\"_blank\" rel=\"noopener noreferrer\">Contact us<\/a> if you are looking for <a href=\"https:\/\/www.checkmateq.com\/cloud\">cloud support team company<\/a> for <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">IT Staff augmentation services<\/a>, <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">Hire dedicated Developer<\/a> ,Cloud DevOps services. Our <a href=\"https:\/\/www.checkmateq.com\/virtual-cto-services\">Virtual CTO Services<\/a> \u00a0will be delighted to assist you for <a href=\"https:\/\/www.checkmateq.com\/technology-consulting\">technology architecture<\/a> consulting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are more than one ways to use amazon web services Web console, boto3 (aws sdk for python), AWS Command Line Interface (AWS CLI). AWS CLI enables users to connect and interact with amazon web services using commands from their terminal, with minimal configuration steps one can start running commands to implement functionalities equivalent to &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/aws-cli-commands\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;AWS CLI commands to get ec2 instance list&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[18,19,21,20,32],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/307"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":53,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":4618,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/307\/revisions\/4618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/364"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}