

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

# 步驟 2：建立目的地
<a name="CreateFirehoseStreamDestination"></a>

**重要**  
此程序中的所有步驟必須在日誌資料收件人帳戶中完成。

建立目的地時，CloudWatch Logs 會以收件人帳戶的名義向目的地傳送測試訊息。當訂閱篩選條件在稍後處於作用中狀態時，CloudWatch Logs 會以來源帳戶的名義向目的地傳送日誌事件。

**若要建立目的地**

1. 等到您在 中建立的 Firehose 串流[步驟 1：建立 Firehose 交付串流](CreateFirehoseStream.md)變成作用中。您可以使用以下命令來檢查 **StreamDescription.StreamStatus** 屬性。

   ```
   aws firehose describe-delivery-stream --delivery-stream-name "my-delivery-stream"
   ```

   此外，請留意 **DeliveryStreamDescription.DeliveryStreamARN** 值，因後續步驟中需要用到。此命令的範例輸出：

   ```
   {
       "DeliveryStreamDescription": {
           "DeliveryStreamName": "my-delivery-stream",
           "DeliveryStreamARN": "arn:aws:firehose:us-east-1:222222222222:deliverystream/my-delivery-stream",
           "DeliveryStreamStatus": "ACTIVE",
           "DeliveryStreamEncryptionConfiguration": {
               "Status": "DISABLED"
           },
           "DeliveryStreamType": "DirectPut",
           "VersionId": "1",
           "CreateTimestamp": "2021-02-01T23:59:15.567000-08:00",
           "Destinations": [
               {
                   "DestinationId": "destinationId-000000000001",
                   "S3DestinationDescription": {
                       "RoleARN": "arn:aws:iam::222222222222:role/FirehosetoS3Role",
                       "BucketARN": "arn:aws:s3:::amzn-s3-demo-bucket",
                       "BufferingHints": {
                           "SizeInMBs": 5,
                           "IntervalInSeconds": 300
                       },
                       "CompressionFormat": "UNCOMPRESSED",
                       "EncryptionConfiguration": {
                           "NoEncryptionConfig": "NoEncryption"
                       },
                       "CloudWatchLoggingOptions": {
                           "Enabled": false
                       }
                   },
                   "ExtendedS3DestinationDescription": {
                       "RoleARN": "arn:aws:iam::222222222222:role/FirehosetoS3Role",
                       "BucketARN": "arn:aws:s3:::amzn-s3-demo-bucket",
                       "BufferingHints": {
                           "SizeInMBs": 5,
                           "IntervalInSeconds": 300
                       },
                       "CompressionFormat": "UNCOMPRESSED",
                       "EncryptionConfiguration": {
                           "NoEncryptionConfig": "NoEncryption"
                       },
                       "CloudWatchLoggingOptions": {
                           "Enabled": false
                       },
                       "S3BackupMode": "Disabled"
                   }
               }
           ],
           "HasMoreDestinations": false
       }
   }
   ```

   可能需要花費幾分鐘，交付串流才會變成作用中狀態。

