WebSocket Server with API Gateway

Recently I wanted to evaluate the experience of writing infrastructure code with AWS CDK using C#.

I decided to write a simple WebSocket server using API Gateway and Lambda. The server uses API Gateway's built-in API key authentication. It persists connections in a DynamoDB table, allows users to connect, subscribe to topics, and broadcast messages to all other subscribers of a topic.

The infrastructure and source code is all written in C# with CDK. The server is implemented in C# as a Lambda function targeting the .NET 8.0 runtime.

I have extensive experience writing terraform in HCL, writing AWS cdk in typescript, and using terraform's cdktf tool (also in typescript) for writing IaC. C# is quite popular in my industry (fintech), and is not without its advantages over node runtimes. However, sourcing
developers comfortable contributing across multiple languages and ecosystems can be somewhat challenging. I wanted to see what the experience is like to write everything--lambda code and infrastructure-- exclusively in C# with CDK and the Lambda .NET runtime.

The source code is available upon request.

Some highlights: AWS Lambda .NET Blueprints and .NET Core Global Tools for AWS.

Some downsides:

  • Cross-platform compilation: the official documentation still warns about cross-compilation issues for native AoT compilation. The .NET Lambda CLI will spin up a whole Docker container
    to compile your image in. These are the kind of cloud-computing issues Go set out to solve at the outset, and the story in .NET world is still not nearly as nice.
  • Again on native AoT: Serialization is a mixed bag. You will have to dig into nitty-gritty details of what serialization options are supported for source generated serialization.
  • CDK constructs: some AWS features are still in Alpha CDK packages.

Overall, it's an experience still filled with the sorts of pitfalls and gotchas that, in my experience, will trip up developers. These kinds of issues lead to "golden config" pressures that CDK otherwise helps to alleviate. At this stage, C# is not going to replace Node as a go-to for fast prototyping, and as a second-language to turn to for performance, it's not as stable an experience on CDK and Lambda as Go.

Where does this all seem to be going? Perhaps C# on CDK will mature into a more stable experience. However, with other trends in the industry, that may not matter as much: projects like cdktf are leaning heavily toward construct library design. Terraform has its cdktf constructs library, and an architecture for publishing and distributing constructs, but only for TypeScript. All of this suggests serverless tooling will continue to favor a TypeScript-everywhere world.