<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Frankovskyi Bogdan</title>
    <description>The latest articles on DEV Community by Frankovskyi Bogdan (@bfrankovskyi).</description>
    <link>https://dev.to/bfrankovskyi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1106614%2Fbc884310-9548-4089-b373-f101d0787c5d.png</url>
      <title>DEV Community: Frankovskyi Bogdan</title>
      <link>https://dev.to/bfrankovskyi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bfrankovskyi"/>
    <language>en</language>
    <item>
      <title>Mastering Multiple AWS Accounts with AWS CLI: Navigating Role Delegation, MFA, and Automated Login Scripts</title>
      <dc:creator>Frankovskyi Bogdan</dc:creator>
      <pubDate>Wed, 15 Nov 2023 09:03:43 +0000</pubDate>
      <link>https://dev.to/bfrankovskyi/mastering-multiple-aws-accounts-with-aws-cli-navigating-role-delegation-mfa-and-automated-login-scripts-5bin</link>
      <guid>https://dev.to/bfrankovskyi/mastering-multiple-aws-accounts-with-aws-cli-navigating-role-delegation-mfa-and-automated-login-scripts-5bin</guid>
      <description>&lt;p&gt;In my role as a platform engineer, I often work with multiple AWS accounts. This can get tricky, especially when setting up Terraform or writing scripts for different account resources. I've learned some helpful tips and tricks along the way, which may be helpful to you. &lt;/p&gt;

&lt;p&gt;For multiple accounts access &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html"&gt;role delegation&lt;/a&gt; is usually used. This is especially helpful, because it is easy to manage, it adds required granularity of access to the different resources and allows user to have only one pair of secrets on computer. It is also &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html"&gt;easy to configure for AWS CLI&lt;/a&gt; and it is supported by Terraform. &lt;/p&gt;

&lt;p&gt;It is a bit more difficult to configure when &lt;a href="https://aws.amazon.com/iam/features/mfa"&gt;Multi-Factor Authentication (MFA) is enabled&lt;/a&gt;  for the additional security. MFA is great for security - it makes sure this is you who are trying to use credentials, not anyone else uses stolen creds. On practice, it means you need to enter some One-Time Password (OTP) code generated for you by some program (like &lt;a href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2&amp;amp;pcampaignid=web_share"&gt;Google Authenicator&lt;/a&gt;) or by separate hardware (like &lt;a href="https://www.yubico.com"&gt;Yubikey&lt;/a&gt; or &lt;a href="https://www.protectimus.com"&gt;Protectimus&lt;/a&gt;). There are other solutions on a market, but I have experience with these only. &lt;/p&gt;

&lt;p&gt;For OTP I usually use Google Authenicator but using it with some short-lived session is very annoying - you have to type code from phone every time. For these cases I use Yubikey to generate OTP codes, or I use &lt;a href="https://support.1password.com/one-time-passwords/"&gt;1Password&lt;/a&gt; when I can't use Yubikey. 1Password is a bit less secure option for this case - because it can be used if someone get access to your computer and have your password, while with GA or hardware token attacker must have access to them too. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/blogs/security/use-yubikey-security-key-sign-into-aws-management-console/"&gt;How to configure Yubikey as AWS MFA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://support.1password.com/one-time-passwords/"&gt;How to configure 1Password for AWS MFA&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have it configured, let's start from configuring AWS CLI and login script. Login script will generate temporary credentials which may be used to access all other account. The flow of it is simple: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieve new set of temporary credentials using &lt;a href="https://docs.aws.amazon.com/cli/latest/reference/sts/get-session-token.html"&gt;sts get-session-token&lt;/a&gt; AWS CLI command using credentials from account where user is created&lt;/li&gt;
&lt;li&gt;Set temporary credentials to &lt;code&gt;~/.aws/credentials&lt;/code&gt; profile&lt;/li&gt;
&lt;li&gt;use this profile as &lt;code&gt;source_profile&lt;/code&gt; with multiple AWS profiles&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RXdqu4Va--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7aetuhm997674uyfxooh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RXdqu4Va--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7aetuhm997674uyfxooh.png" alt="Image description" width="800" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all, you need to add profile where your user was created, and credentials to it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;~/.aws/config&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[profile account-where-user-congured]&lt;/span&gt;
&lt;span class="py"&gt;region&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;us-east-1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;~/.aws/credentials&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[account-where-user-configured]&lt;/span&gt;
&lt;span class="py"&gt;aws_access_key_id&lt;/span&gt;        &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;*******************&lt;/span&gt;
&lt;span class="py"&gt;aws_secret_access_key&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;*********************&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add empty profile where generated credentials will be stored.&lt;br&gt;
&lt;code&gt;~/.aws/credentials&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[account-where-user-configured]&lt;/span&gt;
&lt;span class="py"&gt;aws_access_key_id&lt;/span&gt;        &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;*******************&lt;/span&gt;
&lt;span class="py"&gt;aws_secret_access_key&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;*********************&lt;/span&gt;

&lt;span class="nn"&gt;[root-mfa]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and, of course, add other accounts where user have access with assumed role:&lt;br&gt;
&lt;code&gt;~/.aws/config&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[profile account-where-user-configured]&lt;/span&gt;
&lt;span class="py"&gt;region&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;us-east-1&lt;/span&gt;
&lt;span class="py"&gt;output&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;json&lt;/span&gt;

&lt;span class="nn"&gt;[profile some-account-you-can-assume-role-in]&lt;/span&gt;
&lt;span class="py"&gt;region&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;us-east-1&lt;/span&gt;
&lt;span class="py"&gt;source_profile&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;root-mfa&lt;/span&gt;
&lt;span class="py"&gt;role_arn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;arn:aws:iam::0123456789:role/my-awesome-role&lt;/span&gt;

&lt;span class="nn"&gt;[profile some-other-account-you-can-assume-role-in]&lt;/span&gt;
&lt;span class="py"&gt;region&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;us-east-1&lt;/span&gt;
&lt;span class="py"&gt;source_profile&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;root-mfa&lt;/span&gt;
&lt;span class="py"&gt;role_arn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;arn:aws:iam::0123456789:role/my-awesome-role&lt;/span&gt;
&lt;span class="err"&gt;....&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's make script to retrieve temporary credentials (based on script from here &lt;a href="https://gist.github.com/ogavrisevs/2debdcb96d3002a9cbf2"&gt;Script to generate AWS STS token · GitHub&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;&lt;code&gt;awslogin.sh&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# Sample for getting temp session token from AWS STS&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# aws --profile youriamuser sts get-session-token --duration 3600 \&lt;/span&gt;
&lt;span class="c"&gt;# --serial-number arn:aws:iam::012345678901:mfa/user --token-code 012345&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# Based on : https://github.com/EvidentSecurity/MFAonCLI/blob/master/aws-temp-token.sh&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;

&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt;
&lt;span class="nv"&gt;AWS_CLI&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;which aws&lt;span class="sb"&gt;`&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt; &lt;span class="nt"&gt;-ne&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"AWS CLI is not installed; exiting"&lt;/span&gt;
  &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Using AWS CLI found at &lt;/span&gt;&lt;span class="nv"&gt;$AWS_CLI&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;fi

&lt;/span&gt;&lt;span class="nv"&gt;AWS_USER_PROFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;account-where-user-configured
&lt;span class="nv"&gt;AWS_2AUTH_PROFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;root-mfa
&lt;span class="nv"&gt;ARN_OF_MFA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;arn:aws:iam::account-where-user-configured-id:mfa/yubikey-mfa-arn-name
&lt;span class="nv"&gt;DURATION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;129600

&lt;span class="nb"&gt;read &lt;/span&gt;AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="si"&gt;$(&lt;/span&gt; aws &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$AWS_USER_PROFILE&lt;/span&gt; sts get-session-token &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--duration&lt;/span&gt; &lt;span class="nv"&gt;$DURATION&lt;/span&gt;  &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--serial-number&lt;/span&gt; &lt;span class="nv"&gt;$ARN_OF_MFA&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--token-code&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;ykman oath code &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nv"&gt;$ARN_OF_MFA&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--output&lt;/span&gt; text  | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{ print $2, $4, $5 }'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="sb"&gt;`&lt;/span&gt;aws &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$AWS_2AUTH_PROFILE&lt;/span&gt; configure &lt;span class="nb"&gt;set &lt;/span&gt;aws_access_key_id &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="sb"&gt;`&lt;/span&gt;aws &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$AWS_2AUTH_PROFILE&lt;/span&gt; configure &lt;span class="nb"&gt;set &lt;/span&gt;aws_secret_access_key &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="sb"&gt;`&lt;/span&gt;aws &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$AWS_2AUTH_PROFILE&lt;/span&gt; configure &lt;span class="nb"&gt;set &lt;/span&gt;aws_session_token &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_SESSION_TOKEN&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note &lt;code&gt;$(ykman oath code -s $ARN_OF_MFA)&lt;/code&gt; line - this is where &lt;a href="https://developers.yubico.com/yubikey-manager/"&gt;ykman&lt;/a&gt; will ask you to touch Yubikey. That pretty much it. Now you can login just once and use different AWS profiles without need to re-login.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;⋊&amp;gt;./awslogin.sh 
Using AWS CLI found at /home/linuxbrew/.linuxbrew/bin/aws
Touch your YubiKey...
⋊&amp;gt;aws s3 &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;--profile&lt;/span&gt; some-account-you-can-assume-role-in
&lt;span class="c"&gt;# some buckets will be shown&lt;/span&gt;
&lt;span class="c"&gt;# you can simultaneously use it for all other accounts you can assume role in&lt;/span&gt;
&lt;span class="c"&gt;# without need to switch to it or use env variables&lt;/span&gt;
⋊&amp;gt;aws s3 &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;--profile&lt;/span&gt; some-other-account-you-can-assume-role-in
&lt;span class="c"&gt;# some buckets will be shown&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configuration with 1Password OTP generation is similar — but instead of &lt;code&gt;ykman&lt;/code&gt; &lt;a href="https://1password.com/downloads/command-line/"&gt;op&lt;/a&gt; should be used to retrieve a OTP code. If we are going to use 1Password for auth anyways, it is reasonable to get rid of the local secret for &lt;code&gt;account-where-user-configured&lt;/code&gt; stored in plain text in &lt;code&gt;~/.aws/credentials&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Set up credentials in 1Password and remove &lt;code&gt;[profile account-where-user-configured]&lt;/code&gt; from &lt;code&gt;~/.aws/credentials&lt;/code&gt; and &lt;code&gt;~/.aws/config&lt;/code&gt;.&lt;br&gt;
Credentials for accounts where user is set up will be taken from 1Password and used in script instead of ones in &lt;code&gt;~/.aws/credentials&lt;/code&gt;. We also will use OTP generated by 1Password&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# Sample for getting temp session token from AWS STS&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# aws --profile youriamuser sts get-session-token --duration 3600 \&lt;/span&gt;
&lt;span class="c"&gt;# --serial-number arn:aws:iam::012345678901:mfa/user --token-code 012345&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# Based on : https://github.com/EvidentSecurity/MFAonCLI/blob/master/aws-temp-token.sh&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;

&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt;
&lt;span class="nv"&gt;AWS_CLI&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;which aws&lt;span class="sb"&gt;`&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt; &lt;span class="nt"&gt;-ne&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"AWS CLI is not installed; exiting"&lt;/span&gt;
  &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Using AWS CLI found at &lt;/span&gt;&lt;span class="nv"&gt;$AWS_CLI&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;fi

&lt;/span&gt;&lt;span class="nv"&gt;AWS_USER_PROFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;account-where-user-configured
&lt;span class="nv"&gt;AWS_2AUTH_PROFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;root-mfa
&lt;span class="nv"&gt;ARN_OF_MFA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;arn:aws:iam::account-where-user-configured-id:mfa/yubikey-mfa-arn-name
&lt;span class="nv"&gt;DURATION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;129600

&lt;span class="nb"&gt;read &lt;/span&gt;AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;env &lt;/span&gt;&lt;span class="nv"&gt;AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;op &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="s2"&gt;"op://Private/&amp;lt;your-account-secret&amp;gt;/access key id"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nv"&gt;AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;op &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="s2"&gt;"op://Private/&amp;lt;your-account-secret&amp;gt;/secret access key"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  aws sts get-session-token &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--duration&lt;/span&gt; &lt;span class="nv"&gt;$DURATION&lt;/span&gt;  &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--serial-number&lt;/span&gt; &lt;span class="nv"&gt;$ARN_OF_MFA&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--token-code&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nt"&gt;--token-code&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;op &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="s2"&gt;"op://Private/&amp;lt;your-account-secret&amp;gt;/one-time password"&lt;/span&gt;&lt;span class="si"&gt;))&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--output&lt;/span&gt; text  | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{ print $2, $4, $5 }'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="sb"&gt;`&lt;/span&gt;aws &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$AWS_2AUTH_PROFILE&lt;/span&gt; configure &lt;span class="nb"&gt;set &lt;/span&gt;aws_access_key_id &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="sb"&gt;`&lt;/span&gt;aws &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$AWS_2AUTH_PROFILE&lt;/span&gt; configure &lt;span class="nb"&gt;set &lt;/span&gt;aws_secret_access_key &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="sb"&gt;`&lt;/span&gt;aws &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$AWS_2AUTH_PROFILE&lt;/span&gt; configure &lt;span class="nb"&gt;set &lt;/span&gt;aws_session_token &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$AWS_SESSION_TOKEN&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TIP: You can use it with Terraform/Terragrun by setting environment variable temporary, just for the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;env &lt;/span&gt;&lt;span class="nv"&gt;AWS_PROFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-profile-name terraform init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TIP: You can export generated credentials to environment variable (for cases when aws profiles is not supported) by the &lt;a href="(https://stackoverflow.com/questions/40852223/is-there-a-way-to-export-an-aws-cli-profile-to-environment-variables)"&gt;following command&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;aws configure export-credentials &lt;span class="nt"&gt;--profile&lt;/span&gt; your-profile-name &lt;span class="nt"&gt;--format&lt;/span&gt; &lt;span class="nb"&gt;env&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TIP: you also can pass credentials to docker container on run without storing them in separate file or export to your environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--env-file&lt;/span&gt; &amp;lt;&lt;span class="o"&gt;(&lt;/span&gt;aws configure export-credentials &lt;span class="nt"&gt;--profile&lt;/span&gt; your-profile-name &lt;span class="nt"&gt;--format&lt;/span&gt; env-no-export&lt;span class="o"&gt;)&lt;/span&gt; ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>aws</category>
      <category>security</category>
      <category>mfa</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
