« Lambda » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 34 : Ligne 34 :
== [https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html Lambda function handler in C#] ==
== [https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html Lambda function handler in C#] ==
For class libraries: {{boxx|ASSEMBLY::TYPE::METHOD}}
For class libraries: {{boxx|ASSEMBLY::TYPE::METHOD}}
= Call a lambda from code =
<kode lang='cs'>
var jsonSerializerOptions = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    Converters = { new JsonStringEnumConverter() }
};
var amazonLambdaClient = new AmazonLambdaClient();
var request = new InvokeRequest
{
    FunctionName = functionName,
    Payload = JsonSerializer.Serialize(myObject, jsonSerializerOptions),
    LogType = LogType.Tail
};
var response = await this.amazonLambdaClient.InvokeAsync(request);
if(response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
    var payload = Encoding.ASCII.GetString(response.Payload.ToArray()); // to debug only
    var result = JsonSerializer.Deserialize<AwsJobResult<LambdaJob>>(response.Payload, jsonSerializerOptions);
}
</kode>

Version du 4 avril 2024 à 12:44

Links

.NET 8

scenario description
Native AOT
Custom Runtime Function to use .NET 8
Container Image lambda function package as a container image

Debug locally

On VS, if the extension AWS Toolkit is installed you have the AWS .NET Mock Lambda Test Tool available.
A debug configuration is created for you lambda.

Properties\launchSettings.json
{
  "profiles": {
    "Mock Lambda Test Tool": {
      "commandName": "Executable",
      "commandLineArgs": "--port 5050",
      "executablePath": "<home-directory>\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe",
      "workingDirectory": ".\\bin\\Debug\\net6.0"
    }
  }
}

Deployment

Lambda function handler in C#

For class libraries: ASSEMBLY::TYPE::METHOD

Call a lambda from code

Cs.svg
var jsonSerializerOptions = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    Converters = { new JsonStringEnumConverter() }
};

var amazonLambdaClient = new AmazonLambdaClient();

var request = new InvokeRequest
{
    FunctionName = functionName,
    Payload = JsonSerializer.Serialize(myObject, jsonSerializerOptions),
    LogType = LogType.Tail
};
var response = await this.amazonLambdaClient.InvokeAsync(request);

if(response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
    var payload = Encoding.ASCII.GetString(response.Payload.ToArray()); // to debug only
    var result = JsonSerializer.Deserialize<AwsJobResult<LambdaJob>>(response.Payload, jsonSerializerOptions);
}