Deploying a .NET Core Lambda Project from the .NET Core CLI

In my previous post about "Creating a Lambda function in .NetCore with Visual Studio and AWS Visual Studio Toolkit  we learnt about how to create a lambda function in .net core and deploy it using Visual Studio Toolkit.

The deployment part can also be accomplished from command line using ".NET Core Global Tools for AWS". To install the .Net core global Lambda tools, use the following command in dotnet command line

dotnet tool install --global Amazon.Lambda.Tools --version 3.3.0

It will install the 3.3.0 (current version) of Lambda global tools. If you want to update the global tools you can use it as

dotnet tool update -g Amazon.Lambda.Tools

Deploy Lambda using Global Lambda Tools

Before going to deployment of lambda using Lambda global tools, first you need to understand about the "aws-lambda-tools-defaults.json" file. The file seems as below

{
  "Information": [
    "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI."
  ],

  "profile":"default",
  "region" : "us-east-1",
  "configuration": "Release",
  "framework": "netcoreapp2.1",
  "function-runtime": "dotnetcore2.1",
  "function-memory-size": 256,
  "function-timeout": 30,
  "function-handler": "Lambda::Lambda.Function::FunctionHandler",
  "function-name": "LambdaTest",
  "function-role": "arn:aws:iam::XXXXXXXXXXXX:role/service-role/lambda_basic_execution",
  "environment-variables" : "\"SourceQueue\"=\"source-queue\";\"TargetQueue\"=\"target-queue\";\"ErrorSns\"=\"my-error-sns\""
}
This file has a role for both manual and automated deployments. When you publish with AWS Visual Studio toolkit, the fields are pre-populated with the values from this file.When you use the command line interface, it will use the values specified in this file unless we override those in the command 

Following are some important settings

1) function-handler: This is how you tell AWS Lambda which method to invoke.  It is made up of 3 parts:

  a) Assembly / Namespace name (May be the same as the assembly name if your project isn't complex enough for a namespace heirarchy.)
  b) Class name along with Namespace
  c) Method name

2) function-name: Name of the lambda function in AWS
3) function-role: IAM role arn for executing Lambda.
4) environment-variables: Environment variables which needs to execute the lambda.

Once the settings are correctly placed, open the dotnet command line, navigate to the project folder and run the following command

Note: Your should install & configure the AWS CLI [https://aws.amazon.com/cli/] before running the below commands 

dotnet-lambda deploy-function 
Note: We can use either "dotnet-lambda" or "dotnet lambda"

It will deploy the lambda function to your account

Now you can found the lambda function created in your AWS account using AWS Console

The values are set automatically as we mentioned in the "aws-lambda-tools-defaults.json"


Overriding "aws-lambda-tools-defaults.json" settings

We can override the settings mentioned in "aws-lambda-tools-defaults.json" file while deploying the application by providing some extra parameters to the deploy-function command
The below command will create a function with "LambdaTest2" and 4 environment-variables
dotnet-lambda deploy-function -fn "LambdaTest2" -ev "SourceQueue=sourcequeue;TargetQueue=targetqueue;ErrorSns=myerrorsns;MiddleQueue=middlequeue"
It will create new function in the AWS with name "LambdaTest2" with 4 environment varaiables


You can check the remaining parameters accepted by deploy-function by using command
dotnet-lambda deploy-function --help

You can find more here.

Happy coding 😊!

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment