Configure C#.Net project to use dll from Lambda layer

0

Hi,

I went through many documenations about Lambda layers and how to use them. My problem is, lets say I have a Lambda project which uses library ABC and Lambda layer which contains library ABC (ABC.dll). When I publish the this project to deploy in Lambda, it contains ABC library dll but I want the lambda to use the library ABC.dll from Layer not from the project publish/build zip.

Basically what I am looking is a way not to have ABC.dll in zip which I upoad to lambda and lambda should use ABC.dll from layer.

asked 13 days ago169 views
1 Answer
2
Accepted Answer

Create and Publish Your Lambda Layer

  1. Prepare the Layer Directory: Create a directory structure that Lambda expects. For .NET libraries, Lambda layers expect the structure to be as follows:
layer/
└── bin/
    └── netcoreapp3.1/  (or your target framework version)
        └── ABC.dll
  1. Zip the Layer: Compress the layer/ directory into a .zip file.
  2. Publish the Layer: Use the AWS CLI to publish the layer:
aws lambda publish-layer-version --layer-name "MyDotNetLayer" --zip-file "fileb://layer.zip" --compatible-runtimes dotnetcore3.1

Replace dotnetcore3.1 with your target runtime if different.

Configure Your C#.NET Lambda Project Now, modify your C#.NET project to avoid including ABC.dll in its build output.

  1. Reference the DLL Conditionally: In your .csproj file, reference ABC.dll but set it to not copy to the output directory. Assuming you have the DLL locally for development, your reference might look like this:
<ItemGroup>
  <Reference Include="ABC">
    <HintPath>path\to\ABC.dll</HintPath>
    <Private>false</Private> <!-- This prevents the DLL from being included in the output -->
  </Reference>
</ItemGroup>

Setting <Private>false</Private> ensures the DLL is used during development but not packaged into the deployment.

  1. Build Your Project: Build your project as usual. Confirm that ABC.dll is not in the build output directory.

Deploy Your Lambda Function

After configuring the layer and the project:

  1. Assign the Layer to Your Lambda Function: When creating or updating your Lambda function, specify the layer that includes ABC.dll. This can be done in the AWS Management Console or using the AWS CLI. For example:
aws lambda update-function-configuration --function-name MyFunction --layers arn:aws:lambda:region:account-id:layer:MyDotNetLayer:version-number

  1. Deploy Your Function: Deploy your Lambda function package without ABC.dll. Lambda will use the version of ABC.dll provided in the layer.
profile picture
EXPERT
answered 11 days ago
profile picture
EXPERT
Artem
reviewed 10 days ago
  • Thanks for the answer. If the lib/packages are added in PackageReference tag instaed of Reference tag then reference should like this <ItemGroup> <PackageReference Include="ABC" Version="x.x.x" ExcludeAssets="runtime" /> </ItemGroup>

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions