🌊

サブスクリプションフィルタからLambdaのやつでエラー

2023/11/30に公開
╷
│ Error: putting CloudWatch Logs Subscription Filter (nanntara-log-filter): operation error CloudWatch Logs: PutSubscriptionFilter, https response error StatusCode: 400, RequestID: 14ba679f-c9f4-4c45-b41f-ffffffffffff, InvalidParameterException: Could not execute the lambda function. Make sure you have given CloudWatch Logs permission to execute your function.
│
│   with module.nantara.aws_cloudwatch_log_subscription_filter.filter,
│   on ../../modules/subscription_filter/main.tf line 3, in resource "aws_cloudwatch_log_subscription_filter" "filter":
│    3: resource "aws_cloudwatch_log_subscription_filter" "filter" {

サブスクリプションフィルタからLambdaに投げるやつ作ってたんだけど↑みたいなエラー。

結論から言うと aws_lambda_permission の書き方間違えてた。

resource "aws_lambda_permission" "log_permission" {
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.sns_publish.function_name
  principal     = "logs.ap-northeast-1.amazonaws.com"
  source_arn    = "${data.aws_cloudwatch_log_group.log.arn}:*"
}

↑正しいやつ

間違いその1

resource "aws_lambda_permission" "log_permission" {
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.sns_publish.function_name
  principal     = "logs.ap-northeast-1.amazonaws.com"
  source_arn    = data.aws_cloudwatch_log_group.log.arn
}

:* が必要です。

間違いその2

resource "aws_lambda_permission" "log_permission" {
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.sns_publish.function_name
  principal     = "logs.ap-northeast-1.amazonaws.com"
  source_arn    = "${data.aws_cloudwatch_log_group.log.arn}/*"
}

/ ではない、: や!!!!!!!

ドキュメントにもちゃんと書いてあるのにorz

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission#with-cloudwatch-log-group

ただ、これ、これを書いた時点で

source_arn    = "${data.aws_cloudwatch_log_group.log.arn}/*"

source_arn    = "${data.aws_cloudwatch_log_group.log.arn}"

もないと新規で作れなかったんだよね。

lambda permissionを2つ書いて逃げましたが、はたしてよかったのか…

ただ、毎回差分が出てたかも(思い出したら追記しときます)。

source_arn    = "${data.aws_cloudwatch_log_group.log.arn}*"

でも通るとは思うけど、ちょっと違うなぁと。

Discussion