

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

# 使用 連接組織政策 AWS Organizations
<a name="orgs_policies_attach"></a>

本主題說明如何使用 連接政策 AWS Organizations。*政策*會定義您要套用至 群組的控制項 AWS 帳戶。

**Topics**
+ [連接政策](#attach_policy)

## 使用 連接政策 AWS Organizations
<a name="attach_policy"></a>

**最低許可**  
若要連接政策，您必須具有執行下列動作的許可：  
`organizations:AttachPolicy`

**最低許可**  
若要將授權政策 (SCP 或 RCP) 連接至根帳戶、OU 或帳戶，您需要執行下列動作的許可：  
`organizations:AttachPolicy`，並在包含 "\*" 或指定政策的 Amazon Resource Name（ARN）和您要連接政策的根、OU 或帳戶的 ARN 的相同政策陳述式中具有 `Resource` 元素

### AWS 管理主控台
<a name="attach_policy_console"></a>

------
#### [ Service control policies (SCPs) ]

您可以導覽至政策或連接政策的根、OU 或帳戶，以連接 SCP 政策。

**導覽至根、OU 或帳戶以連接 SCP 政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)** 頁面上，導覽至您要連接 SCP 的根、OU 或帳戶旁邊的核取方塊，然後選擇。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在 **Policies** (政策) 索引標籤的 **Service control policies** (服務控制政策) 項目中，選擇 **Attach** (連接)。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   **Policies** (政策) 索引標籤上的連接的 SCP 清單會更新，以包含新的新增項目。政策變更會立即影響連接的帳戶中 IAM 使用者和角色或連接的根或 OU 下所有帳戶的許可。

**導覽至政策以連接 SCP**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[Service control policies](https://console.aws.amazon.com/organizations/v2/home/policies/service-control-policy)** (服務控制政策) 頁面上，選擇您要連接的政策名稱。

1. 在 **Targets (目標)** 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **Targets** (目標) 索引標籤上的連接的 SCP 清單會更新，以包含新的新增項目。政策變更會立即影響連接的帳戶中 IAM 使用者和角色或連接的根或 OU 下所有帳戶的許可。

------
#### [ Resource control policies (RCPs) ]

您可以透過導覽至政策或要連接政策的根帳戶、OU 或帳戶來連接 RCP。

**若要透過導覽至根、OU 或帳戶來連接 RCP**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在頁面上**[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)**，導覽至 ，然後選擇您要連接 RCP 的根帳戶、OU 帳戶或帳戶旁的核取方塊。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在**政策**索引標籤中，在**資源控制政策**的項目中，選擇**連接**。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   **政策**索引標籤上連接的 RCPs 清單會更新為包含新的新增項目。政策變更會立即生效，影響連接帳戶中的資源許可，或連接根帳戶或 OU 下的所有帳戶許可。

**導覽至政策以連接 RCP**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在**資源控制政策**頁面上，選擇要連接的政策名稱。

1. 在 **Targets (目標)** 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **目標**標籤上連接的 RCPs 清單會更新為包含新的新增項目。政策變更會立即生效，影響連接帳戶中的資源許可，或連接根帳戶或 OU 下的所有帳戶許可。

------
#### [ Declarative policies ]

您可以透過導覽至政策或要連接政策的根、OU 或帳戶來連接宣告性政策。

**若要透過導覽至根、OU 或帳戶來連接宣告政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)** 頁面上，導覽至您要連接政策的根、OU 或帳戶的名稱並選擇。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在**政策**索引標籤的**宣告政策**項目中，選擇**連接**。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   政策****索引標籤上連接的宣告政策清單會更新為包含新的新增項目。政策變更會立即生效。

**導覽至政策以連接宣告式政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在**[宣告政策](https://console.aws.amazon.com/organizations/v2/home/policies/declarative-policy-ec2)**頁面上，選擇您要連接的政策名稱。

1. 在 **Targets (目標)** 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **目標**標籤上連接的宣告政策清單會更新為包含新的新增項目。政策變更會立即生效。

------
#### [ Backup policies ]

您可以導覽至政策或連接政策的根、OU 或帳戶，以連接備份政策。

**導覽至根、OU 或帳戶以連接備份政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)** 頁面上，導覽至您要連接政策的根、OU 或帳戶的名稱並選擇。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在 **Policies** (政策) 索引標籤的 **Backup policies** (備份政策) 項目中，選擇 **Attach** (連接)。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   **Policies** (政策) 索引標籤上的連接的備份政策清單會更新，以包含新的新增項目。政策變更會立即生效。

**導覽至政策以連接備份政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[Backup policies (備份政策)](https://console.aws.amazon.com/organizations/v2/home/policies/backup-policy)** 頁面上，選擇您要連接的政策名稱。

1. 在 **Targets** (目標) 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **Targets** (目標) 索引標籤上的連接的備份政策清單會更新，以包含新的新增項目。政策變更會立即生效。

------
#### [ Tag policies ]

您可以導覽至政策或連接政策的根、OU 或帳戶，以連接標籤政策。

**導覽至根、OU 或帳戶以連接標籤政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)** 頁面上，導覽至您要連接政策的根、OU 或帳戶的名稱並選擇。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在 **Policies** (政策) 索引標籤的 **Tag policies** (標籤政策) 項目中，選擇 **Attach** (連接)。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   **Policies** (政策) 索引標籤上的連接的標籤政策清單會更新，以包含新的新增項目。政策變更會立即生效。

**導覽至政策以連接標籤政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[Tag policies](https://console.aws.amazon.com/organizations/v2/home/policies/tag-policy)** (標籤政策) 頁面上，選擇您要連接的政策名稱。

1. 在 **Targets (目標)** 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **Targets** (目標) 索引標籤上的連接的標籤政策清單會更新，以包含新的新增項目。政策變更會立即生效。

------
#### [ Chat applications policies ]

您可以透過導覽至政策或要連接政策的根帳戶、OU 或帳戶，來連接聊天應用程式政策。

**導覽至根、OU 或帳戶來連接聊天應用程式政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)** 頁面上，導覽至您要連接政策的根、OU 或帳戶的名稱並選擇。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在**政策**索引標籤的**聊天應用程式政策**項目中，選擇**連接**。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   政策索引標籤上連接的聊天應用程式**政策**清單會更新為包含新增項目。政策變更會立即生效。

**導覽至政策以連接聊天應用程式政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[Chatbot 政策](https://console.aws.amazon.com/organizations/v2/home/policies/chatbot-policy)**頁面上，選擇要連接的政策名稱。

1. 在 **Targets (目標)** 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **目標**索引標籤上連接的聊天應用程式政策清單會更新為包含新增項目。政策變更會立即生效。

------
#### [ AI services opt-out policies ]

您可以導覽至政策或連接政策的根、OU 或帳戶，以連接 AI 服務選擇退出政策。

**導覽至根、OU 或帳戶以連接 AI 服務選擇退出政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)** 頁面上，導覽至您要連接政策的根、OU 或帳戶的名稱並選擇。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在 **Policies** (政策) 索引標籤的 **AI service opt-out policies** (AI 服務選擇退出政策) 項目中，選擇 **Attach** (連接)。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   **Policies** (政策) 索引標籤上的連接的 AI 服務選擇退出政策清單會更新，以包含新的新增項目。政策變更會立即生效。

