|
10 | 10 | using Amazon.Lambda.SQSEvents;
|
11 | 11 | using Amazon.XRay.Recorder.Core;
|
12 | 12 | using Amazon.XRay.Recorder.Handlers.AwsSdk;
|
13 |
| -using Newtonsoft.Json; |
14 |
| -using Newtonsoft.Json.Serialization; |
| 13 | +using System.IO; |
15 | 14 |
|
16 | 15 | [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
|
17 | 16 |
|
18 | 17 | namespace blankCsharp
|
19 | 18 | {
|
20 | 19 | public class Function
|
21 | 20 | {
|
22 |
| - private static AmazonLambdaClient lambdaClient; |
23 |
| - |
24 |
| - static Function() { |
25 |
| - initialize(); |
26 |
| - } |
27 |
| - |
28 |
| - static async void initialize() { |
29 |
| - AWSSDKHandler.RegisterXRayForAllServices(); |
30 |
| - lambdaClient = new AmazonLambdaClient(); |
31 |
| - await callLambda(); |
32 |
| - } |
33 |
| - |
34 |
| - public async Task<AccountUsage> FunctionHandler(SQSEvent invocationEvent, ILambdaContext context) |
35 |
| - { |
36 |
| - GetAccountSettingsResponse accountSettings; |
37 |
| - try |
38 |
| - { |
39 |
| - accountSettings = await callLambda(); |
40 |
| - } |
41 |
| - catch (AmazonLambdaException ex) |
42 |
| - { |
43 |
| - throw ex; |
44 |
| - } |
45 |
| - AccountUsage accountUsage = accountSettings.AccountUsage; |
46 |
| - LambdaLogger.Log("ENVIRONMENT VARIABLES: " + JsonConvert.SerializeObject(System.Environment.GetEnvironmentVariables())); |
47 |
| - LambdaLogger.Log("CONTEXT: " + JsonConvert.SerializeObject(context)); |
48 |
| - LambdaLogger.Log("EVENT: " + JsonConvert.SerializeObject(invocationEvent)); |
49 |
| - return accountUsage; |
50 |
| - } |
51 |
| - |
52 |
| - public static async Task<GetAccountSettingsResponse> callLambda() |
53 |
| - { |
54 |
| - var request = new GetAccountSettingsRequest(); |
55 |
| - var response = await lambdaClient.GetAccountSettingsAsync(request); |
56 |
| - return response; |
57 |
| - } |
| 21 | + private AmazonLambdaClient lambdaClient; |
| 22 | + |
| 23 | + public Function() |
| 24 | + { |
| 25 | + initialize(); |
| 26 | + } |
| 27 | + |
| 28 | + async void initialize() |
| 29 | + { |
| 30 | + AWSSDKHandler.RegisterXRayForAllServices(); |
| 31 | + lambdaClient = new AmazonLambdaClient(); |
| 32 | + await callLambda(); |
| 33 | + } |
| 34 | + |
| 35 | + public async Task<AccountUsage> FunctionHandler(SQSEvent invocationEvent, ILambdaContext context) |
| 36 | + { |
| 37 | + GetAccountSettingsResponse accountSettings; |
| 38 | + try |
| 39 | + { |
| 40 | + accountSettings = await callLambda(); |
| 41 | + } |
| 42 | + catch (AmazonLambdaException ex) |
| 43 | + { |
| 44 | + throw ex; |
| 45 | + } |
| 46 | + |
| 47 | + AccountUsage accountUsage = accountSettings.AccountUsage; |
| 48 | + MemoryStream logData = new MemoryStream(); |
| 49 | + StreamReader logDataReader = new StreamReader(logData); |
| 50 | + |
| 51 | + Amazon.Lambda.Serialization.Json.JsonSerializer serializer = new Amazon.Lambda.Serialization.Json.JsonSerializer(); |
| 52 | + |
| 53 | + serializer.Serialize<System.Collections.IDictionary>(System.Environment.GetEnvironmentVariables(), logData); |
| 54 | + LambdaLogger.Log("ENVIRONMENT VARIABLES: " + logDataReader.ReadLine()); |
| 55 | + logData.Position = 0; |
| 56 | + serializer.Serialize<ILambdaContext>(context, logData); |
| 57 | + LambdaLogger.Log("CONTEXT: " + logDataReader.ReadLine()); |
| 58 | + logData.Position = 0; |
| 59 | + serializer.Serialize<SQSEvent>(invocationEvent, logData); |
| 60 | + LambdaLogger.Log("EVENT: " + logDataReader.ReadLine()); |
| 61 | + |
| 62 | + return accountUsage; |
| 63 | + } |
| 64 | + |
| 65 | + public async Task<GetAccountSettingsResponse> callLambda() |
| 66 | + { |
| 67 | + var request = new GetAccountSettingsRequest(); |
| 68 | + var response = await lambdaClient.GetAccountSettingsAsync(request); |
| 69 | + return response; |
| 70 | + } |
58 | 71 | }
|
59 | 72 | }
|
0 commit comments