{"id":62,"date":"2022-04-11T18:43:01","date_gmt":"2022-04-11T09:43:01","guid":{"rendered":"https:\/\/blog.interstellar.co.jp\/en\/?p=62"},"modified":"2022-04-11T18:45:30","modified_gmt":"2022-04-11T09:45:30","slug":"using-the-cli-with-aws-single-sign-on","status":"publish","type":"post","link":"https:\/\/blog.interstellar.co.jp\/en\/2022\/04\/11\/using-the-cli-with-aws-single-sign-on\/","title":{"rendered":"Using the CLI with AWS Single Sign-On"},"content":{"rendered":"\n<p>Hi, I am Agata.<\/p>\n\n\n\n<p>In this article, we will discuss how to use the CLI with AWS Single Sign-On (AWS SSO).<\/p>\n\n\n\n<p>There are two ways to obtain credentials from the SSO user portal or directly from the AWS CLI.<br>Both of these methods use the CLI with SSO, but I thought they were written in separate places in the AWS documentation and were inaccessible, so I summarized them here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to retrieve credentials from the User Portal<\/h2>\n\n\n\n<p>This one is very simple. Simply configure the settings using the values displayed.<br>However, an expiration date exists for SSO authentication. (Default is 1 hour and can be extended up to 12 hours by configuration.)<br>Since authentication is required again after the expiration date, it is recommended to use the system when the use is not frequent and the duration of use is short.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setup procedure<\/h3>\n\n\n\n<ol class=\"is-style-listbox wp-block-list\"><li>Access the SSO user portal (URL such as https:\/\/my-sso-portal.awsapps.com\/start used for SSO login) with your browser.<\/li><li>Click on the AWS account icon to view the list of accounts.<\/li><li>Click on the account for which you want to obtain credentials.<\/li><li>Click Command line or programmatic access next to the role name that appears below the account name.<\/li><li>The dialog Get credentials for will appear. On this screen, select the environment you wish to use: &#8220;macOS and Linux&#8221;, &#8220;Windows&#8221;, or &#8220;PowerShell&#8221;.<\/li><li>Configure the settings in the CLI using the credentials displayed. Select one of the following Option to configure.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Option 1<\/h3>\n\n\n\n<p>It is easiest because you can just run it straight through the CLI. The list of credentials (in macOS and Linux, export \u2026 ), simply paste it into the CLI and execute it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Option 2<\/h3>\n\n\n\n<p>Setup to write authentication information in ~\/.aws\/credentials. Paste the credentials into the copied credeitials file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Option 3<\/h3>\n\n\n\n<p>Set each value to the AWS service client.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to get credentials directly from the AWS CLI<\/h2>\n\n\n\n<p>This one can be accessed directly from the CLI.<br>This one has the following advantages over the method accessed from the User Portal, so if you use the CLI frequently, this one is probably better suited for you.<br>There are automatic and manual configuration methods, but only the automatic configuration method is introduced here.<\/p>\n\n\n\n<ul class=\"is-style-listbox wp-block-list\"><li>Although there is an authentication process to open a browser during the process, most of the process can be carried out using the CLI.<\/li><li>When SSO authentication expires, you can log in again with a single command from the CLI.<\/li><li>Switching between multiple accounts is easy once you have SSO login<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Setup procedure<\/h3>\n\n\n\n<p>Execute the following command and enter the SSO start URL and SSO region.<br>The SSO start URL is the URL of the SSO user portal (such as https:\/\/my-sso-portal.awsapps.com\/start ).<br>SSO region is the region where SSO is configured. Note that this is not the region you want to access. If you make a mistake in specifying this, you will get an &#8220;Invalid grant provided&#8221; error in the step in your browser.<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-dark\"><code>$ aws configure sso\nSSO start URL &#91;None]: &#91;None]: https:\/\/my-sso-portal.awsapps.com\/start\nSSO region &#91;None]:us-east-1<\/code><\/pre>\n\n\n\n<p>When entered, the following message will appear and the browser will open automatically.<br>If the browser does not open automatically, open https:\/\/device.sso.us-east-1.amazonaws.com\/ in your browser and enter the code shown in the message.<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-dark\"><code>Attempting to automatically open the SSO authorization page in your default browser.\nIf the browser does not open or you wish to use a different device to authorize this request, open the following URL:\n\nhttps:&#47;&#47;device.sso.us-east-1.amazonaws.com\/\n\nThen enter the code:\n\nXXXX-XXXX<\/code><\/pre>\n\n\n\n<p>The authentication process proceeds in the browser, and an &#8220;Allow&#8221; button is displayed on the &#8220;Authorize request&#8221; screen.<br>If you click &#8220;Allow&#8221; and there are no problems, &#8220;Request approved&#8221; will be displayed. If the SSO region is specified incorrectly, an error message &#8220;Invalid grant provided&#8221; will appear.<\/p>\n\n\n\n<p>Once the browser has successfully completed the process, return to the CLI.<br>The CLI will display a list of accessible AWS accounts, which you can select with the arrow keys and press Enter to confirm. If there is only one account in the list, it will be automatically selected and you can proceed.<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-dark\"><code>There are 2 AWS accounts available to you.\n&gt; DeveloperAccount, developer-account-admin@example.com (123456789011) \n  ProductionAccount, production-account-admin@example.com (123456789022)<\/code><\/pre>\n\n\n\n<p>Next, select the IAM role to be used. If there is only one in the list, it will be selected automatically and you can proceed.<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-dark\"><code>Using the account ID 123456789011\nThere are 2 roles available to you.\n&gt; ReadOnly\n  FullAccess<\/code><\/pre>\n\n\n\n<p>Next, set the default client region, default output format, and profile name.<br>These are the same settings that are normally made when using the CLI.<br>Note that if the environment variable AWS_PROFILE has been set, the environment variable value is automatically used without asking for the profile name.<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-dark\"><code>CLI default client Region &#91;None]: us-west-2\nCLI default output format &#91;None]: json\nCLI profile name &#91;123456789011_ReadOnly]: my-dev-profile<\/code><\/pre>\n\n\n\n<p>Finally, the following message is displayed and the configuration is complete.<br>Let&#8217;s execute the command with &#8211;profile as shown in the message.<\/p>\n\n\n\n<pre class=\"wp-block-code is-style-dark\"><code>To use this profile, specify the profile name using --profile, as shown:\n\naws s3 ls --profile my-dev-profile<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hi, I am Agata. In this article, we will discuss how to use the CLI with AWS Single Sign-On (AWS SSO). There are two ways to obtain credentials from the SSO user portal or directly from the AWS CLI.Both of these methods use the CLI with SSO, but I thought they were written in separate places in the AWS documentation and were inaccessible, so I summarized them here. How to retrieve credentials from the User Portal This one is very simple. Simply configure the settings using the values displayed.However, an expiration date exists for SSO authentication. (Default is 1 hour and can be extended up to 12 hours by configuration.)Since&#8230;<\/p>\n","protected":false},"author":2,"featured_media":64,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[6],"tags":[7,15,14],"class_list":["post-62","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-aws","tag-cli","tag-sso"],"_links":{"self":[{"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":4,"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":68,"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/posts\/62\/revisions\/68"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/media\/64"}],"wp:attachment":[{"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.interstellar.co.jp\/en\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}