**導覽至政策以連接 AI 服務選擇退出政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AI services opt-out policies](https://console.aws.amazon.com/organizations/v2/home/policies/aiservices-opt-out-policy)** (AI 服務選擇退出政策) 頁面上，選擇您要連接的政策名稱。

1. 在 **Targets** (目標) 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **Targets** (目標) 索引標籤上的連接的 AI 服務選擇退出政策清單會更新，以包含新的新增項目。政策變更會立即生效。

------
#### [ Security Hub policies ]

您可以透過導覽至政策或要連接政策的根帳戶、OU 或帳戶，來連接 Security Hub 政策。

**若要透過導覽至根、OU 或帳戶來連接 Security Hub 政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[AWS 帳戶](https://console.aws.amazon.com/organizations/v2/home/accounts)** 頁面上，導覽至您要連接政策的根、OU 或帳戶的名稱並選擇。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 在**政策**索引標籤中，在 **Security Hub 政策**的項目中，選擇**連接**。

1. 尋找您所需的政策，然後選擇 **Attach policy** (連接政策)。

   政策索引標籤上連接的 Security Hub **政策**清單會更新為包含新的新增項目。政策變更會立即生效。

**導覽至政策以連接 Security Hub 政策**

1. 登入 [AWS Organizations 主控台](https://console.aws.amazon.com/organizations/v2)。您必須以 IAM 使用者登入、擔任 IAM 角色，或是以組織管理帳戶中的根使用者 ([不建議](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)) 身分登入。

1. 在 **[Security Hub 政策](https://console.aws.amazon.com/organizations/v2/home/policies/securityhub-policy)**頁面上，選擇要連接的政策名稱。

1. 在 **Targets (目標)** 索引標籤上，選擇 **Attach** (連接)。

1. 選擇您要連接政策的根、OU 或帳戶旁的選項按鈕。您可能需要展開 OU (選擇 ![Gray cloud icon representing cloud computing or storage services.](http://docs.aws.amazon.com/zh_tw/organizations/latest/userguide/images/console-expand.png))，以尋找您所需的 OU 和帳戶。

1. 選擇**連接政策**。

   **目標**標籤上連接的 Security Hub 政策清單會更新，以包含新的新增項目。政策變更會立即生效。

------

### AWS CLI & AWS SDKs
<a name="attach_policy_cli_sdk"></a>

**連接政策**

下列程式碼範例示範如何使用 `AttachPolicy`。

------
#### [ .NET ]

**適用於 .NET 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/Organizations#code-examples)中設定和執行。

```
    using System;
    using System.Threading.Tasks;
    using Amazon.Organizations;
    using Amazon.Organizations.Model;

    /// <summary>
    /// Shows how to attach an AWS Organizations policy to an organization,
    /// an organizational unit, or an account.
    /// </summary>
    public class AttachPolicy
    {
        /// <summary>
        /// Initializes the Organizations client object and then calls the
        /// AttachPolicyAsync method to attach the policy to the root
        /// organization.
        /// </summary>
        public static async Task Main()
        {
            IAmazonOrganizations client = new AmazonOrganizationsClient();
            var policyId = "p-00000000";
            var targetId = "r-0000";

            var request = new AttachPolicyRequest
            {
                PolicyId = policyId,
                TargetId = targetId,
            };

            var response = await client.AttachPolicyAsync(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine($"Successfully attached Policy ID {policyId} to Target ID: {targetId}.");
            }
            else
            {
                Console.WriteLine("Was not successful in attaching the policy.");
            }
        }
    }
```
+  如需 API 詳細資訊，請參閱《適用於 .NET 的 AWS SDK API 參考》**中的 [AttachPolicy](https://docs.aws.amazon.com/goto/DotNetSDKV3/organizations-2016-11-28/AttachPolicy)。

------
#### [ CLI ]

**AWS CLI**  
**將政策連接到根帳戶、OU 或帳戶**  
**範例 1**  
下列範例示範如何將服務控制政策 (SCP) 連接至 OU：  

```
aws organizations attach-policy
                --policy-id {{p-examplepolicyid111}}
                --target-id {{ou-examplerootid111-exampleouid111}}
```
**範例 2**  
下列範例示範如何將服務控制政策直接連接至帳戶：  

```
aws organizations attach-policy
                --policy-id {{p-examplepolicyid111}}
                --target-id {{333333333333}}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [AttachPolicy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/organizations/attach-policy.html)。

------
#### [ Python ]

**適用於 Python 的 SDK (Boto3)**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/organizations#code-examples)中設定和執行。

```
def attach_policy(policy_id, target_id, orgs_client):
    """
    Attaches a policy to a target. The target is an organization root, account, or
    organizational unit.

    :param policy_id: The ID of the policy to attach.
    :param target_id: The ID of the resources to attach the policy to.
    :param orgs_client: The Boto3 Organizations client.
    """
    try:
        orgs_client.attach_policy(PolicyId=policy_id, TargetId=target_id)
        logger.info("Attached policy %s to target %s.", policy_id, target_id)
    except ClientError:
        logger.exception(
            "Couldn't attach policy %s to target %s.", policy_id, target_id
        )
        raise
```
+  如需 API 詳細資訊，請參閱《AWS SDK for Python (Boto3) API 參考》**中的 [AttachPolicy](https://docs.aws.amazon.com/goto/boto3/organizations-2016-11-28/AttachPolicy)。

------
#### [ SAP ABAP ]

**適用於 SAP ABAP 的開發套件**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/sap-abap/services/org#code-examples)中設定和執行。

```
    TRY.
        lo_org->attachpolicy(
          iv_policyid = iv_policy_id
          iv_targetid = iv_target_id ).
        MESSAGE 'Policy attached to target.' TYPE 'I'.
      CATCH /aws1/cx_orgaccessdeniedex.
        MESSAGE 'You do not have permission to attach the policy.' TYPE 'E'.
      CATCH /aws1/cx_orgpolicynotfoundex.
        MESSAGE 'The specified policy does not exist.' TYPE 'E'.
      CATCH /aws1/cx_orgtargetnotfoundex.
        MESSAGE 'The specified target does not exist.' TYPE 'E'.
      CATCH /aws1/cx_orgduplicateplyatta00.
        MESSAGE 'The policy is already attached to the target.' TYPE 'E'.
    ENDTRY.
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS SAP ABAP 的 SDK API 參考*》中的 [AttachPolicy](https://docs.aws.amazon.com/sdk-for-sap-abap/v1/api/latest/index.html)。

------

政策變更會立即影響連接的帳戶中 IAM 使用者和角色或連接的根或 OU 下所有帳戶的許可