This content is part of the Essential Guide: AWS Lambda architecture brings serverless to enterprise cloud

Use C# on AWS Lambda to build multi-platform apps

AWS Lambda includes support for the C# programming language, enabling developers to use .NET Core to build serverless functions and environments.

AWS supports the C# programming language for AWS Lambda, allowing developers to publish C# code to the service...

using the .NET Core 1.0 runtime open source tool. This enables .NET developers to build Lambda functions and serverless applications using C# on AWS.

C# is multithreaded language that can handle multiple requests per second. Wrapping it with Lambda enables IT teams to build a serverless application that returns consistent performance, even as the demand for the app grows.

Developers can start projects using C# on AWS Lambda and integrate them with other Amazon cloud services, such as Amazon Simple Storage Service, AWS CloudFormation, Amazon DynamoDB and Amazon Simple Notification Service, to improve applications. AWS Lambda enables developers to run and scale code without provisioning servers. Other AWS tools and services trigger C# code in Lambda, or can be automatically called from any web or mobile application.

Running C# scripts on AWS Lambda enables developers to create new APIs through the Amazon API Gateway for serverless applications.

Here is a simple function for C# on AWS that uses a low-level stream interface to write and process DynamoDB events:

C# on AWS that uses a low-level stream interface to write and process DynamoDB events

A developer doesn't need to implement a particular interface to mark it as a Lambda function code; he must provide a handler string while uploading code and instruct Lambda to start the execution. There are only a few choices for handling inputs and return types on Lambda. Developers can use a low-level, System.IO.Stream interface, but they can also define specific serialization logic through implementation of ILambdaSerializer found in the Amazon.Lambda.Core library.

The Amazon.Lambda.Core library provides many classes for other AWS event types, such as the DynamoDBEvent Signature in the ProcessDynamoEvent functions. Developers can add project dependencies on a NuGet package to access static Lambda loggers, serialization interfaces and C# implementations of Lambda context objects. Static Write or Writeline methods that the C# console provides are the log method for the Amazon.Lambda.Core.LambdaLogger class or Logger property in the context object.

AWS provides two project templates, both associated with the AWS toolkit for Visual Studio, which supports developing, testing and deploying .NET Core Lambda functions and serverless applications. The AWS Lambda Project template creates a simple project with a single C# Lambda function, while the AWS Serverless Application template creates a small serverless application.

Use Visual Studio with Lambda

Before firing up C# on AWS Lambda in Visual Studio 2015, you need to start a new project. In the New Project wizard, under Templates, select Visual C# and then select AWS Lambda. From the list of AWS templates, choose Select the AWS Lambda Project (.NET Core) and name your project. In this example, we used the name LambdaFunction.

After finishing the C# coding, the developer can directly deploy it from Visual Studio by right-clicking the project name and selecting Publish to AWS Lambda in the Solution Explorer. The deployment wizard then guides the user through the development process.

Create a serverless app via Visual Studio and Lambda

Let's create an AWS serverless application and name it Blogger. First, select a blueprint; for this example, select Blog API using the Amazon DynamoDB blueprint option.

These are the project files:

  • Blog.cs represents the blog items stored in DynamoDB.
  • Functions.cs defines all C# functions a developer uses as Lambda functions. There are four such functions:
    • GetBlogsAsync gets a list of all blogs.
    • AddBlogAsync adds a blog to the DynamoDB table.
    • GetBlogAsync gets a single blog identified by either the query parameter ID or the ID added to the URL resource path.
    • RemoveBlogAsync removes a blog from the DynamoDB table.

All the functions accept an APIGatewayProxyRequest object and return an API GatewayProxyResponse for Lambda functions to be exposed as HTTP APIs using Amazon API Gateway. APIGatewayProxyRequest has all the information, which represents the HTTP request.

A developer can also use Serverless.template, which is an AWS CloudFormation template that deploys the four functions mentioned above. This template makes it possible to set the name of a DynamoDB table and choose whether CloudFormation creates it or assumes it was already created.

After adding C# coding, a developer can deploy the project and publish to AWS Lambda.

Build serverless applications via .NET

The .NET Framework typically enables developers to build and run applications on Windows. But .NET Core has a feature that enables code development and deployment of C# on AWS or any platform.

Even without access to Visual Studio, developers can still use .NET tools to deploy C# Lambda functions and serverless applications to AWS. They can use the .NET command-line interface (CLI) to build and deploy processes automatically.

After creating a .NET Core project, a developer can use tools like Yeoman or Broccoli to enable Lambda tools in the .NET CLI, while only adding dependency on the Amazon.Lambda.Tools NuGet package to the project.

AWS Lambda tools in the .NET CLI also set up a continuous integration and continuous delivery pipeline to connect with Visual Studio on Windows. Enter ".NET lambda help" in the project directory to view additional Lambda commands in the .NET CLI.

Next Steps

AWS Lambda avoids lock-in, expands development options

Azure Machine Learning makes easy jump to C# for AWS users

Knowledge of C# and other languages provides boosts for AWS careers

Dig Deeper on AWS tools for development