Avoid the 60 minutes timeout when using the AWS CLI with IAM roles

Michael Wittig – 27 Aug 2019

You can configure the AWS CLI to assume an IAM role for you in combination with MFA. If you are a power user of the CLI, you will realize that you have to enter your MFA token every 60 minutes, which is annoying.

Avoid the 60 minute timeout when using the AWS CLI with IAM roles

You will learn how to fix that in the following.

AWS account setup

Let’s assume we have three AWS accounts.

Account id Alias Description
000000000000 iam Only IAM users are created in this account
111111111111 dev Development workloads
222222222222 prod Production workloads

Besides that:

  1. In the iam account, an IAM user named michael is created. MFA is enabled, and an access key is generated.
  2. In the dev and prod accounts, the following IAM role is created (CloudFormation template):
AWSTemplateFormatVersion: '2010-09-09'
Type: 'AWS::IAM::Role'
Version: '2012-10-17'
- Effect: Allow
AWS: 'arn:aws:iam::000000000000:root' # replace this with your iam account id
Action: 'sts:AssumeRole'
'aws:MultiFactorAuthPresent': true
- 'arn:aws:iam::aws:policy/AdministratorAccess'
MaxSessionDuration: 43200 # 12 hours in seconds
RoleName: Admin

Ensure that you set the MaxSessionDuration property! The default is 60 minutes.

Configuring the AWS CLI

The AWS CLI stores the configuration in ~/.aws/credentials (or %UserProfile%\.aws\credentials if you are using Windows).

First of all, configure the access key from the michael IAM user using the aws_access_key_id and aws_secret_access_key configuration values. The value between the square brackets is called the profile name.

Andreas and Michael Wittig

Hej, Andreas & Michael here!

We launched the cloudonaut blog in 2015. Since then, we have published 325 articles: small tips and tricks, best practices, and service reviews. We enjoy writing about all things AWS a lot.

Do you like our blog posts and podcast episodes? Have you learned something new? Consider supporting us create in-depth and independent AWS content. Please help us with a monthly or one-time payment through GitHub Sponsors.

Start supporting us today!
aws_access_key_id = AKIA****************
aws_secret_access_key = ****************************************

After that, configure the IAM roles you want to assume. The following configuration values are used:

Configuration value Description
role_arn ARN of the role you want to assume
source_profile Reference the profile of the IAM user
mfa_serial ARN of the virtual MFA device or the serial number for a hardware device
duration_seconds The expiry of the credentials returned by the assume role call

Ensure that you set the duration_seconds property! The default is 60 minutes.

Add the following profiles to the credentials file.

role_arn = arn:aws:iam::111111111111:role/Admin
source_profile = iam
mfa_serial = arn:aws:iam::000000000000:mfa/michael
duration_seconds = 43200

role_arn = arn:aws:iam::222222222222:role/Admin
source_profile = iam
mfa_serial = arn:aws:iam::000000000000:mfa/michael
duration_seconds = 43200

Using the profiles

The --profile parameter lets you specify the profile you want to use when working with the CLI.

aws --profile dev s3 ls
aws --profile prod s3 ls

The AWS CLI will ask you for your MFA token the first time you make a call.

You can also set the AWS_PROFILE environment variable to avoid typing --profile ... all the time.

export AWS_PROFILE=dev
aws s3 ls


To avoid frequent re-enter of the MFA token when using the AWS CLI, you have to adjust the MaxSessionDuration of the IAM role and the duration_seconds configuration value of the AWS CLI.

Michael Wittig

Michael Wittig

I'm an independent consultant, technical writer, and programming founder. All these activities have to do with AWS. I'm writing this blog and all other projects together with my brother Andreas.

In 2009, we joined the same company as software developers. Three years later, we were looking for a way to deploy our software—an online banking platform—in an agile way. We got excited about the possibilities in the cloud and the DevOps movement. It’s no wonder we ended up migrating the whole infrastructure of Tullius Walden Bank to AWS. This was a first in the finance industry, at least in Germany! Since 2015, we have accelerated the cloud journeys of startups, mid-sized companies, and enterprises. We have penned books like Amazon Web Services in Action and Rapid Docker on AWS, we regularly update our blog, and we are contributing to the Open Source community. Besides running a 2-headed consultancy, we are entrepreneurs building Software-as-a-Service products.

We are available for projects.

Feedback? Questions? Drop me a line: Email, Twitter, LinkedIn.

Briefcase icon
Hire me