【问题标题】:terraform to create cloudwatch event for RDS to trigger lambdaterraform 为 RDS 创建 cloudwatch 事件以触发 lambda
【发布时间】:2019-10-24 14:16:58
【问题描述】:

我想在 RDS 实例重新启动或停止时运行 AWS lambda 函数。 我正在探索创建 cloudwatch 事件,每当 RDS 实例重新启动或停止时,它都应该触发我的 lambda 函数。

如何使用 terraform 进行设置。我已经使用下面的方法尝试了,但它没有执行。

provider "aws" 
{
    access_key = ""
    secret_key = ""
version = "~> 2.10"
    region = "ap-southeast-1"
}
resource "aws_cloudwatch_event_rule" "test-RDS-event" {
  name = "test-RDS-event"
  description = "Capture RDS event for lambda target"
  event_pattern = <<pattern
  {
    "source": [
      "aws.rds"
      ],
      "detail-type": [
        "RDS DB Instance Event"
      ],
      "detail": {
        "EventCategories": [
          "failover"
        ]
      }

  }
pattern
}
resource "aws_cloudwatch_event_target" "lambda" {
  rule = "${aws_cloudwatch_event_rule.test-RDS-event.name}"
  target_id = "populate_NLB_TG_with_RDS"
  arn = "${aws_cloudwatch_event_target.lambda.arn}"
  }

执行此操作时出现以下错误,我不确定需要设置什么来修复错误。

C:\Terraform>terraform.exe 计划

错误:aws_cloudwatch_event_target.lambda: aws_cloudwatch_event_target.lambda:不允许自我引用: “aws_cloudwatch_event_target.lambda.arn”

【问题讨论】:

  • 到目前为止你有什么尝试?
  • resource "aws_cloudwatch_event_rule" "test-RDS-event" { name = "test-RDS-event" description = "Capture RDS event for lambda target" event_pattern =
  • 我在上面写过,当我尝试初始化时,我收到以下错误错误:加载 C:\Terraform\example.tf 时出错:读取 aws_cloudwatch_event_target[lambda] 的配置时出错:aws_lambda_function.populate_NLB_TG_with_RDS:resource变量必须是三部分: TYPE.NAME.ATTR in: ${aws_lambda_function.populate_NLB_TG_with_RDS}
  • 您应该编辑您的问题以包含 Terraform 代码,而不是将其放在未格式化的注释中。
  • 嗨,对不起,我对这个门户和编程很陌生,所以我不太确定。我已经更新了代码和我得到的错误。对上述任何建议。

标签: terraform terraform-provider-aws


【解决方案1】:

在提供的代码中,您没有创建 lambda 函数,因此 - 不要使用 lambda 的 arn。它应该类似于:

resource "aws_lambda_function" "rds-events" {
  filename = "${data.archive_file.start_scheduler.output_path}"
  function_name = "rds-events"
  ...
} 

resource "aws_cloudwatch_event_target" "lambda" {
  rule = "${aws_cloudwatch_event_rule.test-RDS-event.name}"
  target_id = "populate_NLB_TG_with_RDS"
  arn = "${aws_lambda_function.rds-events.arn}"
}

【讨论】:

    猜你喜欢
    • 2021-03-21
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2020-04-15
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多