A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.aws.amazon.com/lambda/latest/dg/csharp-package-cli.html below:

Using the .NET Lambda Global CLI

Using the .NET Lambda Global CLI

The .NET CLI and the .NET Lambda Global Tools extension (Amazon.Lambda.Tools) offer a cross-platform way to create .NET-based Lambda applications, package them, and deploy them to Lambda. In this section, you learn how to create new Lambda .NET projects using the .NET CLI and Amazon Lambda templates, and to package and deploy them using Amazon.Lambda.Tools

Prerequisites
.NET 8 SDK

If you haven't already done so, install the .NET 8 SDK and Runtime.

AWS Amazon.Lambda.Templates .NET project templates

To generate your Lambda function code, use the Amazon.Lambda.Templates NuGet package. To install this template package, run the following command:

dotnet new install Amazon.Lambda.Templates
AWS Amazon.Lambda.Tools .NET Global CLI tools

To create your Lambda functions, you use the Amazon.Lambda.Tools .NET Global Tools extension. To install Amazon.Lambda.Tools, run the following command:

dotnet tool install -g Amazon.Lambda.Tools

For more information about the Amazon.Lambda.Tools .NET CLI extension, see the AWS Extensions for .NET CLI repository on GitHub.

Creating .NET projects using the .NET CLI

In the .NET CLI, you use the dotnet new command to create .NET projects from the command line. Lambda offers additional templates using the Amazon.Lambda.Templates NuGet package.

After installing this package, run the following command to see a list of the available templates.

dotnet new list

To examine details about a template, use the help option. For example, to see details about the lambda.EmptyFunction template, run the following command.

dotnet new lambda.EmptyFunction --help

To create a basic template for a .NET Lambda function, use the lambda.EmptyFunction template. This creates a simple function that takes a string as input and converts it to upper case using the ToUpper method. This template supports the following options:

In this example, we create a new empty function named myDotnetFunction using the default profile and AWS Region settings:

dotnet new lambda.EmptyFunction --name myDotnetFunction

This command creates the following files and directories in your project directory.

└── myDotnetFunction
    ├── src
    │   └── myDotnetFunction
    │       ├── Function.cs
    │       ├── Readme.md
    │       ├── aws-lambda-tools-defaults.json
    │       └── myDotnetFunction.csproj
    └── test
        └── myDotnetFunction.Tests
            ├── FunctionTest.cs
            └── myDotnetFunction.Tests.csproj

Under the src/myDotnetFunction directory, examine the following files:

Under the myfunction/test directory, examine the following files:

Deploying .NET projects using the .NET CLI

To build your deployment package and deploy it to Lambda, you use the Amazon.Lambda.Tools CLI tools. To deploy your function from the files you created in the previous steps, first navigate into the folder containing your function's .csproj file.

cd myDotnetFunction/src/myDotnetFunction

To deploy your code to Lambda as a .zip deployment package, run the following command. Choose your own function name.

dotnet lambda deploy-function myDotnetFunction

During the deployment, the wizard asks you to select a Defining Lambda function permissions with an execution role. For this example, select the lambda_basic_role.

After you have deployed your function, you can test it in the cloud using the dotnet lambda invoke-function command. For the example code in the lambda.EmptyFunction template, you can test your function by passing in a string using the --payload option.

dotnet lambda invoke-function myDotnetFunction --payload "Just checking if everything is OK"

If your function has been successfully deployed, you should see output similar to the following.

dotnet lambda invoke-function myDotnetFunction --payload "Just checking if everything is OK"
Amazon Lambda Tools for .NET Core applications (5.8.0)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Payload:
"JUST CHECKING IF EVERYTHING IS OK"

Log Tail:
START RequestId: id Version: $LATEST
END RequestId: id
REPORT RequestId: id  Duration: 0.99 ms       Billed Duration: 1 ms         Memory Size: 256 MB     Max Memory Used: 12 MB
Using Lambda layers with the .NET CLI

Note

While it's possible to use layers with functions in .NET, we recommend against it. Functions in .NET that use layers manually load the shared assemblies into memory during the Init phase, which can increase cold start times. Instead, include all shared code at compile time to take advantage of the built-in optimizations of the .NET compiler.

The .NET CLI supports commands to help you publish layers and deploy C# functions that consume layers. To publish a layer to a specified Amazon S3 bucket, run the following command in the same directory as your .csproj file:

dotnet lambda publish-layer <layer_name> --layer-type runtime-package-store --s3-bucket <s3_bucket_name>

Then, when you deploy your function using the .NET CLI, specify the layer ARN the consume in the following command:

dotnet lambda deploy-function <function_name> --function-layers arn:aws:lambda:us-east-1:123456789012:layer:layer-name:1

For a complete example of a Hello World function, see the blank-csharp-with-layer sample.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4