Deploy C# Lambda functions using Amazon CDK - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Deploy C# Lambda functions using Amazon CDK

The Amazon Cloud Development Kit (Amazon CDK) is an open-source software development framework for defining cloud infrastructure as code with modern programming languages and frameworks like .NET. Amazon CDK projects are executed to generate Amazon CloudFormation templates which are then used to deploy your code.

To build and deploy an example Hello world .NET application using the Amazon CDK, follow the instructions in the following sections. The sample application implements a basic API backend consisting of an API Gateway endpoint and a Lambda function. API Gateway invokes the Lambda function when you send an HTTP GET request to the endpoint. The function returns a Hello world message, along with the IP address of the Lambda instance that processes your request.

Prerequisites

.NET 8 SDK

Install the .NET 8 SDK and Runtime.

Amazon CDK version 2

To learn how to install the latest version of the Amazon CDK see Getting started with the Amazon CDK in the Amazon Cloud Development Kit (Amazon CDK) v2 Developer Guide.

Deploy a sample Amazon CDK application

  1. Create a project directory for the sample application and navigate into it.

    mkdir hello-world cd hello-world
  2. Initialize a new Amazon CDK application by running the following command.

    cdk init app --language csharp

    The command creates the following files and directories in your project directory

    ├── README.md ├── cdk.json └── src ├── HelloWorld │   ├── GlobalSuppressions.cs │   ├── HelloWorld.csproj │   ├── HelloWorldStack.cs │   └── Program.cs └── HelloWorld.sln
  3. Open the src directory and create a new Lambda function using the .NET CLI. This is the function you will deploy using the Amazon CDK. In this example, you create a Hello world function named HelloWorldLambdausing the lambda.EmptyFunction template.

    cd src dotnet new lambda.EmptyFunction -n HelloWorldLambda

    After this step, your directory structure inside your project directory should look like the following.

    ├── README.md ├── cdk.json └── src ├── HelloWorld │   ├── GlobalSuppressions.cs │   ├── HelloWorld.csproj │   ├── HelloWorldStack.cs │   └── Program.cs ├── HelloWorld.sln └── HelloWorldLambda ├── src │   └── HelloWorldLambda │   ├── Function.cs │   ├── HelloWorldLambda.csproj │   ├── Readme.md │   └── aws-lambda-tools-defaults.json └── test └── HelloWorldLambda.Tests ├── FunctionTest.cs └── HelloWorldLambda.Tests.csproj
  4. Open the HelloWorldStack.cs file from the src/HelloWorld directory. Replace the contents of the file with the following code.

    using Amazon.CDK; using Amazon.CDK.AWS.Lambda; using Amazon.CDK.AWS.Logs; using Constructs; namespace CdkTest { public class HelloWorldStack : Stack { internal HelloWorldStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { var buildOption = new BundlingOptions() { Image = Runtime.DOTNET_8.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" } }; var helloWorldLambdaFunction = new Function(this, "HelloWorldFunction", new FunctionProps { Runtime = Runtime.DOTNET_8, MemorySize = 1024, LogRetention = RetentionDays.ONE_DAY, Handler = "HelloWorldLambda::HelloWorldLambda.Function::FunctionHandler", Code = Code.FromAsset("./src/HelloWorldLambda/src/HelloWorldLambda", new Amazon.CDK.AWS.S3.Assets.AssetOptions { Bundling = buildOption }), }); } } }

    This is the code to compile and bundle the application code, as well as the definition of the Lambda function itself. the BundlingOptions object allows a zip file to be created, along with a set of commands that are used to generate the contents of the zip file. In this instance, the dotnet lambda package command is used to compile and generate the zip file.

  5. To deploy your application, run the following command.

    cdk deploy
  6. Invoke your deployed Lambda function using the .NET Lambda CLI.

    dotnet lambda invoke-function HelloWorldFunction -p "hello world"
  7. After you've finished testing, you can delete the resources you created, unless you want to retain them. Run the following command to delete your resources.

    cdk destroy

Next steps

To learn more about using Amazon CDK to build and deploy Lambda functions using .NET, see the following resources: