How do I build a Lambda deployment package for C# .NET?

3 minute read
0

I created an AWS Lambda function deployment package in C#. However, when I try to invoke the function, Lambda returns one of the following errors: "module not found," "module cannot be loaded," or "cannot find class." How do I troubleshoot the issue?

Short description

If your C# Lambda function returns any of the following errors, then the function's deployment package's folder structure isn't configured correctly:

  • module not found
  • module cannot be loaded
  • cannot find class

To resolve the issue, you must build a C# Lambda function deployment package that has the correct folder structure. There are two ways you can build and deploy a C# Lambda function deployment package with the correct folder structure:

Resolution

To use the .NET Core CLI and the Amazon.Lambda.Tools extension

1.    Install the default Lambda .NET templates and add the Amazon.Lambda.Tools extension to the .NET Core CLI by running the following command:

dotnet new -i 'Amazon.Lambda.Templates::*'

2.    Either create a new Lambda function using one of the templates you installed, or add the Amazon.Lambda.Tools extension to an existing project.

To create a new Lambda function using one of the templates you installed

From the .NET Core CLI in the Lambda function's project root directory, run the following command:

Important: Replace {function-name} with your function's name. Replace {aws-region} with the AWS Region that you want your function in.

dotnet new lambda.EmptyFunction --name {function-name} --profile default --region {aws-region}

To add the Amazon.Lambda.Tools extension to an existing project

From the .NET Core CLI in the Lambda function's project root directory, run the following command:

dotnet tool install -g Amazon.Lambda.Tools

Note: The Amazon.Lambda.Tools extension will prompt you to provide any required parameters that are missing.

3.    Download your deployment package's dependencies by running the following command:

Important: Replace {your-function-directory} with your function directory's name.

cd {your-function-directory}
dotnet restore

Note: If you get a not compatible error, make sure that you're using a version of .NET Core that's compatible with Lambda tools. To download earlier versions of .NET Core, see the .NET Download Archives website.

4.    Build your Lambda deployment package by running the following command:

dotnet lambda deploy-function

Note: Or, you can build a Lambda deployment package from scratch and deploy it separately. For instructions, see Deploying an AWS Lambda project with the .NET Core CLI.

5.    The .NET Core CLI prompts you to enter a function name and assign an AWS Identity and Access Management (IAM) role to the function. Enter a name for your function and assign the function an IAM role. Your function is then created.

To use the AWS Toolkit for Visual Studio

1.    Download and install the AWS Toolkit for Visual Studio.

2.    Create and build an AWS Lambda Project (.NET Core) project. For instructions, see Using the AWS Lambda templates in the AWS Toolkit for Visual Studio and AWS Toolkit for Visual Studio in the AWS Lambda developer guide.

Important: Make sure that the function handler signature is in the following format:

ASSEMBLY::TYPE::METHOD

To confirm that the function is formatted correctly, review the files under your function's src/{function-name} directory. For more information, see .NET Core CLI and AWS Lambda function handler in C#.


AWS OFFICIAL
AWS OFFICIALUpdated 3 years ago