Grants permission for CloudWatch Events to invoke a specific Lambda function.

Terraform Template

resource "aws_iam_role" "iam_for_lambda" {
  assume_role_policy = jsonencode({
 Version = "2012-10-17"
 Statement = [
 {
 Action = "sts:AssumeRole"
 Effect = "Allow"
 Sid = ""
 Principal = {
 Service = "lambda.amazonaws.com"
 }
 },
 ]
 })
  name = "iam_for_lambda"
}

resource "aws_lambda_alias" "test_alias" {
  description = "a sample description"
  function_name = aws_lambda_function.test_lambda.function_name
  function_version = "$LATEST"
  name = "testalias"
}

resource "aws_lambda_function" "test_lambda" {
}

resource "aws_lambda_permission" "allow_cloudwatch" {
  action = "lambda:InvokeFunction"
  function_name = aws_lambda_function.test_lambda.function_name
  principal = "events.amazonaws.com"
  qualifier = aws_lambda_alias.test_alias.name
  source_arn = "arn:aws:events:eu-west-1:111122223333:rule/RunDaily"
  statement_id = "AllowExecutionFromCloudWatch"
}