Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sample-apps/blank-csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The project source includes function code and supporting resources:
Use the following instructions to deploy the sample application. For more information on the application's architecture and implementation, see [Managing Spot Instance Requests](https://docs.aws.amazon.com/lambda/latest/dg/services-ec2-tutorial.html) in the developer guide.

# Requirements
- [.NET Core SDK 3.1](https://dotnet.microsoft.com/download/dotnet-core/3.1)
- [.NET Core SDK 6.0](https://dotnet.microsoft.com/download/dotnet-core/6.0)
- [AWS extensions for .NET CLI](https://github.com/aws/aws-extensions-for-dotnet-cli)
- The Bash shell. For Linux and macOS, this is included by default. In Windows 10, you can install the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) to get a Windows-integrated version of Ubuntu and Bash.
- [The AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) v1.17 or newer.
Expand Down
89 changes: 51 additions & 38 deletions sample-apps/blank-csharp/src/blank-csharp/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,63 @@
using Amazon.Lambda.SQSEvents;
using Amazon.XRay.Recorder.Core;
using Amazon.XRay.Recorder.Handlers.AwsSdk;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.IO;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace blankCsharp
{
public class Function
{
private static AmazonLambdaClient lambdaClient;

static Function() {
initialize();
}

static async void initialize() {
AWSSDKHandler.RegisterXRayForAllServices();
lambdaClient = new AmazonLambdaClient();
await callLambda();
}

public async Task<AccountUsage> FunctionHandler(SQSEvent invocationEvent, ILambdaContext context)
{
GetAccountSettingsResponse accountSettings;
try
{
accountSettings = await callLambda();
}
catch (AmazonLambdaException ex)
{
throw ex;
}
AccountUsage accountUsage = accountSettings.AccountUsage;
LambdaLogger.Log("ENVIRONMENT VARIABLES: " + JsonConvert.SerializeObject(System.Environment.GetEnvironmentVariables()));
LambdaLogger.Log("CONTEXT: " + JsonConvert.SerializeObject(context));
LambdaLogger.Log("EVENT: " + JsonConvert.SerializeObject(invocationEvent));
return accountUsage;
}

public static async Task<GetAccountSettingsResponse> callLambda()
{
var request = new GetAccountSettingsRequest();
var response = await lambdaClient.GetAccountSettingsAsync(request);
return response;
}
private AmazonLambdaClient lambdaClient;

public Function()
{
initialize();
}

async void initialize()
{
AWSSDKHandler.RegisterXRayForAllServices();
lambdaClient = new AmazonLambdaClient();
await callLambda();
}

public async Task<AccountUsage> FunctionHandler(SQSEvent invocationEvent, ILambdaContext context)
{
GetAccountSettingsResponse accountSettings;
try
{
accountSettings = await callLambda();
}
catch (AmazonLambdaException ex)
{
throw ex;
}

AccountUsage accountUsage = accountSettings.AccountUsage;
MemoryStream logData = new MemoryStream();
StreamReader logDataReader = new StreamReader(logData);

Amazon.Lambda.Serialization.Json.JsonSerializer serializer = new Amazon.Lambda.Serialization.Json.JsonSerializer();

serializer.Serialize<System.Collections.IDictionary>(System.Environment.GetEnvironmentVariables(), logData);
LambdaLogger.Log("ENVIRONMENT VARIABLES: " + logDataReader.ReadLine());
logData.Position = 0;
serializer.Serialize<ILambdaContext>(context, logData);
LambdaLogger.Log("CONTEXT: " + logDataReader.ReadLine());
logData.Position = 0;
serializer.Serialize<SQSEvent>(invocationEvent, logData);
LambdaLogger.Log("EVENT: " + logDataReader.ReadLine());

return accountUsage;
}

public async Task<GetAccountSettingsResponse> callLambda()
{
var request = new GetAccountSettingsRequest();
var response = await lambdaClient.GetAccountSettingsAsync(request);
return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"profile":"default",
"region" : "us-east-2",
"configuration" : "Release",
"framework" : "netcoreapp3.1",
"function-runtime":"dotnetcore3.1",
"framework" : "net6.0",
"function-runtime":"dotnet6",
"function-memory-size" : 512,
"function-timeout" : 30,
"function-handler" : "blank-csharp::blankCsharp.Function::FunctionHandler"
Expand Down
17 changes: 8 additions & 9 deletions sample-apps/blank-csharp/src/blank-csharp/blank-csharp.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.7.0" />
<PackageReference Include="AWSSDK.Core" Version="3.3.104.38" />
<PackageReference Include="AWSSDK.Lambda" Version="3.3.108.11" />
<PackageReference Include="AWSXRayRecorder.Core" Version="2.6.2" />
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.7.2" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.0" />
<PackageReference Include="AWSSDK.Core" Version="3.7.103.24" />
<PackageReference Include="AWSSDK.Lambda" Version="3.7.104.3" />
<PackageReference Include="AWSXRayRecorder.Core" Version="2.13.0" />
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.11.0" />
</ItemGroup>
</Project>