

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 的 Firewall Manager 範例 AWS CLI
<a name="cli_2_fms_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 Firewall Manager 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `associate-admin-account`
<a name="fms_AssociateAdminAccount_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `associate-admin-account`。

**AWS CLI**  
**設定 Firewall Manager 管理員帳戶**  
下列 `associate-admin-account` 範例會設定 Firewall Manager 的管理員帳戶。  

```
aws fms associate-admin-account \
    --admin-account 123456789012
```
此命令不會產生輸出。  
如需詳細資訊，請參閱 WAF、Firewall Manager 和 Shield Advanced Developer Guide 中的[設定 AWS Firewall Manager 管理員帳戶](https://docs.aws.amazon.com/waf/latest/developerguide/enable-integration.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [AssociateAdminAccount](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/associate-admin-account.html)。

### `delete-notification-channel`
<a name="fms_DeleteNotificationChannel_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `delete-notification-channel`。

**AWS CLI**  
**移除 Firewall Manager 日誌的 SNS 主題資訊**  
下列 `delete-notification-channel` 範例會移除 SNS 主題資訊。  

```
aws fms delete-notification-channel
```
此命令不會產生輸出。  
如需詳細資訊，請參閱 WAF、Firewall Manager [和 Shield Advanced Developer Guide 中的設定 Amazon SNS 通知和 Amazon CloudWatch 警示](https://docs.aws.amazon.com/waf/latest/developerguide/get-started-fms-shield-cloudwatch.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteNotificationChannel](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/delete-notification-channel.html)。

### `delete-policy`
<a name="fms_DeletePolicy_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `delete-policy`。

**AWS CLI**  
**刪除 Firewall Manager 政策**  
下列 `delete-policy` 範例會移除具有指定 ID 的政策及其所有資源。  

```
aws fms delete-policy \
    --policy-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \
    --delete-all-policy-resources
```
此命令不會產生輸出。  
如需詳細資訊，請參閱 WAF、Firewall Manager 和 Shield Advanced Developer Guide 中的[使用 AWS Firewall Manager 政策](https://docs.aws.amazon.com/waf/latest/developerguide/working-with-policies.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeletePolicy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/delete-policy.html)。

### `disassociate-admin-account`
<a name="fms_DisassociateAdminAccount_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `disassociate-admin-account`。

**AWS CLI**  
**移除 Firewall Manager 管理員帳戶**  
下列 `disassociate-admin-account` 範例會從 Firewall Manager 移除目前的管理員帳戶關聯。  

```
aws fms disassociate-admin-account
```
此命令不會產生輸出。  
如需詳細資訊，請參閱 WAF、Firewall Manager 和 Shield Advanced Developer Guide 中的[設定 AWS Firewall Manager 管理員帳戶](https://docs.aws.amazon.com/waf/latest/developerguide/enable-integration.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [DisassociateAdminAccount](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/disassociate-admin-account.html)。

### `get-admin-account`
<a name="fms_GetAdminAccount_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `get-admin-account`。

**AWS CLI**  
**擷取 Firewall Manager 管理員帳戶**  
下列 `get-admin-account` 範例會擷取管理員帳戶。  

```
aws fms get-admin-account
```
輸出：  

```
{
    "AdminAccount": "123456789012",
    "RoleStatus": "READY"
}
```
如需詳細資訊，請參閱 *AWS WAF、 AWS Firewall Manager 和 AWS Shield Advanced Developer Guide 中的* [AWS Firewall Manager 先決條件](https://docs.aws.amazon.com/waf/latest/developerguide/fms-prereq.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetAdminAccount](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/get-admin-account.html)。

### `get-compliance-detail`
<a name="fms_GetComplianceDetail_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `get-compliance-detail`。

**AWS CLI**  
**擷取帳戶的合規資訊**  
下列 `get-compliance-detail` 範例會擷取指定之政策和成員帳戶的合規資訊。  

```
aws fms get-compliance-detail \
    --policy-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \
    --member-account 123456789012
```
輸出：  

```
{
    "PolicyComplianceDetail": {
    "EvaluationLimitExceeded": false,
    "IssueInfoMap": {},
    "MemberAccount": "123456789012",
    "PolicyId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
    "PolicyOwner": "123456789012",
    "Violators": []
}
```
如需詳細資訊，請參閱 *AWS WAF、 AWS Firewall Manager 和 AWS Shield Advanced Developer Guide* 中的[檢視資源是否符合政策](https://docs.aws.amazon.com/waf/latest/developerguide/fms-compliance.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetComplianceDetail](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/get-compliance-detail.html)。

### `get-notification-channel`
<a name="fms_GetNotificationChannel_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `get-notification-channel`。

**AWS CLI**  
**擷取 Firewall Manager 日誌的 SNS 主題資訊**  
下列 `get-notification-channel` 範例會擷取 SNS 主題資訊。  

```
aws fms get-notification-channel
```
輸出：  

```
{
    "SnsTopicArn": "arn:aws:sns:us-west-2:123456789012:us-west-2-fms",
    "SnsRoleName": "arn:aws:iam::123456789012:role/aws-service-role/fms.amazonaws.com/AWSServiceRoleForFMS"
}
```
如需詳細資訊，請參閱 WAF、Firewall Manager [和 Shield Advanced Developer Guide 中的設定 Amazon SNS 通知和 Amazon CloudWatch 警示](https://docs.aws.amazon.com/waf/latest/developerguide/get-started-fms-shield-cloudwatch.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetNotificationChannel](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/get-notification-channel.html)。

### `get-policy`
<a name="fms_GetPolicy_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `get-policy`。

**AWS CLI**  
**擷取 Firewall Manager 政策**  
下列 `get-policy` 範例會擷取具有指定 ID 的政策。  

```
aws fms get-policy \
    --policy-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111
```
輸出：  

```
{
    "Policy": {
        "PolicyId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "PolicyName": "test",
        "PolicyUpdateToken": "1:p+2RpKR4wPFx7mcrL1UOQQ==",
        "SecurityServicePolicyData": {
            "Type": "SECURITY_GROUPS_COMMON",
            "ManagedServiceData": "{\"type\":\"SECURITY_GROUPS_COMMON\",\"revertManualSecurityGroupChanges\":true,\"exclusiveResourceSecurityGroupManagement\":false,\"securityGroups\":[{\"id\":\"sg-045c43ccc9724e63e\"}]}"
        },
        "ResourceType": "AWS::EC2::Instance",
        "ResourceTags": [],
        "ExcludeResourceTags": false,
        "RemediationEnabled": false
    },
    "PolicyArn": "arn:aws:fms:us-west-2:123456789012:policy/d1ac59b8-938e-42b3-b2e0-7c620422ddc2"
}
```
如需詳細資訊，請參閱 WAF、Firewall Manager 和 Shield Advanced Developer Guide 中的[使用 AWS Firewall Manager 政策](https://docs.aws.amazon.com/waf/latest/developerguide/working-with-policies.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetPolicy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/get-policy.html)。

### `list-compliance-status`
<a name="fms_ListComplianceStatus_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-compliance-status`。

**AWS CLI**  
**擷取成員帳戶的政策合規資訊**  
下列 `list-compliance-status` 範例會擷取指定政策的成員帳戶合規資訊。  

```
aws fms list-compliance-status \
    --policy-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111
```
輸出：  

```
{
    "PolicyComplianceStatusList": [
        {
            "PolicyOwner": "123456789012",
            "PolicyId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "PolicyName": "test",
            "MemberAccount": "123456789012",
            "EvaluationResults": [
                {
                    "ComplianceStatus": "COMPLIANT",
                    "ViolatorCount": 0,
                    "EvaluationLimitExceeded": false
                },
                {
                    "ComplianceStatus": "NON_COMPLIANT",
                    "ViolatorCount": 2,
                    "EvaluationLimitExceeded": false
                }
            ],
            "LastUpdated": 1576283774.0,
            "IssueInfoMap": {}
        }
    ]
}
```
如需詳細資訊，請參閱 *AWS WAF、 AWS Firewall Manager 和 AWS Shield Advanced Developer Guide* 中的[檢視資源是否符合政策](https://docs.aws.amazon.com/waf/latest/developerguide/fms-compliance.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListComplianceStatus](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/list-compliance-status.html)。

### `list-member-accounts`
<a name="fms_ListMemberAccounts_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-member-accounts`。

**AWS CLI**  
**擷取組織中的成員帳戶**  
下列 `list-member-accounts` 範例列出 Firewall Manager 管理員組織中的所有成員帳戶。  

```
aws fms list-member-accounts
```
輸出：  

```
{
    "MemberAccounts": [
        "222222222222",
        "333333333333",
        "444444444444"
    ]
}
```
如需詳細資訊，請參閱 WAF 中的 [AWS Firewall Manager](https://docs.aws.amazon.com/waf/latest/developerguide/fms-chapter.html)、Firewall Manager 和 Shield Advanced Developer Guide。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListMemberAccounts](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/list-member-accounts.html)。

### `list-policies`
<a name="fms_ListPolicies_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-policies`。

**AWS CLI**  
**擷取所有 Firewall Manager 政策**  
下列 `list-policies` 範例會擷取帳戶的政策清單。在此範例中，每個請求的輸出限制為兩個結果。每個呼叫都會傳回 `NextToken`，可用來做為下次 `list-policies` 呼叫中 `--starting-token` 參數的值，以取得清單的下一組結果。  

```
aws fms list-policies \
    --max-items 2
```
輸出：  

```
{
    "PolicyList": [
        {
            "PolicyArn": "arn:aws:fms:us-west-2:123456789012:policy/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "PolicyId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "PolicyName": "test",
            "ResourceType": "AWS::EC2::Instance",
            "SecurityServiceType": "SECURITY_GROUPS_COMMON",
            "RemediationEnabled": false
        },
        {
            "PolicyArn": "arn:aws:fms:us-west-2:123456789012:policy/a1b2c3d4-5678-90ab-cdef-EXAMPLE22222",
            "PolicyId": "457c9b21-fc94-406c-ae63-21217395ba72",
            "PolicyName": "test",
            "ResourceType": "AWS::EC2::Instance",
            "SecurityServiceType": "SECURITY_GROUPS_COMMON",
            "RemediationEnabled": false
        }
    ],
    "NextToken": "eyJOZXh0VG9rZW4iOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAyfQ=="
}
```
如需詳細資訊，請參閱 WAF、Firewall Manager 和 Shield Advanced Developer Guide 中的[使用 AWS Firewall Manager 政策](https://docs.aws.amazon.com/waf/latest/developerguide/working-with-policies.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListPolicies](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/list-policies.html)。

### `put-notification-channel`
<a name="fms_PutNotificationChannel_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `put-notification-channel`。

**AWS CLI**  
**設定 Firewall Manager 日誌的 SNS 主題資訊**  
下列 `put-notification-channel` 範例會設定 SNS 主題資訊。  

```
aws fms put-notification-channel \
    --sns-topic-arn arn:aws:sns:us-west-2:123456789012:us-west-2-fms \
    --sns-role-name arn:aws:iam::123456789012:role/aws-service-role/fms.amazonaws.com/AWSServiceRoleForFMS
```
此命令不會產生輸出。  
如需詳細資訊，請參閱 WAF、Firewall Manager [和 Shield Advanced Developer Guide 中的設定 Amazon SNS 通知和 Amazon CloudWatch 警示](https://docs.aws.amazon.com/waf/latest/developerguide/get-started-fms-shield-cloudwatch.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [PutNotificationChannel](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/put-notification-channel.html)。

### `put-policy`
<a name="fms_PutPolicy_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `put-policy`。

**AWS CLI**  
**建立 Firewall Manager 政策**  
以下 `put-policy` 範例會建立 Firewall Manager 安全群組政策範例。  

```
aws fms put-policy \
    --cli-input-json file://policy.json
```
`policy.json` 的內容：  

```
{
    "Policy": {
        "PolicyName": "test",
        "SecurityServicePolicyData": {
            "Type": "SECURITY_GROUPS_USAGE_AUDIT",
            "ManagedServiceData": "{\"type\":\"SECURITY_GROUPS_USAGE_AUDIT\",\"deleteUnusedSecurityGroups\":false,\"coalesceRedundantSecurityGroups\":true}"
        },
        "ResourceType": "AWS::EC2::SecurityGroup",
        "ResourceTags": [],
        "ExcludeResourceTags": false,
        "RemediationEnabled": false
    },
    "TagList": [
        {
            "Key": "foo",
            "Value": "foo"
        }
    ]
}
```
輸出：  

```
{
    "Policy": {
        "PolicyId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "PolicyName": "test",
        "PolicyUpdateToken": "1:X9QGexP7HASDlsFp+G31Iw==",
        "SecurityServicePolicyData": {
            "Type": "SECURITY_GROUPS_USAGE_AUDIT",
            "ManagedServiceData": "{\"type\":\"SECURITY_GROUPS_USAGE_AUDIT\",\"deleteUnusedSecurityGroups\":false,\"coalesceRedundantSecurityGroups\":true,\"optionalDelayForUnusedInMinutes\":null}"
        },
        "ResourceType": "AWS::EC2::SecurityGroup",
        "ResourceTags": [],
        "ExcludeResourceTags": false,
        "RemediationEnabled": false
    },
    "PolicyArn": "arn:aws:fms:us-west-2:123456789012:policy/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
```
如需詳細資訊，請參閱 WAF、Firewall Manager 和 Shield Advanced Developer Guide 中的[使用 AWS Firewall Manager 政策](https://docs.aws.amazon.com/waf/latest/developerguide/working-with-policies.html)。 *AWS AWS AWS *  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [PutPolicy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fms/put-policy.html)。