

# AWS CLI または AWS API で MFA デバイスを割り当てる
<a name="id_credentials_mfa_enable_cliapi"></a>

IAM ユーザーの仮想 MFA デバイスを有効にするには、AWS CLI コマンドまたは AWS API オペレーションを使用します。AWS CLI、AWS API、Tools for Windows PowerShell、またはその他のコマンドラインツールを使用して AWS アカウントのルートユーザー の MFA デバイスを有効にすることはできません。ただし、AWS マネジメントコンソール を使用して、ルートユーザーの MFA デバイスを有効化することができます。

AWS マネジメントコンソール から MFA デバイスを有効にすると、コンソールがお客様に代わって複数のステップを実行します。代わりに AWS CLI、Tools for Windows PowerShell、または AWS API を使用して仮想デバイスを作成する場合は、それらのステップを適切な順序で自分で実行する必要があります。たとえば、仮想 MFA デバイスを作成するには、IAM オブジェクトを作成し、文字列または QR コードグラフィックとしてコードを抽出する必要があります。次に、デバイスを IAM ユーザーと同期させて関連付ける必要があります。詳細については、[New-IAMVirtualMFADevice](https://docs.aws.amazon.com/powershell/latest/reference/Index.html?page=New-IAMVirtualMFADevice.html&tocid=New-IAMVirtualMFADevice) の **[Examples]** (例) セクションを参照してください。物理デバイスの場合は作成ステップを飛ばし、デバイスを同期してユーザーに関連付ける手順へ直接進んでください。

IAM リソース (仮想 MFA デバイスを含む) にタグをアタッチして、タグへのアクセスを特定、整理、制御することができます。仮想 MFA デバイスにタグを付けることができるのは、AWS CLI または AWS API を使用する場合のみです。

SDK または CLI を使用する IAM ユーザーは、[https://docs.aws.amazon.com/IAM/latest/APIReference/API_EnableMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_EnableMFADevice.html) を呼び出して追加の MFA デバイスを有効にするか、[https://docs.aws.amazon.com/IAM/latest/APIReference/API_DeactivateMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_DeactivateMFADevice.html) を呼び出して既存の MFA デバイスを無効にできます。これを正常に行うには、まず [https://docs.aws.amazon.com/STS/latest/APIReference/API_GetSessionToken.html](https://docs.aws.amazon.com/STS/latest/APIReference/API_GetSessionToken.html) を呼び出し、既存の MFA デバイスで MFA コードを送信する必要があります。この呼び出しで、一時的なセキュリティ認証情報を返されます。この認証情報を使用して、MFA 認証を必要とする API オペレーションに署名できます。リクエストとレスポンスの例については、「[`GetSessionToken`—信頼されていない環境にあるユーザー向けの一時的認証情報](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html#api_getsessiontoken)」を参照してください。

**IAM で仮想デバイスのエンティティを作成し、仮想 MFA デバイスを表すには**  
これらのコマンドは、次のコマンドの多くでシリアルナンバーの代わりに使用されるデバイスの ARN を提供します。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/create-virtual-mfa-device.html](https://docs.aws.amazon.com/cli/latest/reference/iam/create-virtual-mfa-device.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateVirtualMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateVirtualMFADevice.html) 

**AWS を使用するように MFA デバイスを有効にするには**  
これらのコマンドでは、デバイスを AWS と同期させて、ユーザーと関連付けます。デバイスが仮想デバイスの場合、仮想デバイスの ARN をシリアルナンバーとして使用します。

**重要**  
認証コードを生成した後すぐに、リクエストを送信します。コードを生成した後にリクエストを送信するまで時間がかかりすぎる場合、MFA デバイスはユーザーとは正常に関連付けられますが、その MFA デバイスは同期されなくなります。これは、タイムベースドワンタイムパスワード (TOTP) の有効期間が短いために起こります。この場合は、次のコマンドを使用してデバイスを再同期できます。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/enable-mfa-device.html](https://docs.aws.amazon.com/cli/latest/reference/iam/enable-mfa-device.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_EnableMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_EnableMFADevice.html) 

**デバイスを無効にするには**  
以下のコマンドでは、デバイスとユーザーの関連付けを解除し、デバイスを無効化します。デバイスが仮想デバイスの場合、仮想デバイスの ARN をシリアルナンバーとして使用します。別途、仮想デバイスのエンティティも削除する必要があります。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/deactivate-mfa-device.html](https://docs.aws.amazon.com/cli/latest/reference/iam/deactivate-mfa-device.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_DeactivateMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_DeactivateMFADevice.html)

**仮想 MFA デバイスエンティティを一覧表示するには**  
以下のコマンドでは、仮想 MFA デバイスのエンティティを一覧表示します。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/list-virtual-mfa-devices.html](https://docs.aws.amazon.com/cli/latest/reference/iam/list-virtual-mfa-devices.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListVirtualMFADevices.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListVirtualMFADevices.html) 

**仮想 MFA デバイスにタグを付けるには**  
仮想 MFA デバイスにタグを付けるには、次のコマンドを使用します。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/tag-mfa-device.html](https://docs.aws.amazon.com/cli/latest/reference/iam/tag-mfa-device.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_TagMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_TagMFADevice.html) 

**仮想 MFA デバイスのタグを一覧表示するには**  
仮想 MFA デバイスにアタッチされたタグを一覧表示するには、次のコマンドを使用します。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/list-mfa-device-tags.html](https://docs.aws.amazon.com/cli/latest/reference/iam/list-mfa-device-tags.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListMFADeviceTags.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListMFADeviceTags.html) 

**仮想 MFA デバイスのタグを解除するには**  
仮想 MFA デバイスにアタッチされたタグを削除するには、次のコマンドを使用します。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/untag-mfa-device.html](https://docs.aws.amazon.com/cli/latest/reference/iam/untag-mfa-device.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_UntagMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_UntagMFADevice.html) 

**MFA デバイスを再同期するには**  
AWS が受け入れられないコードをデバイスが生成している場合は、以下のコマンドを使用します。デバイスが仮想デバイスの場合、仮想デバイスの ARN をシリアルナンバーとして使用します。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/resync-mfa-device.html](https://docs.aws.amazon.com/cli/latest/reference/iam/resync-mfa-device.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_ResyncMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_ResyncMFADevice.html) 

**IAM で仮想 MFA デバイスのエンティティを削除するには**  
デバイスのユーザーへの関連付けを解除した後、そのデバイスのエンティティを削除できます。
+ AWS CLI: [https://docs.aws.amazon.com/cli/latest/reference/iam/delete-virtual-mfa-device.html](https://docs.aws.amazon.com/cli/latest/reference/iam/delete-virtual-mfa-device.html) 
+ AWS API:[https://docs.aws.amazon.com/IAM/latest/APIReference/API_DeleteVirtualMFADevice.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_DeleteVirtualMFADevice.html) 

**紛失または故障中の仮想 MFA デバイスを復旧するには**  
仮想化 MFA アプリをホストするユーザーのデバイスが紛失、交換、または機能しなくなることがあります。この場合、ユーザーが自分で復旧することはできません。ユーザーは、デバイスを無効にするために管理者に連絡する必要があります。詳細については、「[IAM 内で MFA で保護された ID を復元する](id_credentials_mfa_lost-or-broken.md)」を参照してください。