如何将 event_api_destination 添加到 AWS 中的 cloudwatch 规则?

How I can add event_api_destination to cloudwatch rule in AWS?

如何在 terraform 中将资源 aws_cloudwatch_event_api_destination 和 aws_cloudwatch_event_connection 添加到资源 aws_cloudwatch_event_rule?

下面的代码

resource "aws_cloudwatch_event_rule" "test123_schedule_everyday" {
  name                = "${var.test123_bucket_name}-schedule-everyday-${var.env}"
  description         = "Meter reader get api data every 11:00 PM at lotus"
  schedule_expression = "cron(0 16 * * ? *)"
  is_enabled          = "${var.test123_cloudwatch_is_enable}"

  lifecycle {
    ignore_changes = [schedule_expression, description, is_enabled]
  }
}

resource "aws_cloudwatch_event_api_destination" "test123_event_api" {
  name                             = "test-dev-api"
  description                      = "test-dev-api destination"
  invocation_endpoint              = "https://test.com"
  invocation_rate_limit_per_second = "1"
  http_method                      = "GET"
  connection_arn                   = aws_cloudwatch_event_connection.test123_event_connection.arn
}

resource "aws_cloudwatch_event_connection" "test123_event_connection" {
  name               = "test-dev-connection"
  description        = "A connection description"
  authorization_type = "API_KEY"

  auth_parameters {
    api_key {
      key   = "test-key"
      value = "TUVURVJSRUFESU5HOkNCWktXWFU3NzZDS0dGTk5LNjdGWUFVNFFRNE1HV0o3"
    }
  }
}

我相信您要找的是 aws_cloudwatch_event_target. This allows you to specify your destination as a target for a given rule, as documented in the official API destinations 文档。

resource "aws_cloudwatch_event_target" "this" {
  rule = aws_cloudwatch_event_rule.test123_schedule_everyday.name
  arn  = aws_cloudwatch_event_api_destination.test123_event_api.arn
}