Skip to content
Allows to route a SQS message to a specific endpoint on the Elastic Beanstalk Worker instead of having a single endpoint handling all the messages.
C# Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
build
samples
src/BeanstalkWorker.SimpleRouting
tests/BeanstalkWorker.SimpleRouting.Tests
.editorconfig
.gitattributes
.gitignore
GitVersion.yml
LICENSE
README.md
SimpleRouting.sln
appveyor.yml
bootstrap.ps1
bootstrap.sh
build.cake

README.md

Elastic Beanstalk Worker Simple Routing

Package Release Pre-release
BeanstalkWorker.SimpleRouting NuGet MyGet
CI Status Platform(s) Framework(s) Test Framework(s)
AppVeyor Build Status Windows nestandard2.0 netcoreapp2.2.0

Allows to route a SQS message to a specific endpoint on the Worker instead of having a single endpoint handling all the messages.

Relies on the SQS message attributes. This is distributed via a NuGet package but the implementation is so simple that you can just copy the classes into your own solution if that works better for you.

How it works

Web Tier - Set the attribute on the message

// Instantiate your model
StronglyTypedMessage model = new StronglyTypedMessage();

var sendMessageRequest = new SendMessageRequest
{
  // Serialize your model as JSON
  MessageBody = JsonConvert.SerializeObject(model)
  // Set the QueueUrl
};

// AddRoutingAttribute is an extension method
sendMessageRequest.MessageAttributes.AddRoutingAttribute("task-name");

Web sample project

A sample Web app is provided in samples/SampleWeb.

You can send two distinct types of messages by hitting two different endpoints:

  • GET /send/work
  • GET /send/nothing
Configuration

Create a iAM user (if you don't have one already) which has access to SQS.

You'll need to configure four settings, either using user secrets (preferred way as they'll be shared with the sample worker), appsettings.json or via environment variables:

  • Aws:RegionSystemName - region code, for example ap-southeast-2
  • Aws:Queue:WorkerQueueUrl - URL of the SQS queue, for example https://sqs.ap-southeast-2.amazonaws.com/375985941080/dev-gabriel
  • AWS_ACCESS_KEY_ID - this is the Access key ID of your iAM user
  • AWS_SECRET_ACCESS_KEY - this is the Secret access key of your iAM user

Worker Tier

Add the middleware to the Worker

In the Configure method of your Startup class:

public void Configure(IApplicationBuilder app)
{
    app.UseHeaderRouting();

    // Abbreviated for clarity
}

Use a matching route on a Controller Action

// This is important, we do not want a prefix in front of the action's route
[Route("")]
public class SomeController : Controller
{
  // The route has to match the argument given to AddRoutingAttribute
  [HttpPost("task-name")]
  public async Task<IActionResult> SomeMethod(StronglyTypedMessage model)
  {
      // Abbreviated for clarity
  }
}

Worker sample project

A sample Worker app is provided in samples/SampleWorker.

If you wish to run the Worker without deploying to AWS Beanstalk you can leverage my Beanstalk Seeder project.

Limitations

  • Does not support periodic tasks
    • It could be added fairly easily if required
  • netstandard2.0 and above only
    • .NET is a second class citizen on Elastic Beanstalk, for this reason I recommend creating Docker images so that you can take full advantage of the platform.
You can’t perform that action at this time.