Skip to main content

IAM MFA

This command line tool helps you manage AWS CLI credentials with MFA (multi-factor authentication).

This is intended as a companion to an IAM policy that requiring MFA everywhere, including on API access. One such policy is inculded in this readme.

The tool takes a source IAM profile (which should store your permanent credentials) and an MFA code and outputs API AWS credentials to a destination IAM profile. The output credentials are valid for 24 hours and are blessed with MFA (and thus can be used on APIs that require MFA).

Instalation

It's a pip library, installed by pip install iam-mfa.

It requires version 2 of the AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html. It uses the AWS CLI to manage the configuration and credential files: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

Usage

iam-mfa <source_profile> <dest_profile> <username>

Arguments:

  • source_profile: The profile to use for calling sts get-session-token; this should contain your permanent AWS API credentials
  • dest_profile: The profile in which the temporary credentials will be saved; note that this profile will be overridden
  • username: The IAM username that owns the source_profile credentials

The MFA code is provided as an input to the script running so that it isn't stored in bash history.

AWS IAM Policy

It is recommended that you have an IAM policy on your AWS account that enforces the use of multi-factor authentication for all access.

Below is a policy that does the following:

  • Allow users to change their own password
  • Allow users to view and edit their own MFA devices (a necessary component to requiring them to have MFA)
  • Prevent users from doing most things if they logged in to the console without MFA
  • Still allow users to set up MFA if they logged in without it
  • Enforce these rules for both console and API aceess

It is best to create this as an IAM policy called "RequireMFAEverywhere", and then attach it to any user groups that have human users. You can also attach the policy to a user directly, but that's harder to manage.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:ChangePassword",
                "iam:CreateVirtualMFADevice",
                "iam:EnableMFADevice",
                "iam:ResyncMFADevice",
                "iam:DeleteVirtualMFADevice",
                "iam:DeactivateMFADevice",
                "iam:ListMFADevices",
                "iam:ListVirtualMFADevices",
                "iam:ListAccessKeys",
                "iam:GetAccessKeyLastUsed",
                "iam:GetUser"
            ],
            "Resource": [
                "arn:aws:iam::*:mfa/${aws:username}",
                "arn:aws:iam::*:user/${aws:username}"
            ]
        },
        {
            "Sid": "BlockMostAccessUnlessSignedInWithMFA",
            "Effect": "Deny",
            "NotAction": [
                "iam:CreateVirtualMFADevice",
                "iam:ListVirtualMFADevices",
                "iam:EnableMFADevice",
                "iam:ResyncMFADevice",
                "iam:DeleteVirtualMFADevice",
                "iam:DeactivateMFADevice",
                "iam:ListAccountAliases",
                "iam:ListUsers",
                "iam:ListSSHPublicKeys",
                "iam:ListAccessKeys",
                "iam:GetAccessKeyLastUsed",
                "iam:ListServiceSpecificCredentials",
                "iam:ListMFADevices",
                "iam:GetAccountSummary",
                "iam:ChangePassword",
                "iam:GetUser",
                "sts:GetSessionToken"
            ],
            "Resource": "*",
            "Condition": {
                "BoolIfExists": {
                    "aws:MultiFactorAuthPresent": "false"
                }
            }
        }
    ]
}

Release files for iam-mfa 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iam-mfa 0.1.0
File Size Uploaded
iam_mfa-0.1.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iam-mfa 0.1.0
File Interpreter ABI Platform
iam_mfa-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / iam_mfa-0.1.0.tar.gz

Download URL iam_mfa-0.1.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5da983b896007a8ab8e175a4def8108b3e89f7bc27d2bb445b3ee0f35e7acf99
BLAKE2b-256 checksum
How to use checksums
cddd09d1d8654df816efe05ff6b442923909448a76615c1562700b9b30489a38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / iam_mfa-0.1.0-py3-none-any.whl

Download URL iam_mfa-0.1.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
245b70cf4ba27603aec6069d4523e170188fe60c52e499a91a9282d0e1744687
BLAKE2b-256 checksum
How to use checksums
186cbdc111165a3248c049d4121a580fd9159bbce5ed9d2332a6c83eee023177
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page