CloudWatchのログをS3に送る
以下記事の続き。
今回は以下のイメージで、EC2で稼働するApacheのログデータをS3に保管したいと思います。
Kinesis Firehose の設定
WEBコンソールで設定します。
Firehose
の配信ストリームの作成に進む。
送信されるデータは標準で圧縮処理が行われているため、Firehose側での対応は不要です。
CloudWatch Logs から Amazon Kinesis Data Firehose に送信されたデータは、すでに gzip レベル 6 圧縮で圧縮されているため、Kinesis Data Firehose 配信ストリーム内で圧縮を使用する必要はありません。
IAM roleは作成後に選択を行うことで設定に反映されます。
ログ転送に必要な設定を awscli で実施
まず、設定に使用する以下のファイルを作成します。
- TrustPolicyForCWL.json
{ "Statement": { "Effect": "Allow", "Principal": { "Service": "logs.ap-northeast-1.amazonaws.com" }, "Action": "sts:AssumeRole" } }
- PermissionsForCWL.json
{ "Statement":[ { "Effect":"Allow", "Action":["firehose:*"], "Resource":["arn:aws:firehose:ap-northeast-1:999999999999:*"] }, { "Effect":"Allow", "Action":["iam:PassRole"], "Resource":["arn:aws:iam::999999999999:role/CWLtoKinesisFirehoseRole"] } ] }
今回は以下のような構造にしました。
CWLtoKinesisFirehose/ ┣ PermissionsForCWL.json ┗ TrustPolicyForCWL.json
続いて、信頼ポリシーに CloudWatch を指定するIAMロールを作成します。 このロールは CloudWatch だけが引き受けることのできる限定的なロールです。
ubuntu@ip-10-0-0-183:~/CWLtoKinesisFirehose$ aws iam create-role \ > --role-name CWLtoKinesisFirehoseRole \ > --assume-role-policy-document file://~/CWLtoKinesisFirehose/TrustPolicyForCWL.json { "Role": { "Path": "/", "RoleName": "CWLtoKinesisFirehoseRole", "RoleId": "AROA4DFXVDA2FTKIJUU62", "Arn": "arn:aws:iam::999999999999:role/CWLtoKinesisFirehoseRole", "CreateDate": "2019-05-24T00:48:46Z", "AssumeRolePolicyDocument": { "Statement": { "Effect": "Allow", "Principal": { "Service": "logs.ap-northeast-1.amazonaws.com" }, "Action": "sts:AssumeRole" } } } }
Firehoseに対してログデータの送信を許可するポリシーを先ほど作成したCWLtoKinesisFirehoseRole
に設定します。
ubuntu@ip-10-0-0-183:~/CWLtoKinesisFirehose$ aws iam put-role-policy --role-name CWLtoKinesisFirehoseRole \ > --policy-name Permissions-Policy-For-CWL \ > --policy-document file://~/CWLtoKinesisFirehose/PermissionsForCWL.json
iam:PassRole
は条件キーiam:PassedToService
のことで、特定のサービスにのみロールを渡すことができるように制限が行えます。
CloudWatch Logs のサブスクリプションフィルタを作成します。
ubuntu@ip-10-0-0-183:~/CWLtoKinesisFirehose$ aws logs put-subscription-filter \ > --log-group-name "/var/log/apache2/error.log" \ > --filter-name "All" \ > --filter-pattern "" \ > --destination-arn "arn:aws:firehose:ap-northeast-1:999999999999:deliverystream/test-cwl-archive" \ > --role-arn "arn:aws:iam::999999999999:role/CWLtoKinesisFirehoseRole"
CloudWatch のサブスクリプション
に反映されました。
Apacheのerrorlogを出力すると、S3にもログが転送されました。
余談
ロールを設定するjsonファイルでregion
を指定し忘れていたため、以下のエラーにはまってしましました。
ubuntu@ip-10-0-0-183:~/CWLtoKinesisFirehose$ aws logs put-subscription-filter \ > --log-group-name "/var/log/apache2/error.log" \ > --filter-name "All" \ > --filter-pattern "" \ > --destination-arn "arn:aws:firehose:ap-northeast-1:999999999999:deliverystream/test-cwl-archive" \ > --role-arn "arn:aws:iam::999999999999:role/CWLtoKinesisFirehoseRole" An error occurred (InvalidParameterException) when calling the PutSubscriptionFilter operation: Could not deliver test message to specified Firehose stream. Check if the given Firehose stream is in ACTIVE state.
Windows の awscli から実行したときはエラーになったので諦めました。
PS C:\Users\fukui> aws logs put-subscription-filter --log-group-name "/var/log/apache2/error.log" --filter-name "All" --filter-pattern "" --destination-arn "arn:aws:firehose:ap-northeast-1:999999999999:deliverystream/Test-Fukui-Applog" --role-arn "arn:aws:iam::999999999999:role/CWLtoKinesisFirehoseRole" usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters] To see help text, you can run: aws help aws <command> help aws <command> <subcommand> help __main__.py: error: argument --filter-pattern: expected one argument PS C:\Users\fukui>