Skip to content

Commit

Permalink
EventBridge .NET MVP Updates (awsdocs#4643)
Browse files Browse the repository at this point in the history
* Updating order of the scenario.
* Fix for file references.
* Incorporating feedback and removing old code
  • Loading branch information
rlhagerm authored Apr 5, 2023
1 parent 76f4390 commit 722d8f3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
14 changes: 2 additions & 12 deletions dotnetv3/EventBridge/Actions/HelloEventBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@
// 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;

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<IAmazonEventBridge>(new AWSOptions() { Profile = "default" })
).Build();

// Now the client is available for injection.
var eventBridgeClient = host.Services.GetRequiredService<IAmazonEventBridge>();
var eventBridgeClient = new AmazonEventBridgeClient();

Console.WriteLine($"Hello Amazon EventBridge! Following are some of your EventBuses:");
Console.WriteLine();
Expand Down
50 changes: 35 additions & 15 deletions dotnetv3/EventBridge/Scenarios/EventBridgeScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -140,7 +142,7 @@ private static void ServicesSetup(IHost host)
/// <summary>
/// Create a role to be used by EventBridge.
/// </summary>
/// <returns>The role ARN.</returns>
/// <returns>The role Amazon Resource Name (ARN).</returns>
public static async Task<string> CreateRole()
{
Console.WriteLine(new string('-', 80));
Expand Down Expand Up @@ -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]
/// <summary>
/// Create an Amazon S3 bucket with EventBridge events enabled.
/// 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 Amazon S3 bucket with EventBridge events enabled.");
Console.WriteLine("Creating an S3 bucket with EventBridge events enabled.");

var testBucketName = _configuration["testBucketName"];

Expand Down Expand Up @@ -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();

Expand All @@ -345,20 +348,37 @@ await _snsClient.SubscribeAsync(new SubscribeRequest()
}

/// <summary>
/// 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.
/// </summary>
/// <param name="roleArn">The ARN of the role used by EventBridge.</param>
/// <param name="topicArn">The ARN of theSNS topic.</param>
/// <returns>Async task.</returns>
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));
}

/// <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}.");

Expand Down Expand Up @@ -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));
Expand Down
8 changes: 2 additions & 6 deletions dotnetv3/EventBridge/Scenarios/EventBridgeScenario.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="settings.json" />
<None Remove="settings.local.json" />
</ItemGroup>

<ItemGroup>
<Content Include="settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="settings.local.json">
<Content Include="settings.*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DependentUpon>settings.json</DependentUpon>
</Content>
</ItemGroup>

<ItemGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.EventBridge" Version="3.7.101.20" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.2" />
<PackageReference Include="AWSSDK.IdentityManagement" Version="3.7.100.81" />
Expand Down

0 comments on commit 722d8f3

Please sign in to comment.