EventBridge examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with EventBridge.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use delete-rule.
- AWS CLI
-
To delete a CloudWatch Events rule
The following
delete-ruleexample deletes the rule namedEC2InstanceStateChanges.aws events delete-rule \ --name"EC2InstanceStateChanges"This command produces no output.
For more information, see Disabling or deleting a rule in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see DeleteRule
in AWS CLI Command Reference.
-
The following code example shows how to use describe-rule.
- AWS CLI
-
To display information about a CloudWatch Events rule
The following
describe-ruleexample displays information about the rule namedDailyLambdaFunction.aws events describe-rule \ --name"DailyLambdaFunction"Output:
{ "Name": "DailyLambdaFunction", "Arn": "arn:aws:events:us-west-2:123456789012:rule/DailyLambdaFunction", "ScheduleExpression": "cron(0 9 * * ? *)", "State": "ENABLED", "EventBusName": "default", "CreatedBy": "123456789012" }For more information, see Rules in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see DescribeRule
in AWS CLI Command Reference.
-
The following code example shows how to use disable-rule.
- AWS CLI
-
To disable a CloudWatch Events rule
The following
disable-ruleexample disables the rule namedDailyLambdaFunction. The rule is not deleted.aws events disable-rule \ --name"DailyLambdaFunction"This command produces no output.
For more information, see Disabling or deleting a rule in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see DisableRule
in AWS CLI Command Reference.
-
The following code example shows how to use enable-rule.
- AWS CLI
-
To enable a CloudWatch Events rule
The following
enable-ruleexample enables the rule namedDailyLambdaFunction, which had been previously disabled.aws events enable-rule \ --name"DailyLambdaFunction"This command produces no output.
For more information, see Rules in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see EnableRule
in AWS CLI Command Reference.
-
The following code example shows how to use list-rule-names-by-target.
- AWS CLI
-
To display all the rules that have a specified target
The following
list-rule-names-by-targetexample displays all rules that have the Lambda function namedMyFunctionNameas the target.aws events list-rule-names-by-target \ --target-arn"arn:aws:lambda:us-west-2:123456789012:function:MyFunctionName"Output:
{ "RuleNames": [ "DailyLambdaFunction" ] }For more information, see Event bus targets in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see ListRuleNamesByTarget
in AWS CLI Command Reference.
-
The following code example shows how to use list-rules.
- AWS CLI
-
Example 1: To display a list of all CloudWatch Events rules
The following
list-rulesexample displays all CloudWatch Events rules in the region.aws events list-rulesOutput:
{ "Rules": [ { "Name": "DailyLambdaFunction", "Arn": "arn:aws:events:us-west-2:123456789012:rule/DailyLambdaFunction", "State": "ENABLED", "ScheduleExpression": "cron(0 9 * * ? *)", "EventBusName": "default" }, { "Name": "EC2InstanceStateChanges", "Arn": "arn:aws:events:us-west-2:123456789012:rule/EC2InstanceStateChanges", "EventPattern": "{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EC2 Instance State-change Notification\"]}", "State": "ENABLED", "RoleArn": "arn:aws:iam::123456789012:role/MyRoleForThisRule", "EventBusName": "default" } ] }Example 2: To display a list of CloudWatch Events rules beginning with a certain string
The following
list-rulesexample displays all CloudWatch Events rules in the region that have a name starting withDaily.aws events list-rules \ --name-prefix"Daily"Output:
{ "Rules": [ { "Name": "DailyLambdaFunction", "Arn": "arn:aws:events:us-west-2:123456789012:rule/DailyLambdaFunction", "State": "ENABLED", "ScheduleExpression": "cron(0 9 * * ? *)", "EventBusName": "default" } ] }For more information, see Rules in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see ListRules
in AWS CLI Command Reference.
-
The following code example shows how to use list-targets-by-rule.
- AWS CLI
-
To display all the targets for a CloudWatch Events rule
The following
list-targets-by-ruleexample displays all the targets of the rule namedDailyLambdaFunction.aws events list-targets-by-rule \ --rule"DailyLambdaFunction"Output:
{ "Targets": [ { "Id": "1", "Arn": "arn:aws:lambda:us-west-2:123456789012:function:MyFunctionName" } ] }For more information, see Event bus targets in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see ListTargetsByRule
in AWS CLI Command Reference.
-
The following code example shows how to use put-events.
- AWS CLI
-
To send a custom event to CloudWatch Events
The following
put-eventsexample sends a custom event to CloudWatch Events. The event is contained within theputevents.jsonfile.aws events put-events \ --entriesfile://putevents.jsonContents of
putevents.json:[ { "Source": "com.mycompany.myapp", "Detail": "{ \"key1\": \"value1\", \"key2\": \"value2\" }", "Resources": [ "resource1", "resource2" ], "DetailType": "myDetailType" }, { "Source": "com.mycompany.myapp", "Detail": "{ \"key1\": \"value3\", \"key2\": \"value4\" }", "Resources": [ "resource1", "resource2" ], "DetailType": "myDetailType" } ]Output:
{ "FailedEntryCount": 0, "Entries": [ { "EventId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" }, { "EventId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" } ] }For more information, see Sending events with PutEvents in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see PutEvents
in AWS CLI Command Reference.
-
The following code example shows how to use put-rule.
- AWS CLI
-
To create CloudWatch Events rules
The following
put-ruleexample creates a rule that triggers every day at 9:00am (UTC). If you useput-targetsto add a Lambda function as a target of this rule, you could run the Lambda function every day at the specified time.aws events put-rule \ --name"DailyLambdaFunction"\ --schedule-expression"cron(0 9 * * ? *)"Output:
{ "RuleArn": "arn:aws:events:us-west-2:123456789012:rule/DailyLambdaFunction" }The following
put-ruleexample creates a rule that triggers when any EC2 instance in the region changes state.aws events put-rule \ --name"EC2InstanceStateChanges"\ --event-pattern "{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EC2 Instance State-change Notification\"]}" \ --role-arn"arn:aws:iam::123456789012:role/MyRoleForThisRule"Output:
{ "RuleArn": "arn:aws:events:us-west-2:123456789012:rule/EC2InstanceStateChanges" }The following
put-ruleexample creates a rule that triggers when any EC2 instance in the region is stopped or terminated.aws events put-rule \ --name"EC2InstanceStateChangeStopOrTerminate"\ --event-pattern "{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EC2 Instance State-change Notification\"],\"detail\":{\"state\":[\"stopped\",\"terminated\"]}}" \ --role-arn"arn:aws:iam::123456789012:role/MyRoleForThisRule"Output:
{ "RuleArn": "arn:aws:events:us-west-2:123456789012:rule/EC2InstanceStateChangeStopOrTerminate" }For more information, see Rules in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see PutRule
in AWS CLI Command Reference.
-
The following code example shows how to use put-targets.
- AWS CLI
-
To add targets for CloudWatch Events rules
The following
put-targetsexample adds a Lambda function as the target of a rule.aws events put-targets \ --ruleDailyLambdaFunction\ --targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:123456789012:function:MyFunctionName"Output:
{ "FailedEntryCount": 0, "FailedEntries": [] }The following
put-targetsexample sets an Amazon Kinesis stream as the target, so that events caught by this rule are relayed to the stream.aws events put-targets \ --ruleEC2InstanceStateChanges\ --targets "Id"="1","Arn"="arn:aws:kinesis:us-west-2:123456789012:stream/MyStream","RoleArn"="arn:aws:iam::123456789012:role/MyRoleForThisRule"Output:
{ "FailedEntryCount": 0, "FailedEntries": [] }The following
put-targetsexample sets two Amazon Kinesis streams as targets for one rule.aws events put-targets \ --ruleDailyLambdaFunction\ --targets "Id"="Target1","Arn"="arn:aws:kinesis:us-west-2:123456789012:stream/MyStream1","RoleArn"="arn:aws:iam::123456789012:role/ MyRoleToAccessLambda" "Id"="Target2"," Arn"="arn:aws:kinesis:us-west-2:123456789012:stream/MyStream2","RoleArn"="arn:aws:iam::123456789012:role/MyRoleToAccessLambda"Output:
{ "FailedEntryCount": 0, "FailedEntries": [] }For more information, see Event bus targets in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see PutTargets
in AWS CLI Command Reference.
-
The following code example shows how to use remove-targets.
- AWS CLI
-
To remove a target for an event
The following
remove-targetsexample removes the Amazon Kinesis stream namedMyStream1from being a target of the ruleDailyLambdaFunction. WhenDailyLambdaFunctionwas created, this stream was set as a target with an ID ofTarget1.aws events remove-targets \ --rule"DailyLambdaFunction"\ --ids"Target1"Output:
{ "FailedEntryCount": 0, "FailedEntries": [] }For more information, see Event bus targets in Amazon EventBridge
in the Amazon EventBridge User Guide. -
For API details, see RemoveTargets
in AWS CLI Command Reference.
-
The following code example shows how to use test-event-pattern.
- AWS CLI
-
To check whether an event pattern matches a specified event
The following
test-event-patternexample tests whether the patternsource:com.mycompany.myappmatches the specified event. In this example, the output would betrue.aws events test-event-pattern \ --event-pattern "{\"source\":[\"com.mycompany.myapp\"]}" \ --event "{\"id\":\"1\",\"source\":\"com.mycompany.myapp\",\"detail-type\":\"myDetailType\",\"account\":\"123456789012\",\"region\":\"us-west-2\",\"time\":\"2017-04-11T20:11:04Z\"}"Output:
{ "Result": true }For more information, see Amazon EventBridge event patterns
in the Amazon EventBridge User Guide. -
For API details, see TestEventPattern
in AWS CLI Command Reference.
-