forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.cs
81 lines (66 loc) · 2.89 KB
/
Functions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Newtonsoft.Json;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace app
{
public class Counter
{
public string Id { get; set; }
public Int32 Count { get; set; }
}
public class Functions
{
// This const is the name of the environment variable that the serverless.template will use to set
// the name of the DynamoDB table used to store blog posts.
const string TABLENAME_ENVIRONMENT_VARIABLE_LOOKUP = "COUNTER_TABLE";
IDynamoDBContext DDBContext { get; set; }
/// <summary>
/// Default constructor that Lambda will invoke.
/// </summary>
public Functions()
{
// Check to see if a table name was passed in through environment variables and if so
// add the table mapping.
var tableName = System.Environment.GetEnvironmentVariable(TABLENAME_ENVIRONMENT_VARIABLE_LOOKUP);
if(!string.IsNullOrEmpty(tableName))
{
AWSConfigsDynamoDB.Context.TypeMappings[typeof(Counter)] = new Amazon.Util.TypeMapping(typeof(Counter), tableName);
}
var config = new DynamoDBContextConfig { Conversion = DynamoDBEntryConversion.V2 };
this.DDBContext = new DynamoDBContext(new AmazonDynamoDBClient(), config);
}
/// <summary>
/// A Lambda function that returns a simple JSON object.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<APIGatewayProxyResponse> GetAsync(APIGatewayProxyRequest request, ILambdaContext context)
{
context.Logger.LogLine($"Getting count for '{request.Path}'");
var counter = await DDBContext.LoadAsync<Counter>(request.Path);
if (counter == null) {
counter = new Counter { Id = request.Path, Count = 1 };
}
var count = counter.Count++;
await DDBContext.SaveAsync<Counter>(counter);
context.Logger.LogLine($"Got count {count} for '{request.Path}'");
var response = new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = JsonConvert.SerializeObject(new { Path = request.Path, Count = count }),
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
return response;
}
}
}