Microsoft Workloads on AWS

Build, package, and publish .NET C# Lambda functions with the AWS CDK

AWS Cloud Development Kit (CDK) is an open-source software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation. It offers a high-level abstraction to define AWS resources using modern programming languages. Among its components, it provides aws-s3-assets, which is a high level construct that abstracts packaging AWS Lambda functions. The default behavior of this construct is to zip all the content into a folder and upload it to an Amazon Simple Storage Service (Amazon S3) bucket. That works great for Lambda runtimes like Python or Node.js, which do not require code compilation, but for .NET, Java, or Go, which requires code compilation, you’ll need extra steps to restore external dependencies, compile the code, and publish the binary. This post will explore how to streamline building, packaging, and publishing .NET Lambda functions using AWS CDK.

Architecture overview

In this blog post, we will show you how to set up a basic serverless architecture using the AWS CDK. This architecture will create a REST API using Amazon API Gateway, Lambda proxy integration and ASP.NET Web API on Lambda. ASP.NET Web API is a framework for building web APIs on top of the .NET that you can host and run on Lambda functions. This AWS CDK implementation will create three Lambda functions with this similar architecture, all written in .NET and maintained as separate ASP.NET Web API projects. Depending on the URL path requested, the API Gateway will route the traffic to the specific Lambda function.
Architecture diagram with API Gateway and Three Lambda Functions

Code Walkthrough

Prerequisites

To allow the AWS CDK application to compile and deploy a Lambda function written in .NET, you must first create an object of the BundlingOptions Class. The Lambda constructor will use this object to compile and generate binary files from the .NET Lambda functions source code during the AWS CDK Project synthetization time.

The following example shows how you can define the BundlingOptions Class for .NET Lambda function. These are the relevant properties you’ll need to set:

  • Image – should receive an AWS container image that matches the Lambda runtime
  • User – set to the “root” for the runtime permission to build and generate the output binary file
  • OutputType – defines how the CDK should expect the package. For this demo, it’s set to ZIP Archive.
  • Command – is expecting the build and package instructions. In the following code snippet example, you can see how I’ve implemented it by leveraging the utility Amazon.Lambda.Tools – to help with .NET commands for Lambda function packaging.
var buildOption = new BundlingOptions()
{
    Image = Runtime.DOTNET_6.BundlingImage,
    User = "root",
    OutputType = BundlingOutput.ARCHIVED,
    Command = new string[]{
   "/bin/sh",
    "-c",
    " dotnet tool install -g Amazon.Lambda.Tools"+
    " && dotnet build"+
    " && dotnet lambda package --output-package /asset-output/function.zip"
    }
};

With this BundlingOption object definition, the AWS CDK will handle the process of deploying a .NET Lambda function project. The following sample code is an example of how to pass this BundlingOption object definition to the AWS CDK function construct:

var lambdaFunctionOne = new Function(this, "my-funcOne", new FunctionProps
{
    ...
    Code = Code.FromAsset("../apps/src/FunctionOne/", new Amazon.CDK.AWS.S3.Assets.AssetOptions
    {
        Bundling = buildOption
    }),
});

Deployment and Demo

The GitHub repository “aws-samples/aws-cdk-build-package-publish-dotnet-lambda-function” provides the full demo, so you can deploy it and make requests to three sample API to see the implementation.

Deployment

Follow the steps below to deploy and test the .NET Lambda functions:

  1. Clone the GitHub repository
    git clone https://github.com/aws-samples/aws-cdk-build-package-publish-dotnet-lambda-function.git
  2. Open a terminal session and navigate to the infra folder within the project source code
    cd /aws-cdk-build-package-publish-dotnet-lambda-function/infra
  3. From the Infra folder, type CDK deploy using your AWS account profile
    cdk deploy --profile <your AWS account profile alias>

Demo

Once the deployment is complete, copy the Endpoint from the terminal output.The format should be similar to: https://xxxyyyzzz.execute-api.us-east-1.amazonaws.com/prod/. Use this endpoint in your favorite REST API client to make the request. This demo is using Thunder Client. Or you can paste the URL in a browser address bar and receive the messages that way. The following example shows a Request and Response:

HTTP Get Request to FunctionOne URL using VSCode Thunder Client

Or you can use CURL to make the request, as shown in the following example:

Request: curl https://xxxyyyzzz.execute-api.us-west-2.amazonaws.com/prod/

Response: Welcome to running ASP.NET Core Minimal API on AWS Lambda - Function One!

To test the other Lambda function, alter the URL by adding /functiontwo or /functionthree at the end of the original URL. For example, use https://xxxyyyzzz.execute-api.us-east-1.amazonaws.com/prod/functiontwo and make a request.

HTTP Get Request to FunctionTwo using VSCode Thunder Client

Or you can use CURL to make the request, as shown in the following example:

Request: curl https://xxxyyyzzz.execute-api.us-west-2.amazonaws.com/prod/functiontwo

Response: Welcome to running ASP.NET Core Minimal API on AWS Lambda - Function Two!

Cleaning Up

Follow one of the two ways to clean up the deployment:

  1. Go to the AWS Management Console and navigate to the CloudFormation stacks section and delete the stack:  InfraStack
  2. From the command line, type CDK destroy from the same directory that you deployed from.
    cd ./aws-cdk-build-package-publish-dotnet-lambda-function/infra
    cdk destroy --profile <your AWS account profile alias>

Conclusion

In this blog post, you’ve learned how to streamline building, packaging, and publishing .NET Lambda functions using AWS CDK. You can also apply similar steps to other Lambda runtime that required compiling the code, like Java or Go. This approach simplifies the process by handling the .NET Lambda function code compilation and the infrastructure provisioning, empowering you to build and then deploy the Lambda function with no extra steps for compilation outside of the AWS CDK deployment process.


AWS can help you assess how your company can get the most out of cloud. Join the millions of AWS customers that trust us to migrate and modernize their most important applications in the cloud. To learn more on modernizing Windows Server or SQL Server, visit Windows on AWSContact us to start your modernization journey today.

TAGS:
Ulili Nhaga

Ulili Nhaga

Ulili Nhaga is a Cloud Application Architect at Amazon Web Services in San Diego, California. He helps customers migrate, modernize, architect, and build highly scalable cloud-native applications on AWS. Outside of work, Ulili loves playing soccer, running, cycling, Brazilian BBQ, and enjoying time on the beach.