{"id":517,"date":"2022-01-05T16:56:13","date_gmt":"2022-01-05T15:56:13","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/2022\/01\/05\/aws-cli-tips-and-tricks\/"},"modified":"2022-04-06T08:26:29","modified_gmt":"2022-04-06T06:26:29","slug":"aws-cli-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/","title":{"rendered":"AWS CLI tips and tricks"},"content":{"rendered":"<p>When working with AWS, I would not advice to run all operations through the AWS Console. And it&#8217;s not always possible or easy to do it only with pipelines and Infrastructure as Code. Sometimes, we just need to perform the same operations on many instances or we already have an automation based on a script. In those situations, AWS CLI (the command line) can be very handy. I had the chance recently to attend a <a href=\"https:\/\/www.dbi-services.com\/blog\/aws-reinvent-2021-2nd-day\/\">workshop fully dedicated to AWS CLI<\/a>, in this post I&#8217;ll give some tricks I use.<\/p>\n<h3>Aliases<\/h3>\n<p>There are some commands you may run very often. When working with multiple AWS accounts and\/or temporary credentials, and you should, one of my favorite command is:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">aws sts get-caller-identity<\/pre>\n<p>The output looks like below:<\/p>\n<pre class=\"brush: text; gutter: false; first-line: 1\">{\n    \"UserId\": \"AROA*****************:&lt;User Id&gt;\",\n    \"Account\": \"&lt;AWS Account Id&gt;\",\n    \"Arn\": \"arn:aws:sts::&lt;AWS Account Id&gt;:assumed-role\/&lt;IAM Role Name&gt;\/&lt;User Id&gt;\"\n}<\/pre>\n<p>It&#8217;s an easy way to check if the credentials are still valid and more important on which account the next commands will run. The command is a bit long, let&#8217;s create an alias shorter and easier to remember. The configuration is very easy, the aliases are declared in a file without your user profile.<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">mkdir -p ~\/.aws\/cli\necho '[toplevel]' &gt; ~\/.aws\/cli\/alias\necho 'whoami = sts get-caller-identity' &gt;&gt; ~\/.aws\/cli\/alias<\/pre>\n<p>Then just run the following to get the same output as above:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">aws whoami<\/pre>\n<p>There are a lot of examples of aliases in the project <a href=\"https:\/\/github.com\/awslabs\/awscli-aliases\">awscli-aliases<\/a> at Github.<\/p>\n<h3>Filter with &#8211;query<\/h3>\n<p>I know there are a lots of people already using &#8220;jq&#8221; for processing JSON outputs. But AWS CLI provides a client-side filtering feature using the &#8211;query option. It&#8217;s uses JMESPath syntax which is similar but different from jq. This one is really my favorite and I use it quite a lot, the big advantage is that it works anywhere AWS CLI without the external dependency on jq and on Windows as well. Let&#8217;s start with an example, where we just want to reduce the amount of information displayed about instances.<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">aws ec2 describe-instances --region eu-central-1 --query 'Reservations[*].Instances[].[Tags[?Key==`Name`].Value|[0],InstanceId,State.Name,PrivateIpAddress,Platform]' --output table\n------------------------------------------------------------------------------------------------\n|                                       DescribeInstances                                      |\n+-----------------------------------+----------------------+----------+---------------+--------+\n|  bastion                          |  i-046bfzzzzzzzzzzzz |  running |  169.254.0.10 |  None  |\n|  dbiinsite-management             |  i-0b544yyyyyyyyyyyy |  running |  169.254.0.11 |  None  |\n|  devops-docker-1                  |  i-0fe6ammmmmmmmmmmm |  stopped |  169.254.0.12 |  None  |\n|  devops-docker-2                  |  i-03bannnnnnnnnnnnn |  stopped |  169.254.0.13 |  None  |\n+-----------------------------------+----------------------+----------+---------------+--------+<\/pre>\n<p>In the previous example, we browse 2 lists (Reservations and Instances), then display the values for some identifiers (i.e. InstanceId). But you may have noticed a more advanced syntax to display the name. Indeed, the name is part of another list (Tags) where we want only one value. So, we introduce a filter &#8220;?Key==`Name`&#8221; to get only the entry we are interested in. Finally to avoid having lists of lists, we flatten to get only the value in our table with &#8220;|[0]&#8221;.<\/p>\n<p>Now, you can imagine more complex combinations, combining server-side filtering with the option &#8220;&#8211;filters&#8221; and a client-side filtering to get only the resource id or an additional filter that may not be available on server-side. For example, displaying only the running Linux instances (.i.e. Platform is empty for Linux instances but contains windows for Windows instances).<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">aws ec2 describe-instances --filters '[{\"Name\":\"instance-state-name\",\"Values\":[\"running\"]}]' --query 'Reservations[*].Instances[?Platform!=`windows`].[InstanceId]' --output text\ni-046bfzzzzzzzzzzzz\ni-0b544yyyyyyyyyyyy<\/pre>\n<p>As the filter can very quickly become big and difficult to read, the aliases can be very useful to store the complex filters and run simple commands.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with AWS, I would not advice to run all operations through the AWS Console. And it&#8217;s not always possible or easy to do it only with pipelines and Infrastructure as Code. Sometimes, we just need to perform the same operations on many instances or we already have an automation based on a script. [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1865,955],"tags":[133,134,135],"type_dbi":[],"class_list":["post-517","post","type-post","status-publish","format-standard","hentry","category-aws","category-cloud","tag-aws","tag-aws-cli","tag-cloud"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>AWS CLI tips and tricks - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AWS CLI tips and tricks\" \/>\n<meta property=\"og:description\" content=\"When working with AWS, I would not advice to run all operations through the AWS Console. And it&#8217;s not always possible or easy to do it only with pipelines and Infrastructure as Code. Sometimes, we just need to perform the same operations on many instances or we already have an automation based on a script. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-05T15:56:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-06T06:26:29+00:00\" \/>\n<meta name=\"author\" content=\"Nicolas Jardot\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nicolas Jardot\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/\"},\"author\":{\"name\":\"Nicolas Jardot\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/0aa30f52275a132e9cc2c387708f84ed\"},\"headline\":\"AWS CLI tips and tricks\",\"datePublished\":\"2022-01-05T15:56:13+00:00\",\"dateModified\":\"2022-04-06T06:26:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/\"},\"wordCount\":474,\"commentCount\":0,\"keywords\":[\"AWS\",\"AWS CLI\",\"Cloud\"],\"articleSection\":[\"AWS\",\"Cloud\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/\",\"name\":\"AWS CLI tips and tricks - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2022-01-05T15:56:13+00:00\",\"dateModified\":\"2022-04-06T06:26:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/0aa30f52275a132e9cc2c387708f84ed\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AWS CLI tips and tricks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/0aa30f52275a132e9cc2c387708f84ed\",\"name\":\"Nicolas Jardot\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/112c023c253239221e61a24d59db49d57b354e10da6e7c074cfff50e1b5a6dd8?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/112c023c253239221e61a24d59db49d57b354e10da6e7c074cfff50e1b5a6dd8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/112c023c253239221e61a24d59db49d57b354e10da6e7c074cfff50e1b5a6dd8?s=96&d=mm&r=g\",\"caption\":\"Nicolas Jardot\"},\"description\":\"Nicolas Jardot is a senior consultant with more than nine years of experience in Oracle database infrastructure management and optimization. In addition to standard operations, he is specialized in the performance optimization and tuning of Oracle databases. He also has a strong knowledge of SQL language and has developed several PL\/SQL packages to simplify the administration of database applications. He also maintains the DMK_SQL package of dbi services\u2019 Database Management Kit. Nicolas is Oracle Certified Professional 11g\/12c and Oracle Certified Expert Performance Management and Tuning 12c and holds speeches around Oracle technologies in various conferences including Oracle OpenWorld and UKOUG. Over time, Nicolas has become increasingly interested in Cloud and automation technologies. He has been working for over two years on building and maintaining applications in AWS and operates a CI\/CD pipeline to provision and configure the infrastructure. Nicolas is also certified AWS Solution Architect Professional. Prior to dbi services, Nicolas Jardot was C++ developer on an application virtualization solution, which gave him a solid experience in virtualization and centralization of applications. Nicolas Jardot holds an Engineer\u2019s Degree in Computer Science from the University of Technology of Belfort-Montb\u00e9liard (F). His branch-related experience covers Pharma, Public Sector, Health, Real Estate, Automotive, etc.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/nicolas-jardot-762b9535\/\"],\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/nicolas-jardot\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"AWS CLI tips and tricks - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/","og_locale":"en_US","og_type":"article","og_title":"AWS CLI tips and tricks","og_description":"When working with AWS, I would not advice to run all operations through the AWS Console. And it&#8217;s not always possible or easy to do it only with pipelines and Infrastructure as Code. Sometimes, we just need to perform the same operations on many instances or we already have an automation based on a script. [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/","og_site_name":"dbi Blog","article_published_time":"2022-01-05T15:56:13+00:00","article_modified_time":"2022-04-06T06:26:29+00:00","author":"Nicolas Jardot","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nicolas Jardot","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/"},"author":{"name":"Nicolas Jardot","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/0aa30f52275a132e9cc2c387708f84ed"},"headline":"AWS CLI tips and tricks","datePublished":"2022-01-05T15:56:13+00:00","dateModified":"2022-04-06T06:26:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/"},"wordCount":474,"commentCount":0,"keywords":["AWS","AWS CLI","Cloud"],"articleSection":["AWS","Cloud"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/","url":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/","name":"AWS CLI tips and tricks - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2022-01-05T15:56:13+00:00","dateModified":"2022-04-06T06:26:29+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/0aa30f52275a132e9cc2c387708f84ed"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/aws-cli-tips-and-tricks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"AWS CLI tips and tricks"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/0aa30f52275a132e9cc2c387708f84ed","name":"Nicolas Jardot","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/112c023c253239221e61a24d59db49d57b354e10da6e7c074cfff50e1b5a6dd8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/112c023c253239221e61a24d59db49d57b354e10da6e7c074cfff50e1b5a6dd8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/112c023c253239221e61a24d59db49d57b354e10da6e7c074cfff50e1b5a6dd8?s=96&d=mm&r=g","caption":"Nicolas Jardot"},"description":"Nicolas Jardot is a senior consultant with more than nine years of experience in Oracle database infrastructure management and optimization. In addition to standard operations, he is specialized in the performance optimization and tuning of Oracle databases. He also has a strong knowledge of SQL language and has developed several PL\/SQL packages to simplify the administration of database applications. He also maintains the DMK_SQL package of dbi services\u2019 Database Management Kit. Nicolas is Oracle Certified Professional 11g\/12c and Oracle Certified Expert Performance Management and Tuning 12c and holds speeches around Oracle technologies in various conferences including Oracle OpenWorld and UKOUG. Over time, Nicolas has become increasingly interested in Cloud and automation technologies. He has been working for over two years on building and maintaining applications in AWS and operates a CI\/CD pipeline to provision and configure the infrastructure. Nicolas is also certified AWS Solution Architect Professional. Prior to dbi services, Nicolas Jardot was C++ developer on an application virtualization solution, which gave him a solid experience in virtualization and centralization of applications. Nicolas Jardot holds an Engineer\u2019s Degree in Computer Science from the University of Technology of Belfort-Montb\u00e9liard (F). His branch-related experience covers Pharma, Public Sector, Health, Real Estate, Automotive, etc.","sameAs":["https:\/\/www.linkedin.com\/in\/nicolas-jardot-762b9535\/"],"url":"https:\/\/www.dbi-services.com\/blog\/author\/nicolas-jardot\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=517"}],"version-history":[{"count":4,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":708,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/517\/revisions\/708"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=517"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}