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 EnableRule
with an Amazon SDK or CLI
The following code examples show how to use EnableRule
.
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
-
Enable a rule by its rule name.
/// <summary>
/// Enable a particular rule on an event bus.
/// </summary>
/// <param name="ruleName">The name of the rule.</param>
/// <returns>True if successful.</returns>
public async Task<bool> EnableRuleByName(string ruleName)
{
var ruleResponse = await _amazonEventBridge.EnableRuleAsync(
new EnableRuleRequest()
{
Name = ruleName
});
return ruleResponse.HttpStatusCode == HttpStatusCode.OK;
}
- CLI
-
- Amazon CLI
-
To enable a CloudWatch Events rule
This example enables the rule named DailyLambdaFunction, which had been previously disabled:
aws events enable-rule --name "DailyLambdaFunction"
- Java
-
- SDK for Java 2.x
-
Enable a rule by using its rule name.
public static void changeRuleState(EventBridgeClient eventBrClient, String eventRuleName, Boolean isEnabled) {
try {
if (!isEnabled) {
System.out.println("Disabling the rule: " + eventRuleName);
DisableRuleRequest ruleRequest = DisableRuleRequest.builder()
.name(eventRuleName)
.build();
eventBrClient.disableRule(ruleRequest);
} else {
System.out.println("Enabling the rule: " + eventRuleName);
EnableRuleRequest ruleRequest = EnableRuleRequest.builder()
.name(eventRuleName)
.build();
eventBrClient.enableRule(ruleRequest);
}
} catch (EventBridgeException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
- Kotlin
-
- SDK for Kotlin
-
suspend fun changeRuleState(
eventRuleName: String,
isEnabled: Boolean?,
) {
if (!isEnabled!!) {
println("Disabling the rule: $eventRuleName")
val ruleRequest =
DisableRuleRequest {
name = eventRuleName
}
EventBridgeClient { region = "us-east-1" }.use { eventBrClient ->
eventBrClient.disableRule(ruleRequest)
}
} else {
println("Enabling the rule: $eventRuleName")
val ruleRequest =
EnableRuleRequest {
name = eventRuleName
}
EventBridgeClient { region = "us-east-1" }.use { eventBrClient ->
eventBrClient.enableRule(ruleRequest)
}
}
}
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.