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?

ALBのアクセスログのCloudWatchLogs直接出力 Terraformで試してみた

0
Posted at

ALBのアクセスログをCloudWatchLogsに直接出力する機能が提供されました。
これはうれしいですね。
料金はかかってしまうようですが、便利な機能なので積極的に活用していきたいです。

ただ、私はTerraformを使用して開発をする機会が多いのですが、Terraformの公式リファレンスにはCloudWatchLogsに直接出力するために必要な設定が明記されていません。

これは困った。

しかし、「Terraformの公式に記載がない」=「Terraformで実装できない」というわけではありません。
(私自身、公式リファレンスに反映されていないAWSの新機能をTerraformで構築成功した経験があります。)

そこで、今回はALBのアクセスログのCloudWatchLogs直接出力のTerraform実装に挑戦してみようと思います。

1.そもそもTerraformで実装可能?

調べたところ、公式リファレンスには未記載ですが、実装自体は可能なようです。

次のGitHub Issueのコメントに、ALBのログをCloudWatch Logsへ配信するTerraformの設定例が共有されています。

この設定例を参考に、私も試してみました。

2.必要なリソース

aws_lb 側の設定は変更不要です。必要なのは、CloudWatch Logsの「ログ配信(Vended Logs)」の仕組みに対応する、次の4つのリソースです。

リソース 役割
aws_cloudwatch_log_group ログの保存先
aws_cloudwatch_log_delivery_source ログの送信元(ALBとログ種別)
aws_cloudwatch_log_delivery_destination ログの送信先(ロググループ)
aws_cloudwatch_log_delivery 送信元と送信先をつなぐ

「送信先となるロググループ本体」「ログ送信元指定」「送信先指定」「送信元と送信先の接続」の4要素が追加で必要になるようです。

GitHub Issueに倣って、次のソースコードを作成しました。

# 配信先ロググループ
# /aws/vendedlogs/ プレフィックスはリソースポリシーのサイズ上限を避けるための推奨名
resource "aws_cloudwatch_log_group" "alb_access" {
  name              = "/aws/vendedlogs/alb/${var.name}/access"
  retention_in_days = var.log_retention_days
}

# ログの送信元: このALBのアクセスログ
resource "aws_cloudwatch_log_delivery_source" "alb_access" {
  name         = "${var.name}-access"
  log_type     = "ALB_ACCESS_LOGS" # 他に ALB_CONNECTION_LOGS / ALB_HEALTH_CHECK_LOGS
  resource_arn = aws_lb.this.arn
}

# ログの送信先: 上のロググループ
resource "aws_cloudwatch_log_delivery_destination" "cwl" {
  name                      = "${var.name}-cwl"
  delivery_destination_type = "CWL"
  output_format             = "json"

  delivery_destination_configuration {
    destination_resource_arn = aws_cloudwatch_log_group.alb_access.arn
  }
}

# 送信元と送信先をつなぐ
# ロググループのリソースポリシー(delivery.logs.amazonaws.com向け)は、
# 実行ユーザーに logs:PutResourcePolicy 等があればAWSが自動で作成する
resource "aws_cloudwatch_log_delivery" "alb_access" {
  delivery_source_name     = aws_cloudwatch_log_delivery_source.alb_access.name
  delivery_destination_arn = aws_cloudwatch_log_delivery_destination.cwl.arn
}

3.結果

terraform plan terraform applyが成功し、ALBのCloudWatchLogsへの直接出力設定を導入することができました。

terraform plan(ログ配信に関わる4リソースのみ抜粋)

  # aws_cloudwatch_log_group.alb_access will be created
  + resource "aws_cloudwatch_log_group" "alb_access" {
      + name              = "/aws/vendedlogs/alb/alb-cwlogs-demo/access"
      + region            = "ap-southeast-2"
      + retention_in_days = 7
      (中略)
    }

  # aws_cloudwatch_log_delivery_source.alb_access will be created
  + resource "aws_cloudwatch_log_delivery_source" "alb_access" {
      + log_type     = "ALB_ACCESS_LOGS"
      + name         = "alb-cwlogs-demo-access"
      + region       = "ap-southeast-2"
      + resource_arn = (known after apply)
      + service      = (known after apply)
      (中略)
    }

  # aws_cloudwatch_log_delivery_destination.cwl will be created
  + resource "aws_cloudwatch_log_delivery_destination" "cwl" {
      + delivery_destination_type = "CWL"
      + name                      = "alb-cwlogs-demo-cwl"
      + output_format             = "json"
      + region                    = "ap-southeast-2"

      + delivery_destination_configuration {
          + destination_resource_arn = (known after apply)
        }
      (中略)
    }

  # aws_cloudwatch_log_delivery.alb_access will be created
  + resource "aws_cloudwatch_log_delivery" "alb_access" {
      + delivery_destination_arn  = (known after apply)
      + delivery_source_name      = "alb-cwlogs-demo-access"
      + region                    = "ap-southeast-2"
      (中略)
    }

Plan: 14 to add, 0 to change, 0 to destroy.

terraform apply(抜粋)

aws_cloudwatch_log_group.alb_access: Creating...
aws_cloudwatch_log_group.alb_access: Creation complete after 1s [id=/aws/vendedlogs/alb/alb-cwlogs-demo/access]
aws_cloudwatch_log_delivery_destination.cwl: Creating...
aws_cloudwatch_log_delivery_destination.cwl: Creation complete after 0s [name=alb-cwlogs-demo-cwl]
(中略)
aws_lb.this: Creation complete after 2m32s [id=arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:loadbalancer/app/alb-cwlogs-demo/xxxxxxxxxxxxxxxx]
aws_cloudwatch_log_delivery_source.alb_access: Creating...
aws_cloudwatch_log_delivery_source.alb_access: Creation complete after 0s [name=alb-cwlogs-demo-access]
aws_cloudwatch_log_delivery.alb_access: Creating...
aws_cloudwatch_log_delivery.alb_access: Creation complete after 0s [id=xxxxxxxxxxxxxxxx]

Apply complete! Resources: 14 added, 0 changed, 0 destroyed.

※今回はほかにもいろいろ作ったので、14 addedと表示されていますが、CloudWatchLogs直接出力設定のみであれば、4 addedになるはずです。

コンソールから確認

コンソールからもCloudWatchLogsへのログ配信がALB側に設定されていることが確認できました。

image.png

ログストリームも作られていました。
image.png

おわりに

今回の検証を通して、ALBのアクセスログのCloudWatchLogs直接出力が無事にTerraformで実装できることがわかりました。

以前はALBのアクセスログをCloudWatchに出力するためにEventBridgeをはさんだりと色々設定が大変だったので、今回試したTerraformでのCloudWatchLogs直接出力を他の場所でも使っていきたいと思います。
この記事が私と同じようにALBのアクセスログのCloudWatchLogs直接出力のTerraform実装で困っている方の一助になれば幸いです。

今回が初投稿になりますが、これからも継続的にAWSに関する記事を執筆していく予定ですので、引き続きよろしくお願いします!

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?