Setup
Problem
I'm having difficulty understanding how to manage Lambda Functions in a dotnet core 2.0 project
Current Approach (not implemented just how I think it could work)
Terraform Lambda Function Example
resource "aws_lambda_function" "this" {
function_name = "test_function"
role = "lambda_exec_role"
s3_bucket = "my_bucket"
s3_key = "object_key/package.zip"
handler = "MyApp::Example.Hello::MyHandler"
runtime = "dotnetcore2.0"
}
This approach means that if I change a single function in a project, I have to upload the entire code base to S3 which doesn't feel like a clean way to handle code changes.
Alternative Approach
dotnet lambda deploy-function
This approach feel cleaner from a Lambda code version management perspective, but it means that I'm no longer using Terraform to manage my Lambda Functions.
I've used NodeJs and Go to create Lambda functions before, each seems more lightweight than the dotnet approach (in that it's easier to de-couple each functions source code).
Question
Do either of these setups appear optimal?
You can now use the . NET 6 runtime to build AWS Lambda functions. The new managed runtime supports both x86 and Arm/Graviton2 processors. You can get started with .
SAM CLI can read the infrastructure resource information from the Terraform project and start Lambda functions locally in a docker container to invoke with an event payload, or attach a debugger using AWS toolkits on IDE to step through the Lambda function code. This feature is supported with Terraform version 1.1 +.
I'm aware the question was asked around a year ago from this response, so I don't know how much everything has changed since then, but this is what has worked for me:
I started using the dotnet
CLI Lambda tools, just like you suggested, and it works just fine. It works out of the box and it requires minimum configuration. The problem I encountered is that I needed to set up some specific configuration which Cloudformation didn't allow. That's when I started leveraging Terraform. After some digging, I decided to go with Terraform because it fixed this issue.
Now, you mentioned how the pitfall of using Terraform is that you have to upload the whole code to S3... but I have found that the dotnet
CLI tools do exactly the same. If you checkout the output of executing dotnet lambda deploy-function
you will see:
Zipping publish folder
... zipping: some.dll
... zipping: another.dll
Created publish archive (---)
Uploading to S3. (Bucket: ---)
... Progress: 11%
... Progress: 55%
... Progress: 100%
Creating new Lambda function some_lambda
So, in a nutshell, I've decided to stick to Terraform and simply elaborate a custom shell script which first runs dotnet restore
, then dotnet build
and lastly terraform apply
. And that's all I need to deploy my application to AWS. I find this a more customisable approach than using Serverless of Cloudformation with the dotnet CLI.
I hope that helped!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With