

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用指令碼大量遷移您的政策，以使用精細的 IAM  動作
<a name="migrate-iam-permissions"></a>

**注意**  
下列 AWS Identity and Access Management (IAM) 動作已於 2023 年 7 月結束標準支援：  
`aws-portal` 命名空間
`purchase-orders:ViewPurchaseOrders`
`purchase-orders:ModifyPurchaseOrders`
如果您使用的是 AWS Organizations，您可以使用[大量政策 migrator 指令碼](#migrate-iam-permissions)或大量政策 migrator 從您的付款人帳戶更新政策。也可以使用[舊動作至精細動作對應參考](migrate-granularaccess-iam-mapping-reference.md)來確認需要新增的 IAM 動作。  
如果您有 在 2023 年 3 月 6 日上午 11：00 (PDT) 或之後 AWS Organizations 建立的 AWS 帳戶或 的一部分，則精細動作已在組織中生效。

若要協助遷移 IAM 政策以使用新動作 (稱為精細動作)，您可以使用 [AWS  範例](https://github.com/aws-samples/bulk-policy-migrator-scripts-for-account-cost-billing-consoles)網站上的指令碼。

您可以從組織的付款人帳戶執行這些指令碼，以識別組織中使用舊 IAM 動作的以下受影響政策：
+ 客戶受管 IAM 政策
+ 角色、群組和使用者 IAM 內嵌政策
+ 服務控制政策 (SCP) (僅適用於付款人帳戶)
+ 許可集

這些指令碼會針對與政策中使用的現有動作相對應的  新動作產生建議。然後，您可以檢閱建議，並使用指令碼在組織中所有受影響的政策中增加新動作。您不需要更新 AWS 受管政策或 AWS 受管 SCPs（例如， AWS Control Tower and AWS Organizations SCPs)。

您可以使用這些指令碼執行下列操作：
+ 簡化政策更新，協助您從付款人帳戶管理受影響的政策。
+ 減少更新政策所需要的時間。您不需要登入每個成員帳戶然後手動更新政策。
+ 將不同成員帳戶中的相同政策分成一組。然後，您可以檢閱所有相同的政策並套用相同的更新，而不用逐一檢閱。
+ 在 2023 年 7 月 6 日 AWS 淘汰舊的 IAM 動作後，確保使用者存取不會受到影響。

如需政策和服務控制政策 (SCP) 的詳細資訊，請查看以下主題：
+ *《IAM 使用者指南》*中的[管理 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage.html)
+ *《AWS Organizations 使用者指南》*中的[服務控制政策 (SCP)](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html)。
+ *《IAM Identity Center 使用者指南》*中的[自訂許可](https://docs.aws.amazon.com/singlesignon/latest/userguide/permissionsetcustom.html)

## 概觀
<a name="overview-bulk-migrate-policies"></a>

按照本主題完成以下步驟：

**Topics**
+ [概觀](#overview-bulk-migrate-policies)
+ [先決條件](#prerequisites-running-the-scripts)
+ [步驟 1：設定您的環境](#set-up-your-environment-and-download-the-scripts)
+ [步驟 2：建立 CloudFormation StackSet](#create-the-cloudformation-stack)
+ [步驟 3：識別受影響的政策](#identify-the-affected-policies)
+ [步驟 4：檢閱建議的變更](#review-the-affected-policies)
+ [步驟 5：更新受影響的政策](#update-the-affected-policies)
+ [步驟 6：還原您的變更 (選用)](#revert-changes)
+ [IAM 政策範例](#examples-of-similar-policies)

## 先決條件
<a name="prerequisites-running-the-scripts"></a>

若要開始使用，您必須執行以下操作：
+ 下載並安裝 [Python 3](https://www.python.org/downloads/)
+ 登入付款人帳戶，並確認您擁有具有以下 IAM 許可的 IAM 主體：

  ```
  "iam:GetAccountAuthorizationDetails",
  "iam:GetPolicy",
  "iam:GetPolicyVersion",
  "iam:GetUserPolicy",
  "iam:GetGroupPolicy",
  "iam:GetRole",
  "iam:GetRolePolicy",
  "iam:CreatePolicyVersion",
  "iam:DeletePolicyVersion",
  "iam:ListAttachedRolePolicies",
  "iam:ListPolicyVersions",
  "iam:PutUserPolicy",
  "iam:PutGroupPolicy",
  "iam:PutRolePolicy",
  "iam:SetDefaultPolicyVersion",
  "organizations:ListAccounts",
  "organizations:ListPolicies",
  "organizations:DescribePolicy",
  "organizations:UpdatePolicy",
  "organizations:DescribeOrganization",
  "sso:DescribePermissionSet",
  "sso:DescribePermissionSetProvisioningStatus",
  "sso:GetInlinePolicyForPermissionSet",
  "sso:ListInstances",
  "sso:ListPermissionSets",
  "sso:ProvisionPermissionSet",
  "sso:PutInlinePolicyToPermissionSet",
  "sts:AssumeRole"
  ```

**提示**  
若要開始使用，建議您使用帳戶的子集 (例如測試帳戶)，以確認建議的變更符合預期。  
然後，您可以針對組織中其餘的帳戶執行指令碼。

## 步驟 1：設定您的環境
<a name="set-up-your-environment-and-download-the-scripts"></a>

若要開始使用，請從 [AWS  範例](https://github.com/aws-samples/bulk-policy-migrator-scripts-for-account-cost-billing-consoles)網站下載必要檔案。然後，您可以執行命令來設定您的環境。

**設定您的環境。**

1. 從 [AWS  範例](https://github.com/aws-samples/bulk-policy-migrator-scripts-for-account-cost-billing-consoles)網站複製儲存庫。在命令列視窗中，執行以下命令：

   ```
   git clone https://github.com/aws-samples/bulk-policy-migrator-scripts-for-account-cost-billing-consoles.git
   ```

1. 導覽至您下載檔案的目錄。您可以使用下列命令：

   ```
   cd bulk-policy-migrator-scripts-for-account-cost-billing-consoles
   ```

   您可以在儲存器中找到下列指令碼和資源：
   + `billing_console_policy_migrator_role.json` – 在組織的成員帳戶中建立 `BillingConsolePolicyMigratorRole` IAM 角色的 CloudFormation 範本。此角色可讓指令碼擔任該角色，然後讀取並更新受影響的政策。
   + `action_mapping_config.json` – 包含舊動作與新動作的一對多映射。指令碼會使用此檔案，為含有舊動作的每個受影響政策建議新動作。

     每個舊動作都對應至多個精細動作。檔案中建議的新動作可在遷移 AWS 服務 前讓使用者存取相同的 。
   + `identify_affected_policies.py` – 掃描並識別組織中受影響的政策。此指令碼會產生一個 `affected_policies_and_suggestions.json` 檔案，檔案中會列出受影響的政策以及建議的新動作。

     使用相同舊動作集的受影響政策會集中在 JSON 檔案中，以便您能檢閱或更新建議的新動作。
   + `update_affected_policies.py` – 更新組織中受影響的政策。指令碼會輸入 `affected_policies_and_suggestions.json` 檔案，然後將建議的新動作新增至政策。
   + `rollback_affected_policies.py` – (選用) 還原對受影響政策所做的變更。此指令碼會從受影響的政策中移除新的精細動作。

1. 執行以下命令以設定並啟用虛擬環境。

   ```
   python3 -m venv venv
   ```

   ```
   source venv/bin/activate
   ```

1. 執行下列命令來安裝 適用於 Python (Boto3) 的 AWS SDK 相依性。

   ```
   pip install -r requirements.txt
   ```
**注意**  
您必須將 AWS 登入資料設定為使用 AWS Command Line Interface (AWS CLI)。如需詳細資訊，請參閱[適用於 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html)。

如需詳細資訊，請參閱 [README.md](https://github.com/aws-samples/bulk-policy-migrator-scripts-for-account-cost-billing-consoles#readme) 檔案。

## 步驟 2：建立 CloudFormation StackSet
<a name="create-the-cloudformation-stack"></a>

按照此程序來建立 CloudFormation「堆疊集」**。此堆疊集就會為組織中的所有成員帳戶建立 `BillingConsolePolicyMigratorRole` IAM 角色。

**注意**  
您只需要從管理帳戶 (付款人帳戶) 完成此步驟一次。

**若要建立 CloudFormation StackSet**

1. 在文字編輯器中，開啟 `billing_console_policy_migrator_role.json` 檔案，並將 *`<management_account>`* 的每個執行個體替換為付款人帳戶的帳戶 ID (例如，*123456789012*)。

1. 儲存檔案。

1. 以付款人帳戶 AWS 管理主控台 身分登入 。

1. 在 CloudFormation 主控台中，使用您更新的 `billing_console_policy_migrator_role.json` 檔案建立堆疊集。

   如需詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[在 AWS CloudFormation 主控台上建立堆疊集](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-getting-started-create.html)。

CloudFormation 建立堆疊集之後，組織中的每個成員帳戶都具有一個 `BillingConsolePolicyMigratorRole` IAM 角色。

IAM 角色含有以下許可：

```
"iam:GetAccountAuthorizationDetails",
"iam:GetPolicy",
"iam:GetPolicyVersion",
"iam:GetUserPolicy",
"iam:GetGroupPolicy",
"iam:GetRolePolicy",
"iam:CreatePolicyVersion",
"iam:DeletePolicyVersion",
"iam:ListPolicyVersions",
"iam:PutUserPolicy",
"iam:PutGroupPolicy",
"iam:PutRolePolicy",
"iam:SetDefaultPolicyVersion"
```

**備註**  
就每個成員帳戶而言，這些指令碼會呼叫 [AssumeRole](https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html) API 操作以取得擔任 `BillingConsolePolicyMigratorRole` IAM 角色的臨時憑證。
指令碼會呼叫 [ListAccounts](https://docs.aws.amazon.com/organizations/latest/APIReference/API_ListAccounts.html) API 操作，以取得所有成員帳戶。
這些指令碼也會呼叫 IAM API 作業，以執行政策的讀取和寫入許可。

## 步驟 3：識別受影響的政策
<a name="identify-the-affected-policies"></a>

建立堆疊集並下載檔案之後，請執行 `identify_affected_policies.py` 指令碼。此指令碼擔任每個成員帳戶的 `BillingConsolePolicyMigratorRole` IAM 角色，然後識別受影響的政策。

**若要識別受影響的政策**

1. 導覽至您下載指令碼的目錄。

   ```
   cd policy_migration_scripts/scripts
   ```

1. 執行 `identify_affected_policies.py` 指令碼。

您可以使用以下輸入參數：
+ AWS 帳戶 您希望指令碼掃描的 。若要指定帳戶，請使用以下輸入參數：
  +  `--all` – 掃描組織中所有的成員帳戶。

    ```
    python3 identify_affected_policies.py --all
    ```
  +  `--accounts` – 掃描組織中成員帳戶的子集。

    ```
    python3 identify_affected_policies.py --accounts 111122223333, 444455556666, 777788889999
    ```
  + `--exclude-accounts` – 排除組織中的特定成員帳戶。

    ```
    python3 identify_affected_policies.py --all --exclude-accounts 111111111111, 222222222222, 333333333333
    ```
+ ` –-action-mapping-config-file` – (選用) 指定 `action_mapping_config.json` 檔案的路徑。此指令碼會使用此檔案產生受影響政策的建議更新。如果您未指定路徑，指令碼會使用資料夾中的 `action_mapping_config.json` 檔案。

  ```
  python3 identify_affected_policies.py –-action-mapping-config-file c:\Users\username\Desktop\Scripts\action_mapping_config.json –-all
  ```

**注意**  
您無法使用此指令碼來指定組織單位 (OU)。

執行指令碼後，其會在 `Affected_Policies_<Timestamp>` 資料夾中建立兩個 JSON 檔案：
+ `affected_policies_and_suggestions.json`
+ `detailed_affected_policies.json`

**`affected_policies_and_suggestions.json`**  
 列出受影響的政策及建議的新動作。會在檔案中將使用相同舊動作集的受影響政策分成一組。  
此檔案包含以下區段：  
+ 提供您在指令碼中指定之帳戶概觀的中繼資料，包括：
  + 已掃描的帳戶以及用於 `identify_affected_policies.py` 指令碼的輸入參數
  + 受影響的帳戶數目
  + 受影響政策的數目
  + 類似政策群組的數目
+ 類似政策群組 – 包括帳戶清單和政策詳細資訊，包括以下區段：
  + `ImpactedPolicies` – 指定受影響且包含在群組中的政策

     
  + `ImpactedPolicyStatements` – 提供目前在受影響政策中使用舊動作之 `Sid` 區塊的相關資訊。本區段包含舊動作和 IAM 元素，例如 `Effect`、`Principal`、`NotPrincipal`、`NotAction` 和 `Condition`。
+ `SuggestedPolicyStatementsToAppend` – 提供新增為新 `SID` 區塊的建議新動作。

  當您更新政策時，此區塊會附加在政策的末尾。

**Example 範例 `affected_policies_and_suggestions.json` 檔案**  
此檔案會根據以下標準將類似的政策分成一組：  
+ 使用的相同舊動作 – 在所有 `SID` 區塊中具有相同舊動作的政策。
+ 比對詳細資訊 – 除了受影響的動作外，政策還具有相同的 IAM 元素，例如：
  + `Effect` (`Allow`/`Deny`)
  + `Principal` (誰被允許或拒絕存取)
  + `NotAction` (不允許採取哪些動作)
  + `NotPrincipal` (誰被明確拒絕存取)
  + `Resource` （政策適用的 AWS 資源）
  + `Condition` (適用該政策的任何特定條件) 
如需詳細資訊，請參閱[IAM 政策範例](#examples-of-similar-policies)。

**Example 範例 `affected_policies_and_suggestions.json`**  

```
[{
        "AccountsScanned": [
            "111111111111",
            "222222222222"
        ],
        "TotalAffectedAccounts": 2,
        "TotalAffectedPolicies": 2,
        "TotalSimilarPolicyGroups": 2
    },
    {
        "GroupName": "Group1",
        "ImpactedPolicies": [{
                "Account": "111111111111",
                "PolicyType": "UserInlinePolicy",
                "PolicyName": "Inline-Test-Policy-Allow",
                "PolicyIdentifier": "1111111_1-user:Inline-Test-Policy-Allow"
            },
            {
                "Account": "222222222222",
                "PolicyType": "UserInlinePolicy",
                "PolicyName": "Inline-Test-Policy-Allow",
                "PolicyIdentifier": "222222_1-group:Inline-Test-Policy-Allow"
            }
        ],
        "ImpactedPolicyStatements": [
            [{
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": [
                    "aws-portal:ViewAccounts"
                ],
                "Resource": "*"
            }]
        ],
        "SuggestedPolicyStatementsToAppend": [{
            "Sid": "BillingConsolePolicyMigrator0",
            "Effect": "Allow",
            "Action": [
                "account:GetAccountInformation",
                "account:GetAlternateContact",
                "account:GetChallengeQuestions",
                "account:GetContactInformation",
                "billing:GetContractInformation",
                "billing:GetIAMAccessPreference",
                "billing:GetSellerOfRecord",
                "payments:ListPaymentPreferences"
            ],
            "Resource": "*"
        }]
    },
    {
        "GroupName": "Group2",
        "ImpactedPolicies": [{
                "Account": "111111111111",
                "PolicyType": "UserInlinePolicy",
                "PolicyName": "Inline-Test-Policy-deny",
                "PolicyIdentifier": "1111111_2-user:Inline-Test-Policy-deny"
            },
            {
                "Account": "222222222222",
                "PolicyType": "UserInlinePolicy",
                "PolicyName": "Inline-Test-Policy-deny",
                "PolicyIdentifier": "222222_2-group:Inline-Test-Policy-deny"
            }
        ],
        "ImpactedPolicyStatements": [
            [{
                "Sid": "VisualEditor0",
                "Effect": "deny",
                "Action": [
                    "aws-portal:ModifyAccount"
                ],
                "Resource": "*"
            }]
        ],
        "SuggestedPolicyStatementsToAppend": [{
            "Sid": "BillingConsolePolicyMigrator1",
            "Effect": "Deny",
            "Action": [
                "account:CloseAccount",
                "account:DeleteAlternateContact",
                "account:PutAlternateContact",
                "account:PutChallengeQuestions",
                "account:PutContactInformation",
                "billing:PutContractInformation",
                "billing:UpdateIAMAccessPreference",
                "payments:UpdatePaymentPreferences"
            ],
            "Resource": "*"
        }]
    }
]
```

**`detailed_affected_policies.json`**  
包含 `identify_affected_policies.py` 指令碼針對成員帳戶識別的所有受影響政策的定義。  
檔案會將類似的政策分成一組。您可以使用此檔案做為參照，以便檢閱並管理政策變更，無需登入每個成員帳戶，即可單獨檢閱每個政策和帳號的更新。  
您可以在檔案中搜尋政策名稱 (例如 `YourCustomerManagedReadOnlyAccessBillingUser`)，然後檢閱受影響的政策定義。  

**Example 範例：`detailed_affected_policies.json`**  

## 步驟 4：檢閱建議的變更
<a name="review-the-affected-policies"></a>

指令碼建立 `affected_policies_and_suggestions.json` 檔案後，檢閱該檔案並進行任何變更。

**若要檢閱受影響的政策**

1. 在文字編輯器中，開啟 `affected_policies_and_suggestions.json` 檔案。

1. 在 `AccountsScanned` 區段中，確認已掃描帳戶中所識別的類似群組數目符合預期。

1. 檢閱將新增至受影響政策的建議的精細動作。

1. 視需要更新檔案，然後儲存。

### 範例 1：更新 `action_mapping_config.json` 檔案
<a name="example-1-update-action-mapping-config-file"></a>

您可以更新 `action_mapping_config.json` 中建議的映射。更新檔案之後，您可以重新執行 `identify_affected_policies.py` 指令碼。此指令碼會針對受影響的政策產生更新的建議。

您可以建立 `action_mapping_config.json` 檔案的多個版本，以變更具有不同許可之不同帳戶的政策。例如，您可以建立一個名為 `action_mapping_config_testing.json` 的檔案，以遷移測試帳戶的許可，另一個名為 `action_mapping_config_production.json` 的檔案則用於生產帳戶的許可。

### 範例 2：更新 `affected_policies_and_suggestions.json` 檔案
<a name="example-2-make-changes-to-affected-policy-groups"></a>

若要變更特定受影響政策群組的建議替換項，您可以直接編輯 `affected_policies_and_suggestions.json` 檔案中的建議替換項區段。

您在此區段中所做的任何變更都會套用至該特定受影響政策群組內的所有政策。

### 範例 3：自訂特定政策
<a name="example-3-customize-a-specific-policy"></a>

如果您發現受影響的政策群組中的政策需要與建議的更新不同的變更，您可以執行以下操作：
+ 在 `identify_affected_policies.py` 指令碼中排除特定帳戶。然後，您可以單獨檢閱這些被排除帳戶。
+ 移除需要不同許可的受影響政策和帳號，以更新受影響的 `Sid` 區塊。建立僅包含特定帳戶的 JSON 區塊，或將其排除在目前更新的受影響政策執行之外。

  當您重新執行 `identify_affected_policies.py` 指令碼時，只有相關帳號會出現在更新的區塊中。然後，您可以細化該特定 `Sid` 區塊的建議替換項。

## 步驟 5：更新受影響的政策
<a name="update-the-affected-policies"></a>

檢閱並調整建議的替換項之後，執行 `update_affected_policies.py` 指令碼。該指令碼以 `affected_policies_and_suggestions.json` 檔案作為輸入。此指令碼擔任 `BillingConsolePolicyMigratorRole` IAM 角色來更新 `affected_policies_and_suggestions.json` 檔案中列出的受影響政策。

**若要更新受影響的政策**

1. 如果您尚未打開  AWS CLI 的命令行視窗，請打開。

1. 輸入以下命令以執行 `update_affected_policies.py` 指令碼。您可以輸入以下輸入參數：
+ 包含要更新之受影響政策清單之 `affected_policies_and_suggestions.json` 檔案的目錄路徑。此檔案是上一個步驟的輸出。

```
python3 update_affected_policies.py --affected-policies-directory Affected_Policies_<Timestamp>
```

`update_affected_policies.py` 指令碼會使用建議的新動作來更新 `affected_policies_and_suggestions.json` 檔案中的受影響政策。指令碼會將標示為 `BillingConsolePolicyMigrator#` 的 `Sid` 區塊新增至政策，其中 *\$1* 對應至增量計數器 (例如，1、2、3)。

例如，如果受影響政策中有多個使用舊動作的 `Sid` 區塊，則指令碼會新增多個顯示為 `BillingConsolePolicyMigrator#` 的 `Sid` 區塊，以對應至每個 `Sid` 區塊。

**重要**  
指令碼不會移除政策中舊的 IAM 動作，也不會變更政策中的現有 `Sid` 區塊。相反的，其會建立 `Sid` 區塊，並將這些區塊附加到政策的末尾。這些新的 `Sid` 區塊具有 JSON 檔案中建議的新動作。如此可確保不會變更原始政策的許可。
如果需要還原變更，我們不建議您變更 `BillingConsolePolicyMigrator#` `Sid` 區塊的名稱。

**Example 範例：附加有 `Sid` 區塊的政策**  
請參閱 `Sid` 和 `BillingConsolePolicyMigrator1` 區塊中附加的 `BillingConsolePolicyMigrator2` 區塊。

指令碼會產生含有失敗操作的狀態報告，並在本機輸出 JSON 檔案。

**Example 範例：狀態報告**  

```
[{
    "Account": "111111111111",
    "PolicyType": "Customer Managed Policy"
    "PolicyName": "AwsPortalViewPaymentMethods",
    "PolicyIdentifier": "identifier",
    "Status": "FAILURE", // FAILURE or SKIPPED	
    "ErrorMessage": "Error message details"
}]
```

**重要**  
如果您重新執行 `identify_affected_policies.py` 和 `update_affected_policies.py` 指令碼，其會略過含有 `BillingConsolePolicyMigratorRole#``Sid` 區塊的所有政策。指令碼假設該等政策先前已經過掃描和更新，而且不需要其他更新。這樣可防止指令在政策中重複相同的動作。
更新受影響的政策後，您可以藉由使用受影響政策工具來使用新的 IAM。如果您發現任何問題，可以使用此工具切換回先前的動作。您也可以使用指令碼來還原政策更新。  
如需詳細資訊，請參閱 [如何使用受影響的政策工具](migrate-security-iam-tool.md) 和[AWS 帳單、成本管理和帳戶主控台許可的變更](https://aws.amazon.com/blogs/aws-cloud-financial-management/changes-to-aws-billing-cost-management-and-account-consoles-permissions/)部落格文章。
若要管理您的更新，您可以：  
分別針對每個帳戶執行指令碼。
分批針對類似帳戶 (例如測試、QA 和生產帳戶) 執行指令碼。
針對所有帳戶執行指令碼。
選擇分批更新某些帳戶然後分別更新其他帳戶的混合方式。

## 步驟 6：還原您的變更 (選用)
<a name="revert-changes"></a>

`rollback_affected_policies.py` 指令碼會針對指定帳戶還原套用至每個受影響政策的變更。指令碼會移除附加 `update_affected_policies.py` 指令碼的所有 `Sid` 區塊。這些 `Sid` 區塊具有 `BillingConsolePolicyMigratorRole#` 格式。

**若要還原您的變更**

1. 如果您尚未打開  AWS CLI 的命令行視窗，請打開。

1. 輸入以下命令以執行 `rollback_affected_policies.py` 指令碼。您可以輸入以下輸入參數：
+ `--accounts` 
  + 指定您要包含在復原中的 AWS 帳戶 IDs的逗號分隔清單。
  + 下列範例會掃描指定 中的政策 AWS 帳戶，並使用 `BillingConsolePolicyMigrator#``Sid`區塊移除任何陳述式。

    ```
    python3 rollback_affected_policies.py –-accounts 111122223333, 555555555555, 666666666666
    ```
+ `--all`
  + 在您的組織中包含 AWS 帳戶 IDs。
  + 以下範例會掃描組織中的所有政策，並移除具有 `BillingConsolePolicyMigratorRole#` `Sid` 區塊的任何陳述式。

  ```
  python3 rollback_affected_policies.py –-all
  ```
+ `--exclude-accounts`
  + 指定您要從復原中排除 AWS 帳戶 IDs 的逗號分隔清單。

    只有當亦指定 `--all` 參數時，您才能使用此參數。
  + 下列範例會掃描 AWS 帳戶 組織中所有 的政策，但指定的帳戶除外。

    ```
    python3 rollback_affected_policies.py --all --exclude-accounts 777777777777, 888888888888, 999999999999
    ```

## IAM 政策範例
<a name="examples-of-similar-policies"></a>

如果政策在以下方面相同，則被認為是類似政策：
+  跨所有 `Sid` 區塊受影響的動作。
+  下列 IAM 元素中的詳細資訊：
  + `Effect` (`Allow`/`Deny`)
  + `Principal` (誰被允許或拒絕存取)
  + `NotAction` (不允許採取哪些動作)
  + `NotPrincipal` (誰被明確拒絕存取)
  + `Resource` （政策適用的 AWS 資源）
  + `Condition` (適用該政策的任何特定條件)

以下範例顯示 IAM 可能會或可能不會因兩者之間的差異而視為類似的政策。

**Example 範例 1：政策被視為類似**  
每個政策類型皆不同，但兩個政策都含有一個具有相同受影響 `Action` 的 `Sid` 區塊。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:ViewAccount",
            "aws-portal:*Billing"
        ],
        "Resource": "*"
    }]
}
```  
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:ViewAccount",
            "aws-portal:*Billing"
        ],
        "Resource": "*"
    }]
}
```

**Example 範例 2：政策被視為類似**  
兩個政策都含有一個具有相同受影響 `Action` 的 `Sid` 區塊。政策 2 包含其他動作，但這些動作不受影響。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:ViewAccount",
            "aws-portal:*Billing"
        ],
        "Resource": "*"
    }]
}
```  
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:ViewAccount",
            "aws-portal:*Billing",
            "athena:*"
        ],
        "Resource": "*"
    }]
}
```

**Example 範例 3：政策未被視為類似**  
兩個政策都含有一個具有相同受影響 `Action` 的 `Sid` 區塊。但是，政策 2 含有政策 1 中沒有的 `Condition` 元素。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:ViewAccount",
            "aws-portal:*Billing"
        ],
        "Resource": "*"
    }]
}
```  
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:ViewAccount",
            "aws-portal:*Billing",
            "athena:*"
        ],
        "Resource": "*",
        "Condition": {
            "BoolIfExists": {
                "aws:MultiFactorAuthPresent": "true"
            }
        }
    }]
}
```

**Example 範例 4：政策被視為類似**  
政策 1 具有一個含有受影響 `Action` 的 `Sid` 區塊。政策 2 具有多個 `Sid` 區塊，但受影響 `Action` 僅出現在一個區塊中。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:View*"
        ],
        "Resource": "*"
    }]
}
```  
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "aws-portal:View*"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "cloudtrail:Get*"
            ],
            "Resource": "*"
        }
    ]
}
```

**Example 範例 5：政策未被視為類似**  
政策 1 具有一個含有受影響 `Action` 的 `Sid` 區塊。政策 2 具有多個 `Sid` 區塊，且受影響 `Action` 出現在多個區塊中。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "aws-portal:View*"
        ],
        "Resource": "*"
    }]
}
```  
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "aws-portal:View*"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Deny",
            "Action": [
                "aws-portal:Modify*"
            ],
            "Resource": "*"
        }
    ]
}
```

