0
0
AWScloud~5 mins

CloudWatch Events (EventBridge) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
CloudWatch Events, now called EventBridge, helps you watch for changes or actions in your AWS environment and respond automatically. It solves the problem of manually checking or reacting to events by letting you set rules that trigger actions when something happens.
When you want to automatically start a backup every day at a specific time.
When you need to trigger a Lambda function whenever a new file is uploaded to an S3 bucket.
When you want to send a notification if an EC2 instance changes its state, like stopping or starting.
When you want to route events from your own applications to AWS services without polling.
When you want to connect different AWS services to work together based on events.
Config File - eventbridge-rule.json
eventbridge-rule.json
{
  "Name": "daily-backup-rule",
  "ScheduleExpression": "rate(1 day)",
  "State": "ENABLED",
  "Targets": [
    {
      "Id": "backupLambda",
      "Arn": "arn:aws:lambda:us-east-1:123456789012:function:dailyBackup"
    }
  ]
}

This JSON file defines a CloudWatch Events rule named daily-backup-rule. It triggers every day using the rate(1 day) schedule. The rule is enabled and targets a Lambda function with the specified ARN. When the rule triggers, it runs the Lambda function to perform the backup.

Commands
This command creates or updates a CloudWatch Events rule named 'daily-backup-rule' that triggers once every day. It enables the rule so it can start working immediately.
Terminal
aws events put-rule --name daily-backup-rule --schedule-expression "rate(1 day)" --state ENABLED
Expected OutputExpected
{"RuleArn":"arn:aws:events:us-east-1:123456789012:rule/daily-backup-rule"}
--name - Sets the name of the event rule.
--schedule-expression - Defines how often the rule triggers using a rate or cron expression.
--state - Enables or disables the rule.
This command attaches a target to the rule. The target is a Lambda function that will run when the rule triggers.
Terminal
aws events put-targets --rule daily-backup-rule --targets Id=backupLambda,Arn=arn:aws:lambda:us-east-1:123456789012:function:dailyBackup
Expected OutputExpected
{"FailedEntryCount":0,"FailedEntries":[]}
--rule - Specifies which rule to attach the target to.
--targets - Defines the target(s) that will be triggered by the rule.
This command lists all the CloudWatch Events rules in your account so you can verify that your rule was created.
Terminal
aws events list-rules
Expected OutputExpected
Name State ScheduleExpression ----------------------------------------------- daily-backup-rule ENABLED rate(1 day)
This command shows the targets attached to the 'daily-backup-rule' so you can confirm your Lambda function is set as a target.
Terminal
aws events list-targets-by-rule --rule daily-backup-rule
Expected OutputExpected
Targets: - Id: backupLambda Arn: arn:aws:lambda:us-east-1:123456789012:function:dailyBackup
--rule - Specifies the rule to list targets for.
Key Concept

If you remember nothing else from this pattern, remember: CloudWatch Events (EventBridge) lets you automatically react to changes or schedules by connecting events to actions.

Common Mistakes
Creating a rule but forgetting to add targets.
The rule triggers but nothing happens because no action is connected.
Always attach at least one target to your rule using 'put-targets' after creating the rule.
Using an incorrect ARN for the target resource.
The event triggers but fails to invoke the target because the ARN is invalid.
Double-check the ARN format and region for your target resource before attaching it.
Setting the rule state to DISABLED.
The rule will not trigger any events until enabled.
Make sure to set the rule state to ENABLED to activate it.
Summary
Create a CloudWatch Events rule with a schedule or event pattern using 'put-rule'.
Attach targets like Lambda functions to the rule using 'put-targets' to define what happens when the rule triggers.
Verify your rule and targets with 'list-rules' and 'list-targets-by-rule' commands.