TeeKay
TeeKay

Reputation: 1065

Configure CloudWatch Events to send input to Lambda function with Terraform

I want to configure CloudWatch Events to send input to Lambda function with Terraform. I have used the following script to do so:

resource "aws_cloudwatch_event_rule" "aa-rule-event" {
  count               = "${var.count}"
  name                = "${var.application_name}-${element(var.names, count.index)}"
  description         = "${element(var.descriptions, count.index)}"
  schedule_expression = "${element(var.cron-expressions, count.index)}"
  is_enabled          = "${element(var.rule-status-states, count.index)}"
}

resource "aws_cloudwatch_event_target" "aa-rule-target" {
  count     = "${var.count}"
  rule      = "${var.application_name}-${element(var.names, count.index)}"
  target_id = "CloudWatchToLambda"
  arn       = "arn:aws:lambda:${var.aws_region}:${var.aws_account_number}:function:${var.application_name}-${element(var.target-lambda-function, count.index)}"
}

I need to give an input to the target Lambda through this CloudWatch Event. I know the input can be configured but how do I configure that in Terraform?

Upvotes: 8

Views: 8782

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56877

The aws_cloudwatch_event_target resource takes an optional input parameter that can pass a JSON blob to the target equivalent to the payload when invoking a Lambda function.

resource "aws_cloudwatch_event_rule" "aa-rule-event" {
  count               = "${var.count}"
  name                = "${var.application_name}-${element(var.names, count.index)}"
  description         = "${element(var.descriptions, count.index)}"
  schedule_expression = "${element(var.cron-expressions, count.index)}"
  is_enabled          = "${element(var.rule-status-states, count.index)}"
}

resource "aws_cloudwatch_event_target" "aa-rule-target" {
  count     = "${var.count}"
  rule      = "${var.application_name}-${element(var.names, count.index)}"
  target_id = "CloudWatchToLambda"
  arn       = "arn:aws:lambda:${var.aws_region}:${var.aws_account_number}:function:${var.application_name}-${element(var.target-lambda-function, count.index)}"
  input = <<JSON
{
  "foo": {
    "bar": [
      1,
      2
    ]
  }
}
JSON
}

Upvotes: 13

Related Questions