Lambdas, SAM and Dotnet
Tags: lambdas, dotnet, sam
Published: 2020-03-18 13:02:23

Recently, had a chance encounter with lambdas and dotnet. [AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) provides a pretty good hello-world for dotnet application. So, I gave that a chance and below are some of my findings.

## Table of Contents
* [Dotnet SDK](#dotnet-sdk)
* [Dotnet Tools](#aws-dotnet-tools)
* [SAM Build](#sam-build)
* [Dotnet Publish](#dotnet-publish)
* [Dotnet Lambda](#dotnet-lambda)

### Dotnet SDK <a name="dotnet-sdk"></a>
Currently, SAM works with dotnetcore2.0 and dotnetcore2.1. And the latest available version of [dotnet SDK](https://dotnet.microsoft.com/download/dotnet-core) is 3.1. In order to work with the hello-world example of AWS SAM with dotnet, you would need at least sdk version 2.1 installed on your machine (I was able to make it work with sdk 3.1 too).

### Dotnet Tools <a name="aws-dotnet-tools"></a>
You might also want to install [aws dotnet tools](https://github.com/aws/aws-lambda-dotnet). These are required by sam to create, build and deploy the lambda functions. If you don't have them installed, then you can do so by
```dotnet tool install -g Amazon.Lambda.Tools```
If you get an error that dotnet not found, then you might want to check the location `/usr/local/share/dotnet/dotnet`. This needs to have symlink to `/usr/local/bin`. In order to do that, just run 
`ln -s /usr/local/share/dotnet/dotnet /usr/local/bin`

While running `sam build`, you might run into the following issue:
```
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-xyz does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
```
To resolve it, add the following `$HOME/.dotnet/tools` to your PATH variable. This should do it, `export PATH=$PATH:$HOME/.dotnet/tools`

### SAM Build <a name="sam-build"></a>
Now when you run `sam build`, it will build the code and put the compiled files into `.aws-sam/build` directory and will also create the package to deploy to aws lambda via `sam deploy`. If all of your setup is correct, then you can either invoke this lambda locally via `sam local invoke` OR deploy it to AWS via `sam deploy -g`.

### Dotnet Publish <a name="dotnet-publish"></a>
If you don't want to run the `sam build` and have some more control over how you build your code, you can do that via `dotnet publish`. With this you can provide more options like runtime, configuration etc for the build. Also you can specify an alternate location for the output files. Only other thing you would need to do is that you will have to manually create the zip file to deploy to lambda. You can do that by using any compression tool (`zip` for unix, for eg.).

### Dotnet Lambda <a name="dotnet-lambda"></a>
Another way you can do is via `dotnet lambda`. This feature is available via [Dotnet Tools](#aws-dotnet-tools). Using this, you can combine the build and package steps together. This creates the deployable package which you can either deploy to lambda manually or via the tool itself (`dotnet lambda deploy-function`).

These were some of my findings and being an alien to dotnet universe, these things were very frustrating and tricky to resolve. So, thought of putting it all together in one place.

Sample code can be found here {% github vikasgarghb/dotnet-sam %}.