Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
EventBridge ejemplos que utilizan AWS SDK for .NET
Los siguientes ejemplos de código muestran cómo realizar acciones e implementar escenarios comunes mediante el uso del AWS SDK for .NET with EventBridge.
Los conceptos básicos son ejemplos de código que muestran cómo realizar las operaciones esenciales dentro de un servicio.
Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las funciones de servicio individuales, es posible ver las acciones en contexto en los escenarios relacionados.
Cada ejemplo incluye un enlace al código fuente completo, donde puede encontrar instrucciones sobre cómo configurar y ejecutar el código en su contexto.
Introducción
Los siguientes ejemplos de código muestran cómo empezar a usarlo EventBridge.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. using Amazon.EventBridge; using Amazon.EventBridge.Model; namespace EventBridgeActions; public static class HelloEventBridge { static async Task Main(string[] args) { var eventBridgeClient = new AmazonEventBridgeClient(); Console.WriteLine($"Hello Amazon EventBridge! Following are some of your EventBuses:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get the first five event buses. var response = await eventBridgeClient.ListEventBusesAsync( new ListEventBusesRequest() { Limit = 5 }); foreach (var eventBus in response.EventBuses) { Console.WriteLine($"\tEventBus: {eventBus.Name}"); Console.WriteLine($"\tArn: {eventBus.Arn}"); Console.WriteLine($"\tPolicy: {eventBus.Policy}"); Console.WriteLine(); } } }
-
Para API obtener más información, consulte ListEventBusesla AWS SDK for .NET APIReferencia.
-
Conceptos básicos
En el siguiente ejemplo de código, se muestra cómo:
Crear una regla y agregarle un destino
Habilitar y deshabilitar reglas.
Enumerar y actualizar reglas y destinos.
Enviar eventos y, después, limpiar los recursos
- AWS SDK for .NET
-
nota
Hay más información sobre GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Ejecutar un escenario interactivo en un símbolo del sistema.
public class EventBridgeScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks with Amazon EventBridge: - Create a rule. - Add a target to a rule. - Enable and disable rules. - List rules and targets. - Update rules and targets. - Send events. - Delete the rule. */ private static ILogger logger = null!; private static EventBridgeWrapper _eventBridgeWrapper = null!; private static IConfiguration _configuration = null!; private static IAmazonIdentityManagementService? _iamClient = null!; private static IAmazonSimpleNotificationService? _snsClient = null!; private static IAmazonS3 _s3Client = null!; static async Task Main(string[] args) { // Set up dependency injection for Amazon EventBridge. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonEventBridge>() .AddAWSService<IAmazonIdentityManagementService>() .AddAWSService<IAmazonS3>() .AddAWSService<IAmazonSimpleNotificationService>() .AddTransient<EventBridgeWrapper>() ) .Build(); _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally, load local settings. .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<EventBridgeScenario>(); ServicesSetup(host); string topicArn = ""; string roleArn = ""; Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the Amazon EventBridge example scenario."); Console.WriteLine(new string('-', 80)); try { roleArn = await CreateRole(); await CreateBucketWithEventBridgeEvents(); await AddEventRule(roleArn); await ListEventRules(); topicArn = await CreateSnsTopic(); var email = await SubscribeToSnsTopic(topicArn); await AddSnsTarget(topicArn); await ListTargets(); await ListRulesForTarget(topicArn); await UploadS3File(_s3Client); await ChangeRuleState(false); await GetRuleState(); await UpdateSnsEventRule(topicArn); await ChangeRuleState(true); await UploadS3File(_s3Client); await UpdateToCustomRule(topicArn); await TriggerCustomRule(email); await CleanupResources(topicArn); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); await CleanupResources(topicArn); } Console.WriteLine(new string('-', 80)); Console.WriteLine("The Amazon EventBridge example scenario is complete."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Populate the services for use within the console application. /// </summary> /// <param name="host">The services host.</param> private static void ServicesSetup(IHost host) { _eventBridgeWrapper = host.Services.GetRequiredService<EventBridgeWrapper>(); _snsClient = host.Services.GetRequiredService<IAmazonSimpleNotificationService>(); _s3Client = host.Services.GetRequiredService<IAmazonS3>(); _iamClient = host.Services.GetRequiredService<IAmazonIdentityManagementService>(); } /// <summary> /// Create a role to be used by EventBridge. /// </summary> /// <returns>The role Amazon Resource Name (ARN).</returns> public static async Task<string> CreateRole() { Console.WriteLine(new string('-', 80)); Console.WriteLine("Creating a role to use with EventBridge and attaching managed policy AmazonEventBridgeFullAccess."); Console.WriteLine(new string('-', 80)); var roleName = _configuration["roleName"]; var assumeRolePolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Effect\": \"Allow\"," + "\"Principal\": {" + $"\"Service\": \"events.amazonaws.com\"" + "}," + "\"Action\": \"sts:AssumeRole\"" + "}]" + "}"; var roleResult = await _iamClient!.CreateRoleAsync( new CreateRoleRequest() { AssumeRolePolicyDocument = assumeRolePolicy, Path = "/", RoleName = roleName }); await _iamClient.AttachRolePolicyAsync( new AttachRolePolicyRequest() { PolicyArn = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess", RoleName = roleName }); // Allow time for the role to be ready. Thread.Sleep(10000); return roleResult.Role.Arn; } /// <summary> /// Create an Amazon Simple Storage Service (Amazon S3) bucket with EventBridge events enabled. /// </summary> /// <returns>Async task.</returns> private static async Task CreateBucketWithEventBridgeEvents() { Console.WriteLine(new string('-', 80)); Console.WriteLine("Creating an S3 bucket with EventBridge events enabled."); var testBucketName = _configuration["testBucketName"]; var bucketExists = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(_s3Client, testBucketName); if (!bucketExists) { await _s3Client.PutBucketAsync(new PutBucketRequest() { BucketName = testBucketName, UseClientRegion = true }); } await _s3Client.PutBucketNotificationAsync(new PutBucketNotificationRequest() { BucketName = testBucketName, EventBridgeConfiguration = new EventBridgeConfiguration() }); Console.WriteLine($"\tAdded bucket {testBucketName} with EventBridge events enabled."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Create and upload a file to an S3 bucket to trigger an event. /// </summary> /// <returns>Async task.</returns> private static async Task UploadS3File(IAmazonS3 s3Client) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Uploading a file to the test bucket. This will trigger a subscription email."); var testBucketName = _configuration["testBucketName"]; var fileName = $"example_upload_{DateTime.UtcNow.Ticks}.txt"; // Create the file if it does not already exist. if (!File.Exists(fileName)) { await using StreamWriter sw = File.CreateText(fileName); await sw.WriteLineAsync( "This is a sample file for testing uploads."); } await s3Client.PutObjectAsync(new PutObjectRequest() { FilePath = fileName, BucketName = testBucketName }); Console.WriteLine($"\tPress Enter to continue."); Console.ReadLine(); Console.WriteLine(new string('-', 80)); } /// <summary> /// Create an Amazon Simple Notification Service (Amazon SNS) topic to use as an EventBridge target. /// </summary> /// <returns>Async task.</returns> private static async Task<string> CreateSnsTopic() { Console.WriteLine(new string('-', 80)); Console.WriteLine( "Creating an Amazon Simple Notification Service (Amazon SNS) topic for email subscriptions."); var topicName = _configuration["topicName"]; string topicPolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Sid\": \"EventBridgePublishTopic\"," + "\"Effect\": \"Allow\"," + "\"Principal\": {" + $"\"Service\": \"events.amazonaws.com\"" + "}," + "\"Resource\": \"*\"," + "\"Action\": \"sns:Publish\"" + "}]" + "}"; var topicAttributes = new Dictionary<string, string>() { { "Policy", topicPolicy } }; var topicResponse = await _snsClient!.CreateTopicAsync(new CreateTopicRequest() { Name = topicName, Attributes = topicAttributes }); Console.WriteLine($"\tAdded topic {topicName} for email subscriptions."); Console.WriteLine(new string('-', 80)); return topicResponse.TopicArn; } /// <summary> /// Subscribe a user email to an SNS topic. /// </summary> /// <param name="topicArn">The ARN of the SNS topic.</param> /// <returns>The user's email.</returns> private static async Task<string> SubscribeToSnsTopic(string topicArn) { Console.WriteLine(new string('-', 80)); string email = ""; while (string.IsNullOrEmpty(email)) { Console.WriteLine("Enter your email to subscribe to the Amazon SNS topic:"); email = Console.ReadLine()!; } var subscriptions = new List<string>(); var paginatedSubscriptions = _snsClient!.Paginators.ListSubscriptionsByTopic( new ListSubscriptionsByTopicRequest() { TopicArn = topicArn }); // Get the entire list using the paginator. await foreach (var subscription in paginatedSubscriptions.Subscriptions) { subscriptions.Add(subscription.Endpoint); } if (subscriptions.Contains(email)) { Console.WriteLine($"\tYour email is already subscribed."); Console.WriteLine(new string('-', 80)); return email; } await _snsClient.SubscribeAsync(new SubscribeRequest() { TopicArn = topicArn, Protocol = "email", Endpoint = email }); Console.WriteLine($"Use the link in the email you received to confirm your subscription, then press Enter to continue."); Console.ReadLine(); Console.WriteLine(new string('-', 80)); return email; } /// <summary> /// Add a rule which triggers when a file is uploaded to an S3 bucket. /// </summary> /// <param name="roleArn">The ARN of the role used by EventBridge.</param> /// <returns>Async task.</returns> private static async Task AddEventRule(string roleArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Creating an EventBridge event that sends an email when an Amazon S3 object is created."); var eventRuleName = _configuration["eventRuleName"]; var testBucketName = _configuration["testBucketName"]; await _eventBridgeWrapper.PutS3UploadRule(roleArn, eventRuleName, testBucketName); Console.WriteLine($"\tAdded event rule {eventRuleName} for bucket {testBucketName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Add an SNS target to the rule. /// </summary> /// <param name="topicArn">The ARN of the SNS topic.</param> /// <returns>Async task.</returns> private static async Task AddSnsTarget(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Adding a target to the rule to that sends an email when the rule is triggered."); var eventRuleName = _configuration["eventRuleName"]; var testBucketName = _configuration["testBucketName"]; var topicName = _configuration["topicName"]; await _eventBridgeWrapper.AddSnsTargetToRule(eventRuleName, topicArn); Console.WriteLine($"\tAdded event rule {eventRuleName} with Amazon SNS target {topicName} for bucket {testBucketName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// List the event rules on the default event bus. /// </summary> /// <returns>Async task.</returns> private static async Task ListEventRules() { Console.WriteLine(new string('-', 80)); Console.WriteLine("Current event rules:"); var rules = await _eventBridgeWrapper.ListAllRulesForEventBus(); rules.ForEach(r => Console.WriteLine($"\tRule: {r.Name} Description: {r.Description} State: {r.State}")); Console.WriteLine(new string('-', 80)); } /// <summary> /// Update the event target to use a transform. /// </summary> /// <param name="topicArn">The SNS topic ARN target to update.</param> /// <returns>Async task.</returns> private static async Task UpdateSnsEventRule(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Let's update the event target with a transform."); var eventRuleName = _configuration["eventRuleName"]; var testBucketName = _configuration["testBucketName"]; await _eventBridgeWrapper.UpdateS3UploadRuleTargetWithTransform(eventRuleName, topicArn); Console.WriteLine($"\tUpdated event rule {eventRuleName} with Amazon SNS target {topicArn} for bucket {testBucketName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Update the rule to use a custom event pattern. /// </summary> /// <returns>Async task.</returns> private static async Task UpdateToCustomRule(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Updating the event pattern to be triggered by a custom event instead."); var eventRuleName = _configuration["eventRuleName"]; await _eventBridgeWrapper.UpdateCustomEventPattern(eventRuleName); Console.WriteLine($"\tUpdated event rule {eventRuleName} to custom pattern."); await _eventBridgeWrapper.UpdateCustomRuleTargetWithTransform(eventRuleName, topicArn); Console.WriteLine($"\tUpdated event target {topicArn}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Send rule events for a custom rule using the user's email address. /// </summary> /// <param name="email">The email address to include.</param> /// <returns>Async task.</returns> private static async Task TriggerCustomRule(string email) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Sending an event to trigger the rule. This will trigger a subscription email."); await _eventBridgeWrapper.PutCustomEmailEvent(email); Console.WriteLine($"\tEvents have been sent. Press Enter to continue."); Console.ReadLine(); Console.WriteLine(new string('-', 80)); } /// <summary> /// List all of the targets for a rule. /// </summary> /// <returns>Async task.</returns> private static async Task ListTargets() { Console.WriteLine(new string('-', 80)); Console.WriteLine("List all of the targets for a particular rule."); var eventRuleName = _configuration["eventRuleName"]; var targets = await _eventBridgeWrapper.ListAllTargetsOnRule(eventRuleName); targets.ForEach(t => Console.WriteLine($"\tTarget: {t.Arn} Id: {t.Id} Input: {t.Input}")); Console.WriteLine(new string('-', 80)); } /// <summary> /// List all of the rules for a particular target. /// </summary> /// <param name="topicArn">The ARN of the SNS topic.</param> /// <returns>Async task.</returns> private static async Task ListRulesForTarget(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("List all of the rules for a particular target."); var rules = await _eventBridgeWrapper.ListAllRuleNamesByTarget(topicArn); rules.ForEach(r => Console.WriteLine($"\tRule: {r}")); Console.WriteLine(new string('-', 80)); } /// <summary> /// Enable or disable a particular rule. /// </summary> /// <param name="isEnabled">True to enable the rule, otherwise false.</param> /// <returns>Async task.</returns> private static async Task ChangeRuleState(bool isEnabled) { Console.WriteLine(new string('-', 80)); var eventRuleName = _configuration["eventRuleName"]; if (!isEnabled) { Console.WriteLine($"Disabling the rule: {eventRuleName}"); await _eventBridgeWrapper.DisableRuleByName(eventRuleName); } else { Console.WriteLine($"Enabling the rule: {eventRuleName}"); await _eventBridgeWrapper.EnableRuleByName(eventRuleName); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Get the current state of the rule. /// </summary> /// <returns>Async task.</returns> private static async Task GetRuleState() { Console.WriteLine(new string('-', 80)); var eventRuleName = _configuration["eventRuleName"]; var state = await _eventBridgeWrapper.GetRuleStateByRuleName(eventRuleName); Console.WriteLine($"Rule {eventRuleName} is in current state {state}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Clean up the resources from the scenario. /// </summary> /// <param name="topicArn">The ARN of the SNS topic to clean up.</param> /// <returns>Async task.</returns> private static async Task CleanupResources(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"Clean up resources."); var eventRuleName = _configuration["eventRuleName"]; if (GetYesNoResponse($"\tDelete all targets and event rule {eventRuleName}? (y/n)")) { Console.WriteLine($"\tRemoving all targets from the event rule."); await _eventBridgeWrapper.RemoveAllTargetsFromRule(eventRuleName); Console.WriteLine($"\tDeleting event rule."); await _eventBridgeWrapper.DeleteRuleByName(eventRuleName); } var topicName = _configuration["topicName"]; if (GetYesNoResponse($"\tDelete Amazon SNS subscription topic {topicName}? (y/n)")) { Console.WriteLine($"\tDeleting topic."); await _snsClient!.DeleteTopicAsync(new DeleteTopicRequest() { TopicArn = topicArn }); } var bucketName = _configuration["testBucketName"]; if (GetYesNoResponse($"\tDelete Amazon S3 bucket {bucketName}? (y/n)")) { Console.WriteLine($"\tDeleting bucket."); // Delete all objects in the bucket. var deleteList = await _s3Client.ListObjectsV2Async(new ListObjectsV2Request() { BucketName = bucketName }); await _s3Client.DeleteObjectsAsync(new DeleteObjectsRequest() { BucketName = bucketName, Objects = deleteList.S3Objects .Select(o => new KeyVersion { Key = o.Key }).ToList() }); // Now delete the bucket. await _s3Client.DeleteBucketAsync(new DeleteBucketRequest() { BucketName = bucketName }); } var roleName = _configuration["roleName"]; if (GetYesNoResponse($"\tDelete role {roleName}? (y/n)")) { Console.WriteLine($"\tDetaching policy and deleting role."); await _iamClient!.DetachRolePolicyAsync(new DetachRolePolicyRequest() { RoleName = roleName, PolicyArn = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess", }); await _iamClient!.DeleteRoleAsync(new DeleteRoleRequest() { RoleName = roleName }); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Helper method to get a yes or no response from the user. /// </summary> /// <param name="question">The question string to print on the console.</param> /// <returns>True if the user responds with a yes.</returns> private static bool GetYesNoResponse(string question) { Console.WriteLine(question); var ynResponse = Console.ReadLine(); var response = ynResponse != null && ynResponse.Equals("y", StringComparison.InvariantCultureIgnoreCase); return response; } }
Crea una clase que abarque EventBridge las operaciones.
/// <summary> /// Wrapper for Amazon EventBridge operations. /// </summary> public class EventBridgeWrapper { private readonly IAmazonEventBridge _amazonEventBridge; private readonly ILogger<EventBridgeWrapper> _logger; /// <summary> /// Constructor for the EventBridge wrapper. /// </summary> /// <param name="amazonEventBridge">The injected EventBridge client.</param> /// <param name="logger">The injected logger for the wrapper.</param> public EventBridgeWrapper(IAmazonEventBridge amazonEventBridge, ILogger<EventBridgeWrapper> logger) { _amazonEventBridge = amazonEventBridge; _logger = logger; } /// <summary> /// Get the state for a rule by the rule name. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="eventBusName">The optional name of the event bus. If empty, uses the default event bus.</param> /// <returns>The state of the rule.</returns> public async Task<RuleState> GetRuleStateByRuleName(string ruleName, string? eventBusName = null) { var ruleResponse = await _amazonEventBridge.DescribeRuleAsync( new DescribeRuleRequest() { Name = ruleName, EventBusName = eventBusName }); return ruleResponse.State; } /// <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; } /// <summary> /// Disable 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> DisableRuleByName(string ruleName) { var ruleResponse = await _amazonEventBridge.DisableRuleAsync( new DisableRuleRequest() { Name = ruleName }); return ruleResponse.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// List the rules on an event bus. /// </summary> /// <param name="eventBusArn">The optional ARN of the event bus. If empty, uses the default event bus.</param> /// <returns>The list of rules.</returns> public async Task<List<Rule>> ListAllRulesForEventBus(string? eventBusArn = null) { var results = new List<Rule>(); var request = new ListRulesRequest() { EventBusName = eventBusArn }; // Get all of the pages of rules. ListRulesResponse response; do { response = await _amazonEventBridge.ListRulesAsync(request); results.AddRange(response.Rules); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; } /// <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; } /// <summary> /// List names of all rules matching a target. /// </summary> /// <param name="targetArn">The ARN of the target.</param> /// <returns>The list of rule names.</returns> public async Task<List<string>> ListAllRuleNamesByTarget(string targetArn) { var results = new List<string>(); var request = new ListRuleNamesByTargetRequest() { TargetArn = targetArn }; ListRuleNamesByTargetResponse response; do { response = await _amazonEventBridge.ListRuleNamesByTargetAsync(request); results.AddRange(response.RuleNames); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; } /// <summary> /// Create a new event rule that triggers when an Amazon S3 object is created in a bucket. /// </summary> /// <param name="roleArn">The ARN of the role.</param> /// <param name="ruleName">The name to give the rule.</param> /// <param name="bucketName">The name of the bucket to trigger the event.</param> /// <returns>The ARN of the new rule.</returns> public async Task<string> PutS3UploadRule(string roleArn, string ruleName, string bucketName) { string eventPattern = "{" + "\"source\": [\"aws.s3\"]," + "\"detail-type\": [\"Object Created\"]," + "\"detail\": {" + "\"bucket\": {" + "\"name\": [\"" + bucketName + "\"]" + "}" + "}" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Example S3 upload rule for EventBridge", RoleArn = roleArn, EventPattern = eventPattern }); return response.RuleArn; } /// <summary> /// Update an Amazon S3 object created rule with a transform on the target. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="targetArn">The ARN of the target.</param> /// <param name="eventBusArn">Optional event bus ARN. If empty, uses the default event bus.</param> /// <returns>The ID of the target.</returns> public async Task<string> UpdateS3UploadRuleTargetWithTransform(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); var targets = new List<Target> { new Target() { Id = targetID, Arn = targetArn, InputTransformer = new InputTransformer() { InputPathsMap = new Dictionary<string, string>() { {"bucket", "$.detail.bucket.name"}, {"time", "$.time"} }, InputTemplate = "\"Notification: an object was uploaded to bucket <bucket> at <time>.\"" } } }; var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; } /// <summary> /// Update a custom rule with a transform on the target. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="targetArn">The ARN of the target.</param> /// <param name="eventBusArn">Optional event bus ARN. If empty, uses the default event bus.</param> /// <returns>The ID of the target.</returns> public async Task<string> UpdateCustomRuleTargetWithTransform(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); var targets = new List<Target> { new Target() { Id = targetID, Arn = targetArn, InputTransformer = new InputTransformer() { InputTemplate = "\"Notification: sample event was received.\"" } } }; var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; } /// <summary> /// Add an event to the event bus that includes an email, message, and time. /// </summary> /// <param name="email">The email to use in the event detail of the custom event.</param> /// <returns>True if successful.</returns> public async Task<bool> PutCustomEmailEvent(string email) { var eventDetail = new { UserEmail = email, Message = "This event was generated by example code.", UtcTime = DateTime.UtcNow.ToString("g") }; var response = await _amazonEventBridge.PutEventsAsync( new PutEventsRequest() { Entries = new List<PutEventsRequestEntry>() { new PutEventsRequestEntry() { Source = "ExampleSource", Detail = JsonSerializer.Serialize(eventDetail), DetailType = "ExampleType" } } }); return response.FailedEntryCount == 0; } /// <summary> /// Update a rule to use a custom defined event pattern. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <returns>The ARN of the updated rule.</returns> public async Task<string> UpdateCustomEventPattern(string ruleName) { string customEventsPattern = "{" + "\"source\": [\"ExampleSource\"]," + "\"detail-type\": [\"ExampleType\"]" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Custom test rule", EventPattern = customEventsPattern }); return response.RuleArn; } /// <summary> /// Add an Amazon SNS target topic to a rule. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <param name="targetArn">The ARN of the Amazon SNS target.</param> /// <param name="eventBusArn">The optional event bus name, uses default if empty.</param> /// <returns>The ID of the target.</returns> public async Task<string> AddSnsTargetToRule(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); // Create the list of targets and add a new target. var targets = new List<Target> { new Target() { Arn = targetArn, Id = targetID } }; // Add the targets to the rule. var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; } /// <summary> /// Delete an event rule by name. /// </summary> /// <param name="ruleName">The name of the event rule.</param> /// <returns>True if successful.</returns> public async Task<bool> RemoveAllTargetsFromRule(string ruleName) { var targetIds = new List<string>(); var request = new ListTargetsByRuleRequest() { Rule = ruleName }; ListTargetsByRuleResponse targetsResponse; do { targetsResponse = await _amazonEventBridge.ListTargetsByRuleAsync(request); targetIds.AddRange(targetsResponse.Targets.Select(t => t.Id)); request.NextToken = targetsResponse.NextToken; } while (targetsResponse.NextToken is not null); var removeResponse = await _amazonEventBridge.RemoveTargetsAsync( new RemoveTargetsRequest() { Rule = ruleName, Ids = targetIds }); if (removeResponse.FailedEntryCount > 0) { removeResponse.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to remove target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return removeResponse.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Delete an event rule by name. /// </summary> /// <param name="ruleName">The name of the event rule.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteRuleByName(string ruleName) { var response = await _amazonEventBridge.DeleteRuleAsync( new DeleteRuleRequest() { Name = ruleName }); return response.HttpStatusCode == HttpStatusCode.OK; } }
-
Para API obtener más información, consulte los siguientes temas en AWS SDK for .NET APIReference.
-
Acciones
En el siguiente ejemplo de código se muestra cómo usarloDeleteRule
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Eliminar una regla por su nombre.
/// <summary> /// Delete an event rule by name. /// </summary> /// <param name="ruleName">The name of the event rule.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteRuleByName(string ruleName) { var response = await _amazonEventBridge.DeleteRuleAsync( new DeleteRuleRequest() { Name = ruleName }); return response.HttpStatusCode == HttpStatusCode.OK; }
-
Para API obtener más información, consulte DeleteRulela AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloDescribeRule
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Obtener el estado de una regla mediante la descripción de la regla.
/// <summary> /// Get the state for a rule by the rule name. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="eventBusName">The optional name of the event bus. If empty, uses the default event bus.</param> /// <returns>The state of the rule.</returns> public async Task<RuleState> GetRuleStateByRuleName(string ruleName, string? eventBusName = null) { var ruleResponse = await _amazonEventBridge.DescribeRuleAsync( new DescribeRuleRequest() { Name = ruleName, EventBusName = eventBusName }); return ruleResponse.State; }
-
Para API obtener más información, consulte DescribeRulela AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloDisableRule
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Deshabilitar una regla por su nombre de regla.
/// <summary> /// Disable 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> DisableRuleByName(string ruleName) { var ruleResponse = await _amazonEventBridge.DisableRuleAsync( new DisableRuleRequest() { Name = ruleName }); return ruleResponse.HttpStatusCode == HttpStatusCode.OK; }
-
Para API obtener más información, consulte DisableRulela AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloEnableRule
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Habilitar una regla por su nombre de regla.
/// <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; }
-
Para API obtener más información, consulte EnableRulela AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloListRuleNamesByTarget
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Enumerar todos los nombres de regla por el destino.
/// <summary> /// List names of all rules matching a target. /// </summary> /// <param name="targetArn">The ARN of the target.</param> /// <returns>The list of rule names.</returns> public async Task<List<string>> ListAllRuleNamesByTarget(string targetArn) { var results = new List<string>(); var request = new ListRuleNamesByTargetRequest() { TargetArn = targetArn }; ListRuleNamesByTargetResponse response; do { response = await _amazonEventBridge.ListRuleNamesByTargetAsync(request); results.AddRange(response.RuleNames); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; }
-
Para API obtener más información, consulte ListRuleNamesByTargetla AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloListRules
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Enumerar todas las reglas de un bus de eventos.
/// <summary> /// List the rules on an event bus. /// </summary> /// <param name="eventBusArn">The optional ARN of the event bus. If empty, uses the default event bus.</param> /// <returns>The list of rules.</returns> public async Task<List<Rule>> ListAllRulesForEventBus(string? eventBusArn = null) { var results = new List<Rule>(); var request = new ListRulesRequest() { EventBusName = eventBusArn }; // Get all of the pages of rules. ListRulesResponse response; do { response = await _amazonEventBridge.ListRulesAsync(request); results.AddRange(response.Rules); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; }
-
Para API obtener más información, consulte ListRulesla AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloListTargetsByRule
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Enumerar todos los destinos para una regla por el nombre de regla.
/// <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; }
-
Para API obtener más información, consulte ListTargetsByRulela AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloPutEvents
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Enviar un evento que coincida con el patrón personalizado de una regla.
/// <summary> /// Add an event to the event bus that includes an email, message, and time. /// </summary> /// <param name="email">The email to use in the event detail of the custom event.</param> /// <returns>True if successful.</returns> public async Task<bool> PutCustomEmailEvent(string email) { var eventDetail = new { UserEmail = email, Message = "This event was generated by example code.", UtcTime = DateTime.UtcNow.ToString("g") }; var response = await _amazonEventBridge.PutEventsAsync( new PutEventsRequest() { Entries = new List<PutEventsRequestEntry>() { new PutEventsRequestEntry() { Source = "ExampleSource", Detail = JsonSerializer.Serialize(eventDetail), DetailType = "ExampleType" } } }); return response.FailedEntryCount == 0; }
-
Para API obtener más información, consulte PutEventsla AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloPutRule
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Crear una regla que se active cuando se agregue un objeto a un bucket de Amazon Simple Storage Service.
/// <summary> /// Create a new event rule that triggers when an Amazon S3 object is created in a bucket. /// </summary> /// <param name="roleArn">The ARN of the role.</param> /// <param name="ruleName">The name to give the rule.</param> /// <param name="bucketName">The name of the bucket to trigger the event.</param> /// <returns>The ARN of the new rule.</returns> public async Task<string> PutS3UploadRule(string roleArn, string ruleName, string bucketName) { string eventPattern = "{" + "\"source\": [\"aws.s3\"]," + "\"detail-type\": [\"Object Created\"]," + "\"detail\": {" + "\"bucket\": {" + "\"name\": [\"" + bucketName + "\"]" + "}" + "}" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Example S3 upload rule for EventBridge", RoleArn = roleArn, EventPattern = eventPattern }); return response.RuleArn; }
Crear una regla que use un patrón personalizado.
/// <summary> /// Update a rule to use a custom defined event pattern. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <returns>The ARN of the updated rule.</returns> public async Task<string> UpdateCustomEventPattern(string ruleName) { string customEventsPattern = "{" + "\"source\": [\"ExampleSource\"]," + "\"detail-type\": [\"ExampleType\"]" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Custom test rule", EventPattern = customEventsPattern }); return response.RuleArn; }
-
Para API obtener más información, consulte PutRulela AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloPutTargets
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Añade un SNS tema de Amazon como objetivo de una regla.
/// <summary> /// Add an Amazon SNS target topic to a rule. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <param name="targetArn">The ARN of the Amazon SNS target.</param> /// <param name="eventBusArn">The optional event bus name, uses default if empty.</param> /// <returns>The ID of the target.</returns> public async Task<string> AddSnsTargetToRule(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); // Create the list of targets and add a new target. var targets = new List<Target> { new Target() { Arn = targetArn, Id = targetID } }; // Add the targets to the rule. var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; }
Agregue un transformador de entrada al destino de una regla.
/// <summary> /// Update an Amazon S3 object created rule with a transform on the target. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="targetArn">The ARN of the target.</param> /// <param name="eventBusArn">Optional event bus ARN. If empty, uses the default event bus.</param> /// <returns>The ID of the target.</returns> public async Task<string> UpdateS3UploadRuleTargetWithTransform(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); var targets = new List<Target> { new Target() { Id = targetID, Arn = targetArn, InputTransformer = new InputTransformer() { InputPathsMap = new Dictionary<string, string>() { {"bucket", "$.detail.bucket.name"}, {"time", "$.time"} }, InputTemplate = "\"Notification: an object was uploaded to bucket <bucket> at <time>.\"" } } }; var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; }
-
Para API obtener más información, consulta PutTargetsla AWS SDK for .NET APIReferencia.
-
En el siguiente ejemplo de código se muestra cómo usarloRemoveTargets
.
- AWS SDK for .NET
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Eliminar todos los destinos de una regla por el nombre de regla.
/// <summary> /// Delete an event rule by name. /// </summary> /// <param name="ruleName">The name of the event rule.</param> /// <returns>True if successful.</returns> public async Task<bool> RemoveAllTargetsFromRule(string ruleName) { var targetIds = new List<string>(); var request = new ListTargetsByRuleRequest() { Rule = ruleName }; ListTargetsByRuleResponse targetsResponse; do { targetsResponse = await _amazonEventBridge.ListTargetsByRuleAsync(request); targetIds.AddRange(targetsResponse.Targets.Select(t => t.Id)); request.NextToken = targetsResponse.NextToken; } while (targetsResponse.NextToken is not null); var removeResponse = await _amazonEventBridge.RemoveTargetsAsync( new RemoveTargetsRequest() { Rule = ruleName, Ids = targetIds }); if (removeResponse.FailedEntryCount > 0) { removeResponse.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to remove target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return removeResponse.HttpStatusCode == HttpStatusCode.OK; }
-
Para API obtener más información, consulte RemoveTargetsla AWS SDK for .NET APIReferencia.
-