Starter template for serverless framework with following scope:
- deploy C# / NET 6 solution in AWS cloud using:
- Lambda
- Api Gateway
- debug and test solution locally in Visual Studio Code
- works operating system independent
- NodeJS
- Serverless Framework CLI
- .NET Core 6
- AWS-Lambda-DotNet
- Visual Studio Code
- C# Extension for Visual Studio Code
Verify that everything is installed (copy & paste)
# package manager for nodejs
npm -v
# serverless framework cli > 1.5
sls -v
# dotnet (cli) > 6.0
dotnet --version
# clone solution
# serverless create --template-url https://github.com/pharindoko/serverlessDotNetStarter --path {SERVICE_NAME}
serverless create --template-url https://github.com/pharindoko/serverlessDotNetStarter --path serverlessDotNetStarter
cd serverlessDotNetStarter
# restore / install dotnet references described in csproj file
dotnet restore AwsDotnetCsharp.csproj
# install Lambda NET Mock Test Tool and Amazon Lambda Tools
# more details: https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool
dotnet tool install -g Amazon.Lambda.Tools
dotnet tool install --global Amazon.Lambda.TestTool-6.0
dotnet tool list -g
# required dotnet packages:
#
# Package Id Version Commands
------------------------------------------------------------------------
#amazon.lambda.testtool-6.0 0.12.4 dotnet-lambda-test-tool-6.0
#amazon.lambda.tools 5.4.5 dotnet-lambda
For VS Code Debugging:
code --install-extension ms-dotnettools.csharp --force
I followed this guideline: (Please read in case of issues)
How to Debug .NET Core Lambda Functions Locally with the Serverless Framework
# open Visual Studio Code
code .
Edit the "program" property in .vscode/launch.json file and update placeholder for {user} (placeholders marked in bold)
"program": /Users/{user}/.dotnet/tools/dotnet-lambda-test-tool-6.0
"program": /Users/{user}/.dotnet/tools/dotnet-lambda-test-tool-6.0
More information:
- https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool#configure-for-visual-studio-code,
- https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool#configure-for-visual-studio-for-mac
In case of issues - try this:
"program": /Users/{user}/.dotnet/tools/.store/amazon.lambda.testtool-6.0/{nuget-version}/amazon.lambda.testtool-6.0/{nuget-version}/tools/net6.0/any/Amazon.Lambda.TestTool.WebTester6.0.dll",
how to get the right nuget version ?
dotnet tool list -g
Result:
Package Id Version Commands
------------------------------------------------------------------------
amazon.lambda.testtool-6.0 e.g. version 0.12.4 dotnet-lambda-test-tool-6.0
- Hint: Lambda Mock Test Tool should be started locally on port 5050
- Click on Button "Execute Function"
you should get hello world as a result.
-
Select function to getquerystring (upper right dropdownlist)
-
Insert this json value in the function input textbox for a first test:
{ "httpMethod": "GET", "queryStringParameters": { "foo": "dfgdfg", "woot": "food" } }
Mind: For a successful response querystringParameter foo must be inserted
Mac OS or Linux
./build.sh
Windows
build.cmd
serverless deploy
A cloudformation stack in AWS will be created in background containing all needed resources
Service Information service: myService stage: dev region: us-east-1 stack: myService-dev resources: 10 api keys: None endpoints: GET - endpointUrl --> https://{api}.execute-api.us-east-1.amazonaws.com/dev/hello functions: hello: myService-dev-hello layers: None
2 simple options:
- Use postman as UI Tool
- Use curl
Use the endpointUrl from up above.
curl https://{api}.execute-api.us-east-1.amazonaws.com/dev/hello
curl https://{api}.execute-api.us-east-1.amazonaws.com/dev/getquerystring?foo=test
Mind: For a successful response of function getquerystring the querystringParameter foo must be inserted
- Setup API Key in serverless.yml file https://serverless.com/framework/docs/providers/aws/events/apigateway/#setting-api-keys-for-your-rest-api
- Create a new C# Function in Handler.cs or use another file
- Add a new function to serverless.yml and reference the C# Function as handler https://serverless.com/framework/docs/providers/aws/guide/functions/
sls remove
- Check Cloudwatch Logs in AWS - the issue should be describe there.
- For a successful response of function getquerystring the querystringParameter foo must be inserted
Please have a look to the serverless guideline: https://serverless.com/framework/docs/providers/aws/guide/deploying/