Use ListTargetsByRule with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use ListTargetsByRule with an AWS SDK or CLI

The following code examples show how to use ListTargetsByRule.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

.NET
AWS SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List all of the targets for a rule using the rule name.

/// <summary> /// List all of the targets matching a rule by name. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <returns>The list of targets.</returns> public async Task<List<Target>> ListAllTargetsOnRule(string ruleName) { var results = new List<Target>(); var request = new ListTargetsByRuleRequest() { Rule = ruleName }; ListTargetsByRuleResponse response; do { response = await _amazonEventBridge.ListTargetsByRuleAsync(request); results.AddRange(response.Targets); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; }
CLI
AWS CLI

To display all the targets for a CloudWatch Events rule

This example displays all the targets of the rule named DailyLambdaFunction:

aws events list-targets-by-rule --rule "DailyLambdaFunction"
Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List all of the targets for a rule by using the rule name.

public static void listTargets(EventBridgeClient eventBrClient, String ruleName) { ListTargetsByRuleRequest ruleRequest = ListTargetsByRuleRequest.builder() .rule(ruleName) .build(); ListTargetsByRuleResponse res = eventBrClient.listTargetsByRule(ruleRequest); List<Target> targetsList = res.targets(); for (Target target: targetsList) { System.out.println("Target ARN: "+target.arn()); } }
Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun listTargets(ruleName: String?) { val ruleRequest = ListTargetsByRuleRequest { rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(ruleRequest) response.targets?.forEach { target -> println("Target ARN: ${target.arn}") } } }