0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CWLのサブスクリプションフィルター作成(クロスアカウント)

Posted at

参考サイト
https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/logs/CreateDestination.html
https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/logs/CreateSubscriptionFilter.html

大まかに説明

1~3、5の手順は送信されるアカウント側の作業
4の手順は送信するアカウント側の作業

1.Kinesis で送信先ストリームを作成

EC2インスタンスにログインし、以下のコマンドを実施

aws kinesis create-stream --stream-name "RecipientStream" --shard-count 1

2.Kinesis ロールを作成

(1)まずはポリシーを作成viコマンドで「TrustPolicyForCWL.json」を作成する

# vi TrustPolicyForCWL.json

以下の内容でポリシーを作成

{
  "Statement": {
    "Effect": "Allow",
    "Principal": { "Service": "logs.ap-northeast-1.amazonaws.com" },
    "Action": "sts:AssumeRole"
  }
}

(2)ロール作成

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

(3)アクセス許可ポリシー「PermissionsForCWL.json」を作成

# vi PermissionsForCWL.json
{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "kinesis:PutRecord",
      "Resource": "arn:aws:kinesis:ap-northeast-1:送信されるアカウントID:stream/RecipientStream"
    },
    {
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::送信されるアカウントID:role/CWLtoKinesisRole"
    }
  ]
}

(4)ポリシーを先ほど作成したロールにアタッチ

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

3.CloudWatch Logs の送信先を作成

(1)以下のコマンドでCloudWatch Logs の送信先を作成

# aws logs put-destination --destination-name "任意の名前" --target-arn "arn:aws:kinesis:ap-northeast-1:送信されるアカウントID:stream/RecipientStream" --role-arn "arn:aws:iam::送信されるアカウントID:role/CWLtoKinesisRole"

(2)作成したCloudWatchLogsのアクセスポリシー作成 「AccessPolicy.json」作成

# vi AccessPolicy.json
{
  "Version" : "2012-10-17",
  "Statement" : [
    {
      "Sid" : "",
      "Effect" : "Allow",
      "Principal" : {
        "AWS" : "送信するアカウントID"
      },
      "Action" : "logs:PutSubscriptionFilter",
      "Resource" : "arn:aws:logs:ap-northeast-1:送信されるアカウントID:destination:任意の名前"
    }
  ]
}

(3)ポリシー割り当て

aws logs put-destination-policy \
--destination-name "任意の名前" \
--access-policy file://AccessPolicy.json

4.CloudWatchLogsサブスクリプションフィルター作成

(1)転送設定実施この手順を転送したいログの分だけ実施する

転送したいログは以下の通り(例)

  • messages/api/prod
  • messages/api/stg
  • messages/batch/prod
  • messages/batch/stg
  • messages/error/prod
  • messages/error/stg

フィルターパターンに関して以下のログは「Exception -CancellationException」とする
※「Exception」は拾い「-CancellationException」は除外

  • messages/api/prod
  • messages/api/stg
  • messages/batch/prod
  • messages/batch/stg
aws logs put-subscription-filter \
--log-group-name "ここは可変" \
--filter-name "任意の名前" \
--filter-pattern "フィルターパターン" \
--destination-arn "arn:aws:logs:ap-northeast-1:送信されるアカウントID:destination:任意の名前"

例)messages/api/prodの設定

aws logs put-subscription-filter \
--log-group-name "messages/api/prod" \
--filter-name "任意の名前" \
--filter-pattern "Exception -CancellationException" \
--destination-arn "arn:aws:logs:ap-northeast-1:送信されるアカウントID:destination:任意の名前"

5.kinesis fire data firehose作成

(1)S3のバケットを作成しておくバケット名「任意の名前」

(2)firehose作成

![image][pasted-2020.06.23-15.48.00.png]

(3)各種設定項目を以下の通りに入力し、作成
[Delivery stream name]
任意の名前

[Souce]
Kinesis Data Stream

[Kinesis data stream]
RecipientStream

[Data transformation]
Disable

[Record format conversion]
Disable

[Destination]
Amazon S3

[S3 destination]
任意の名前

[S3 prefix]
JCB

[S3 error prefix]
null※空白のまま

[Buffer size]
5

[Buffer interval]
300

[S3 compression]
Dsable

[S3 encryption]
Disable

[Error logging]
Disabled

[IAM role]
firehose_delivery_role

これで設定完了

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?