From 722d8f38570d59e6b1cf571982a69ad97ae4d24e Mon Sep 17 00:00:00 2001 From: Rachel Hagerman <110480692+rlhagerm@users.noreply.github.com> Date: Wed, 5 Apr 2023 13:54:44 -0500 Subject: [PATCH] EventBridge .NET MVP Updates (#4643) * Updating order of the scenario. * Fix for file references. * Incorporating feedback and removing old code --- .../EventBridge/Actions/HelloEventBridge.cs | 14 +----- .../Scenarios/EventBridgeScenario.cs | 50 +++++++++++++------ .../Scenarios/EventBridgeScenario.csproj | 8 +-- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/dotnetv3/EventBridge/Actions/HelloEventBridge.cs b/dotnetv3/EventBridge/Actions/HelloEventBridge.cs index 162d8264520..1f6188289be 100644 --- a/dotnetv3/EventBridge/Actions/HelloEventBridge.cs +++ b/dotnetv3/EventBridge/Actions/HelloEventBridge.cs @@ -2,11 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 // snippet-start:[EventBridge.dotnetv3.HelloEventBridge] -using Microsoft.Extensions.Hosting; + using Amazon.EventBridge; using Amazon.EventBridge.Model; -using Amazon.Extensions.NETCore.Setup; -using Microsoft.Extensions.DependencyInjection; namespace EventBridgeActions; @@ -14,15 +12,7 @@ public static class HelloEventBridge { static async Task Main(string[] args) { - // Use the AWS .NET Core Setup package to set up dependency injection for the Amazon EventBridge service. - // Use your AWS profile name, or leave it blank to use the default profile. - using var host = Host.CreateDefaultBuilder(args) - .ConfigureServices((_, services) => - services.AddAWSService(new AWSOptions() { Profile = "default" }) - ).Build(); - - // Now the client is available for injection. - var eventBridgeClient = host.Services.GetRequiredService(); + var eventBridgeClient = new AmazonEventBridgeClient(); Console.WriteLine($"Hello Amazon EventBridge! Following are some of your EventBuses:"); Console.WriteLine(); diff --git a/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.cs b/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.cs index 3e9a5a37f00..45e0ceea86c 100644 --- a/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.cs +++ b/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.cs @@ -44,7 +44,7 @@ public class EventBridgeScenario static async Task Main(string[] args) { - // Set up dependency injection for EventBridge. + // Set up dependency injection for Amazon EventBridge. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) @@ -84,18 +84,20 @@ static async Task Main(string[] args) await CreateBucketWithEventBridgeEvents(); + await AddEventRule(roleArn); + + await ListEventRules(); + topicArn = await CreateSnsTopic(); var email = await SubscribeToSnsTopic(topicArn); - await AddSnsEventRule(roleArn, topicArn); + await AddSnsTarget(topicArn); - await ListEventRules(); + await ListTargets(); await ListRulesForTarget(topicArn); - await ListTargets(); - await UploadS3File(_s3Client); await ChangeRuleState(false); @@ -140,7 +142,7 @@ private static void ServicesSetup(IHost host) /// /// Create a role to be used by EventBridge. /// - /// The role ARN. + /// The role Amazon Resource Name (ARN). public static async Task CreateRole() { Console.WriteLine(new string('-', 80)); @@ -174,20 +176,21 @@ await _iamClient.AttachRolePolicyAsync( PolicyArn = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess", RoleName = roleName }); - + // Allow time for the role to be ready. + Thread.Sleep(10000); return roleResult.Role.Arn; } // snippet-end:[EventBridge.dotnetv3.CreateRole] // snippet-start:[EventBridge.dotnetv3.CreateBucketWithEvents] /// - /// Create an Amazon S3 bucket with EventBridge events enabled. + /// Create an Amazon Simple Storage Service (Amazon S3) bucket with EventBridge events enabled. /// /// Async task. private static async Task CreateBucketWithEventBridgeEvents() { Console.WriteLine(new string('-', 80)); - Console.WriteLine("Creating an Amazon S3 bucket with EventBridge events enabled."); + Console.WriteLine("Creating an S3 bucket with EventBridge events enabled."); var testBucketName = _configuration["testBucketName"]; @@ -336,7 +339,7 @@ await _snsClient.SubscribeAsync(new SubscribeRequest() Endpoint = email }); - Console.WriteLine($"Use the link in the email you received to confirm your subscription, then press enter to continue."); + Console.WriteLine($"Use the link in the email you received to confirm your subscription, then press Enter to continue."); Console.ReadLine(); @@ -345,20 +348,37 @@ await _snsClient.SubscribeAsync(new SubscribeRequest() } /// - /// Add a rule which triggers an SNS target when a file is uploaded to an S3 bucket. + /// Add a rule which triggers when a file is uploaded to an S3 bucket. /// /// The ARN of the role used by EventBridge. - /// The ARN of theSNS topic. /// Async task. - private static async Task AddSnsEventRule(string roleArn, string topicArn) + 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"]; - var topicName = _configuration["topicName"]; + await _eventBridgeWrapper.PutS3UploadRule(roleArn, eventRuleName, testBucketName); + Console.WriteLine($"\tAdded event rule {eventRuleName} for bucket {testBucketName}."); + + Console.WriteLine(new string('-', 80)); + } + + /// + /// Add an SNS target to the rule. + /// + /// The ARN of the SNS topic. + /// Async task. + 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}."); @@ -433,7 +453,7 @@ private static async Task TriggerCustomRule(string email) await _eventBridgeWrapper.PutCustomEmailEvent(email); - Console.WriteLine($"\tEvents have been sent. Press enter to continue."); + Console.WriteLine($"\tEvents have been sent. Press Enter to continue."); Console.ReadLine(); Console.WriteLine(new string('-', 80)); diff --git a/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.csproj b/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.csproj index cd129e4d32c..1f5aba75415 100644 --- a/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.csproj +++ b/dotnetv3/EventBridge/Scenarios/EventBridgeScenario.csproj @@ -7,22 +7,18 @@ enable - - - - PreserveNewest - + PreserveNewest settings.json - +