terraform为RDS创建cloudwatch事件以触发lambda

时间:2019-06-10 07:10:17

标签: terraform terraform-provider-aws

我想在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”

1 个答案:

答案 0 :(得分:0)

在提供的代码中,您没有创建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}"
}