Deploy C# Lambda functions using AWS CDK
The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework for defining cloud infrastructure as code with modern programming languages and frameworks like .NET. AWS CDK projects are executed to generate AWS CloudFormation templates which are then used to deploy your code.
To build and deploy an example Hello world .NET application using the AWS 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. - AWS CDK version 2
-
To learn how to install the latest version of the AWS CDK see Getting started with the AWS CDK in the AWS Cloud Development Kit (AWS CDK) v2 Developer Guide.
Deploy a sample AWS CDK application
-
Create a project directory for the sample application and navigate into it.
mkdir hello-world cd hello-world
-
Initialize a new AWS 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
-
Open the
src
directory and create a new Lambda function using the .NET CLI. This is the function you will deploy using the AWS CDK. In this example, you create a Hello world function namedHelloWorldLambda
using thelambda.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
-
Open the
HelloWorldStack.cs
file from thesrc/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, thedotnet lambda package
command is used to compile and generate the zip file. -
To deploy your application, run the following command.
cdk deploy
-
Invoke your deployed Lambda function using the .NET Lambda CLI.
dotnet lambda invoke-function HelloWorldFunction -p "hello world"
-
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 AWS CDK to build and deploy Lambda functions using .NET, see the following resources: