Serverless API with AWS Lambda, Cloudformation and C# Dotnet

Serverless API with AWS Lambda, Cloudformation and C# Dotnet

 
Lambda Annotations is a programming model for writing .NET methods like GET, PUT, POST etc in a way that combines whats great about the serverless aspect of AWS Lambdas and the coding/maintenance aspect of coding a C# .NET API locally.
One challenge when writing Lambda functions is the limited context and if you wanted to run an entire API as a part of a lambda function that thats a lot of overhead which defies much of the purpose for serverless.
Using annotation the developer can focus on clean/maintainable code while still getting the performance and efficiencies of serverless without much extra planning. In-fact you can convert existing APIs to Lambda annotations with relatively little effort.
 
In this tutorial we’ll setup a sample codebase and deploy it using Cloud Formation. Every Method endpoint will be automatically deployed as it’s own lambda function inside AWS
 
 
Pros:
Simple config, easy to maintain and serverless
 
Cons:
Somewhat inflexible, doesn’t necessarily scale as the project becomes complex and an unconventional pattern
 

Tools

  • Jetbrains Rider
  • AWS Account
  • AWS CLI
  • C# .NET
  • Optional: AWS Serverless Toolkit Extension (If not using Jetbrains Rider for starter files)
 
 

Setup Demo Project

1- Create an AWS account and an S3 bucket with a name for your deployment, e.g my-app
2- Open Jetbrains Rider
3- Create a new solution and select Lambda Annotations Framework Sample
notion image
 
4- Note the created files in the directory you chose
  • CalculatorService.cs - Contains all the classes/methods you can build your application from
  • aws-lambda-tools-defaults.json - Contains code that tells dotnet how to handle the repo
  • Functions.cs - Where your GET, PUT, POST etc methods will go and be broken off as individual Lambda functions
  • Startup.cs - Where depenencies and Singletons can be installed
  • serverless.template - Cloudformation script which is automatically generated as you write your API (This is the best part of the whole setup!) Think main.tf if you know terraform
 
5- Build your API as desired, installing dependencies as needed in the above files and even make use of other AWS services by adding to Startup.cs, e.g Adding DynamoDB
notion image
 
6- To deploy, ensure you’ve configured your AWS CLI
7- Run dotnet lambda deploy-serverless
  • Add a name for the CloudFormation stack, anything you’d like to see in AWS, e.g MyAppStack
  • For the S3 bucket, use the same name you made in Step 1.
8- Test your endpoints with the output url https://myapp.execute-api.ap-southeast-2.amazonaws.com/
notion image
 
 

Why not use Azure?

Azure is arguably a better solution for anything based in C#, but in this case it might be preferred for tighter and easier integration with AWS services such as DynamoDB as mentioned above. Simply add the dependency and role then you’re all set
 
 

Why CloudFormation and not Terraform?

The best part about this setup is the cloud deployment is automatically generated as you extend your API, no config needed. You can absolutely use terraform, but it’s more complicated than this approach

Guides and practical notes, training references, and code snippets shared freely for learning and career growth.