Creating an EventBridge Scheduler to Trigger a Lambda Function using Terraform
Tags: lambda, aws, terraform, infrastructureascode
Published: 2023-10-02 15:12:13

**EventBridge** is a fully managed, serverless event bus service that makes it easy to connect applications together using events. It supports a broad set of event sources and targets, including AWS services, SaaS applications, and custom applications.

**EventBridge Scheduler** is a feature of EventBridge that allows you to schedule events to be delivered to targets at specific times or intervals. This makes it easy to automate tasks that need to be run on a regular basis.

In this blog post, we will show you how to create an EventBridge scheduler to trigger a Lambda function using Terraform.

## **Prerequisites**

Before getting started, you will need to have the following prerequisites:

- A Terraform installation
- An AWS account with the necessary permissions to create and manage AWS resources

## **Creating a Terraform configuration**

The first step is to create a Terraform configuration file. This file will define the resources that you want to create, such as the EventBridge scheduler and the Lambda function.

Here is a simple example of a Terraform configuration for an EventBridge scheduler to trigger a Lambda function:

```
# Lambda Function
module "lambda_function" {
  source                 = "terraform-aws-modules/lambda/aws"
  function_name          = var.function_name
  description            = var.description
  handler                = var.handler
  runtime                = var.runtime
  local_existing_package = var.local_path
  vpc_subnet_ids         = var.subnet_ids
  vpc_security_group_ids = var.security_group_ids
  attach_network_policy  = var.attach_network_policy
}

# EventBridge Scheduler
module "eventbridge" {
  source               = "terraform-aws-modules/eventbridge/aws"
  bus_name             = var.bus_name
  attach_lambda_policy = true
  lambda_target_arns   = [module.lambda_function.lambda_function_arn]
  schedules = {
    lambda-cron = {
      description         = "Run Lambda function based on given scheduled"
      schedule_expression = var.schedule_expression
      timezone            = var.timezone
      arn                 = module.lambda_function.lambda_function_arn
    }
  }
}
```

This configuration will create a new EventBridge rule that is scheduled to run based on given scheduled. The rule will target the `var.lambda_function` Lambda function.

## **Deploying the EventBridge scheduler**

Once you have created the Terraform configuration file, you can deploy the EventBridge scheduler by running the following command:

`terraform apply`

This will create the EventBridge rule and the Lambda function in AWS.

## **Testing the EventBridge scheduler**

Once the EventBridge scheduler has been deployed, you can test it to make sure that it is working properly. You can do this by waiting until the next time that the scheduler is scheduled to run and then checking to see if the Lambda function has been invoked.

![eventbridge-lambda-function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nm7jfldo6t0hqkmmy99.png)

## **Conclusion**

In this blog post, we have shown you how to create an EventBridge scheduler to trigger a Lambda function using Terraform. This is a powerful way to automate tasks that need to be run on a regular basis.

You can also use EventBridge Scheduler to trigger other types of targets, such as Amazon Simple Notification Service (SNS) topics, Amazon Simple Queue Service (SQS) queues, and other EventBridge rules.