1. 當交付串流處於作用中狀態時，請建立 IAM 角色，授予 CloudWatch Logs 將資料放入 Firehose 串流的許可。首先，您將需要在檔案 **\$1/TrustPolicyForCWL.json** 中建立信任政策。請使用文字編輯器來建立此政策。如需 CloudWatch Logs 端點的詳細資訊，請參閱 [Amazon CloudWatch Logs 端點和配額](https://docs.aws.amazon.com/general/latest/gr/cwl_region.html)。

   此政策包含 `aws:SourceArn` 全域條件內容金鑰，可指定 `sourceAccountId` 以協助預防混淆代理人安全問題。如果您在第一次呼叫中還不知道來源帳戶 ID，我們建議您將目的地 ARN 放在來源 ARN 欄位中。在後續呼叫中，應將來源 ARN 設定為從第一次呼叫中收集的實際來源 ARN。如需詳細資訊，請參閱[預防混淆代理人](Subscriptions-confused-deputy.md)。

   ```
   {
       "Statement": {
           "Effect": "Allow",
           "Principal": {
               "Service": "logs.region.amazonaws.com"
           },
           "Action": "sts:AssumeRole",
           "Condition": {
               "StringLike": {
                   "aws:SourceArn": [
                       "arn:aws:logs:region:sourceAccountId:*",
                       "arn:aws:logs:region:recipientAccountId:*"
                   ]
               }
           }
        }
   }
   ```

1. 使用 **aws iam create-role** 命令來建立 IAM 角色，並指定您剛建立的信任政策檔案。

   ```
   aws iam create-role \
         --role-name CWLtoKinesisFirehoseRole \
         --assume-role-policy-document file://~/TrustPolicyForCWL.json
   ```

   下列為範例輸出。請留意傳回的 `Role.Arn` 值，因為後續步驟中需要用到。

   ```
   {
       "Role": {
           "Path": "/",
           "RoleName": "CWLtoKinesisFirehoseRole",
           "RoleId": "AROAR3BXASEKYJYWF243H",
           "Arn": "arn:aws:iam::222222222222:role/CWLtoKinesisFirehoseRole",
           "CreateDate": "2021-02-02T08:10:43+00:00",
           "AssumeRolePolicyDocument": {
               "Statement": {
                   "Effect": "Allow",
                   "Principal": {
                       "Service": "logs.region.amazonaws.com"
                   },
                   "Action": "sts:AssumeRole",
                   "Condition": {
                       "StringLike": {
                           "aws:SourceArn": [
                               "arn:aws:logs:region:sourceAccountId:*",
                               "arn:aws:logs:region:recipientAccountId:*"
                           ]
                       }
                   }
               }
           }
       }
   }
   ```

1. 建立許可政策以定義 CloudWatch Logs 可在您的帳戶上執行哪些動作。首先，使用文字編輯器在檔案 **\$1/PermissionsForCWL.json** 中建立許可政策：

   ```
   {
       "Statement":[
         {
           "Effect":"Allow",
           "Action":["firehose:*"],
           "Resource":["arn:aws:firehose:region:222222222222:*"]
         }
       ]
   }
   ```

1. 輸入以下命令，將許可政策與角色建立關聯：

   ```
   aws iam put-role-policy --role-name CWLtoKinesisFirehoseRole --policy-name Permissions-Policy-For-CWL --policy-document file://~/PermissionsForCWL.json
   ```

1. Firehose 交付串流處於作用中狀態且您已建立 IAM 角色後，您可以建立 CloudWatch Logs 目的地。

   1. 此步驟不會將存取政策與您的目的地建立關聯，且為完成建立目的地之兩個步驟的僅第一個步驟。記下承載中傳回的新目的地的 ARN，因為您會在後續步驟中使用它作為 `destination.arn`。

      ```
      aws logs put-destination \                                                       
          --destination-name "testFirehoseDestination" \
          --target-arn "arn:aws:firehose:us-east-1:222222222222:deliverystream/my-delivery-stream" \
          --role-arn "arn:aws:iam::222222222222:role/CWLtoKinesisFirehoseRole"
      
      {
          "destination": {
              "destinationName": "testFirehoseDestination",
              "targetArn": "arn:aws:firehose:us-east-1:222222222222:deliverystream/my-delivery-stream",
              "roleArn": "arn:aws:iam::222222222222:role/CWLtoKinesisFirehoseRole",
              "arn": "arn:aws:logs:us-east-1:222222222222:destination:testFirehoseDestination"}
      }
      ```

   1. 上一個步驟完成後，請在日誌資料收件人帳戶中 (222222222222)，將存取政策與目的地建立關聯。

      此政策可讓日誌資料寄件者帳戶 (111111111111) 只能在日誌資料收件人帳戶 (222222222222) 中存取目的地。您可以使用文字編輯器來將此政策在放在 **\$1/AccessPolicy.json** 檔案中：

------
#### [ JSON ]

****  

      ```
      {
        "Version":"2012-10-17",		 	 	 
        "Statement" : [
          {
            "Sid" : "",
            "Effect" : "Allow",
            "Principal" : {
              "AWS" : "111111111111"
            },
            "Action" : "logs:PutSubscriptionFilter",
            "Resource" : "arn:aws:logs:us-east-1:222222222222:destination:testFirehoseDestination"
          }
        ]
      }
      ```

------

   1. 這會建立可定義誰擁有對目的地的寫入存取權之政策。此政策必須指定 **logs:PutSubscriptionFilter** 動作來存取目的地。跨帳戶的使用者將使用 **PutSubscriptionFilter** 動作來將日誌事件傳送到目的地：

      ```
      aws logs put-destination-policy \
          --destination-name "testFirehoseDestination" \
          --access-policy file://~/AccessPolicy.json
      ```