8

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?

1 Answer 1

13

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, but cannot seem to retrieve the input value within the lambda function (written in Go). Is it in the context somewhere?
Oh good golly! I had the func handler signature wrong -- the input comes in as the 2nd parameter to the handler (not in the context), but it must be of the correct type! I originally had events.SNSEvent as the type (more typical), but in this case I was using a custom type.
warrens i am not getting this input in my go lambda can you help me out where we can find in i am using CloudWatchEvent as event in handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.