1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

最近Firehoseでエラーログの出力を有効化して、ログ監視設定を行う必要があったので、その設定方法について簡単にまとめました。

  • 動作環境
    • terraform: 1.9.8
    • hashicorp/aws: 5.83.1

既存構成について

以下のような構成でログパイプラインを構築しています。

ECS -> Kinesis Data Stream -> Data Firehose -> S3

エラーログの出力を有効化

Firehoseでデータ配信失敗時にエラーログを記録したいので、エラーログの出力を有効化します。

まず、Firehoseのエラーログを受け取るためのCloudWatchリソースを作成します。

resource "aws_cloudwatch_log_group" "sample_firehose" {
  name              = "/aws/kinesisfirehose/sample-firehose"
  retention_in_days = var.firehose_log_retention_in_days
}

resource "aws_cloudwatch_log_stream" "sample_firehose" {
  name           = "sample-firehose-log-stream"
  log_group_name = aws_cloudwatch_log_group.sample_firehose.name
}

次に、Kinesis Data Firehose配信ストリームでCloudWatchログ出力を有効化します。

resource "aws_kinesis_firehose_delivery_stream" "sample" {
  ...

    cloudwatch_logging_options {
      enabled         = true
      log_group_name  = aws_cloudwatch_log_group.sample_firehose.name
      log_stream_name = aws_cloudwatch_log_stream.sample_firehose.name
    }  

  ...

}

Firehose用のIAMロールとポリシーも適切に設定する必要があります。本記事ではTerraformコードの記述を割愛します。

メトリクスフィルターとアラームの作成

エラーログの監視を行うには、メトリクスフィルターを作成し、CloudWatchアラームを設定する必要があります。

Firehoseのエラーログは基本的にJSON形式で出力され、"errorCode"というフィールドが含まれるため、フィルターパターンはerrorで作成します。

エラーログの一例
{
    "deliveryStreamARN": "arn:aws:firehose:<リージョン名>:<アカウントID>:deliverystream/<リソース名>",
    "destination": "arn:aws:s3:::<S3バケット名>",
    "deliveryStreamVersionId": 3,
    "message": "Access was denied. Ensure that the trust policy for the provided IAM role allows Firehose to assume the role, and the access policy allows access to the S3 bucket.",
    "errorCode": "S3.AccessDenied"
}

より具体的なエラーを検知したい場合は、フィルターパターンを細かく設定できます。

メトリクスフィルターとアラームを作成するTerraformコードの一例です。

resource "aws_cloudwatch_log_metric_filter" "sample_firehose_error" {
  name           = "sample-firehose-error"
  log_group_name = aws_cloudwatch_log_group.sample_firehose.name
  pattern        = "error"

  metric_transformation {
    name      = "sample-firehose-error"
    namespace = "KinesisFirehose/Custom"
    value     = "1"
  }
}

resource "aws_cloudwatch_metric_alarm" "sample_firehose_error" {
  alarm_name          = "sample-firehose-error-alarm"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "1"
  metric_name         = "sample-firehose-error"
  namespace           = "KinesisFirehose/Custom"
  period              = "60"
  statistic           = "Sum"
  threshold           = "1"
  alarm_description   = "Alarm when firehose has errors"
  alarm_actions       = [aws_sns_topic.sample_firehose_error_notification.arn]
  treat_missing_data  = "notBreaching"
}

参考URL

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?