Custom Metrics to AWS CloudWatch from ASP.NET Core

CloudWatch Custom Metrics

I was playing around with AWS CloudWatch and was curious to send custom metrics from ASP.NET Core. Specifically the execution time of an HTTP request.

AWS SDK

I created a simple middleware that starts a StopWatch before calling the next middleware in the pipeline. When it returns, stop the StopWatch and send the data to CloudWatch.

First is to add the relevant NuGet packages.

Configuration

If you’ve never used the AWS SDK/Packages, I recommend checking out my post on Configuring AWS SDK in ASP.NET Core. It goes over creating a named profile to store your AWS credentials and using appSettings or Environment variables to pass them through to ASP.NET Core.

Middleware

Next is to create a simple middleware using the SDK. The middleware is having the IAmazonCloudwatch injected into the constructor. In the Startup’s ConfigureServices is where this is configured.

We’re simply calling the PutMetricDataAsync with a list of one MetricDatum. It contains the metric name, value, unit, timestamp, and dimensions.

Startup

As mentioned, in order to use the AWS SDK and the IAmazonCloudWatch in the middleware, you can use the AddDefaultAWSOptions and AddAWSService<IAmazonCloudWatch> to ConfigureServices to register the appropriate types.

Results

If you look in CloudWatch, you can now see the new custom metrics we’ve published.

Next

Before you start using this in a production environment, there are a couple of glaring issues.

First, sending the metrics to AWS, although incredibly fast should be handled outside of the actual HTTP request. this can be done completely asynchronously from the actual request. There is no point in delaying the HTTP response to the client. To solve this, we’ll add a queue and do it in a background service.

Secondly, some of the cost associated with CloudWatch is based on the number of API calls. This is why there is a List<MetricDatum> in the PutMetricDataRequest. It allows you to send/batch multiple metrics together to limit the number of API calls.

This is a better solution is to collect PutMetricDataRequest separately and send them in batch in a background service.

More on that coming soon.

Follow @CodeOpinion on Twitter

Software Architecture & Design

Get all my latest YouTube Vidoes and Blog Posts on Software Architecture & Design

Using AWS Parameter Store for .NET Core Configuration

Parameter Store for .NET Core

One of the aspects I love about .NET Core is the new configuration and options pattern. In a continuation from my last post on using AWS Parameter Store for Data Protection keys, you can imagine it is possible to use Parameter Store for .NET Core Configuration.

Amazon.Extensions.Configuration.SystemsManager

There is a package by AWS that facilitates making using Parameter Store incredibly easy. Simply add the Amazon.Extensions.Configuration.SystemsManager package to your project and use the AddSystemsManager extension method on IConfigurationBuilder

The argument you pass to AddSystemsManager will be the prefix to your configuration hierarchy within Parameter Store. In my example, I’m using /Demo as my prefix, as you will also see below.


If you look at the first two entries, I can now create classes that match this hierarchy.

Configure

Next step is to add this configuration via the IServiceCollection using the Configure<T> method. Where T is our configuration type DemoConfig

This will add registration for the type IOptions<DemoConfig> so we can inject it in our Controllers or anywhere else the ServiceProvider is used.

This results in our config variable being set to the values from Parameter Store.

Parameter Store for .NET Core

If you have any questions or comments, please let me know on twitter as I will focus my posts on those questions and comments.

Related Links:

Follow @CodeOpinion on Twitter

Software Architecture & Design

Get all my latest YouTube Vidoes and Blog Posts on Software Architecture & Design

Using AWS Parameter Store for ASP.NET Core Data Protection Keys

Using AWS Parameter Store for ASP.NET Core Data Protection Keys

If you’re using ASP.NET Core in AWS under any type of load balanced scenario, either through Elastic Beanstalk or an ALB with and ECS, etc, you will need to share the data protection keys. This is because each instance of your application needs to be using the exact same keys.

This isn’t an issue if you are using a single instance as the keys will be stored in memory.

If you haven’t yet used the AWS SDK, I highly recommend first checking out my quick start on configuring AWS SDK in ASP.NET Core.

AWS Systems Manager Parameter Store

One option is to use Parameter store to store the data protection keys. Thankfully AWS has released a nice little package to make this really simple.

First, add the Amazon.AspNetCore.DataProtection.SSM package to your csproj.

Now you can use the PersistKeysToAWSSystemsManager method passing the prefix as the parameter.

That’s it! That simple. Now when you run your application, you will see that a new parameter has been created with the prefix you specified followed by key-{GUID}.

ASP.NET Core Data Protection

If you want to persist data protection keys to somewhere like S3, check out this other post.

If you have any questions or comments, please let me know on twitter as I will focus my posts on those questions and comments.

Related Links:

Follow @CodeOpinion on Twitter

Software Architecture & Design

Get all my latest YouTube Vidoes and Blog Posts on Software Architecture & Design