**Example 範例 6：政策被視為類似**  
兩個政策都有多個 `Sid` 區塊，每個 `Sid` 區塊中都有相同的受影響 `Action`。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "aws-portal:*Account",
                "iam:Get*"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Deny",
            "Action": [
                "aws-portal:Modify*",
                "iam:Update*"
            ],
            "Resource": "*"
        }
    ]
}
```  
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "aws-portal:*Account",
                "athena:Get*"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Deny",
            "Action": [
                "aws-portal:Modify*",
                "athena:Update*"
            ],
            "Resource": "*"
        }
    ]
}
```

**Example 範例 7**  
以下兩個政策未被視為類似。  
政策 1 具有一個含有受影響 `Action` 的 `Sid` 區塊。政策 2 具有一個含有相同受影響 `Action` 的 `Sid` 區塊。但是，政策 2 還含有另一個具有不同動作的 `Sid` 區塊。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "aws-portal:*Account",
                "iam:Get*"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Deny",
            "Action": [
                "aws-portal:Modify*",
                "iam:Update*"
            ],
            "Resource": "*"
        }
    ]
}
```  
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "aws-portal:*Account",
                "athena:Get*"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Deny",
            "Action": [
                "aws-portal:*Billing",
                "athena:Update*"
            ],
            "Resource": "*"
        }
    ]
}
```