Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Use ListTargetsByRule
with an Amazon 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
-
- Amazon SDK for .NET
-
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
-
- Amazon 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
-
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
-
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}")
}
}
}
For a complete list of Amazon SDK developer guides and code examples, see
Using EventBridge with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.