At the time of this writing (Jan 2017), the following components will need to be installed on your machine in this order. The machine I used had Windows 10 Home installed.
Each of these components are actively developed and released, so double check the links and versions and update this document when needed.
Each of these can take some time to install, so be patient, do one at a time to minimize mistakes.
Lastly, rebooting Windows before starting development is sad, but always a good idea after doing major development tool installations on a Windows box.
Open Function.cs
and replace the class code with the following:
public class Function
{
/// <summary>
/// A simple function that takes a birth date and returns Age in years
/// </summary>
/// <param name="input"></param>
/// <returns>Age is years</returns>
///
[LambdaSerializer(typeof(SimpleSerializer))]
public string FunctionHandler(Dictionary<string, int> input)
{
var defaultMessage = "Age could not be determined.";
var birthDate = new DateTime(input["year"], input["month"], input["day"]);
var ageInYears = DateTime.Today.Year - birthDate.Year;
if (birthDate.DayOfYear > DateTime.Today.DayOfYear)
ageInYears--;
defaultMessage = $"Age in years: {ageInYears}";
return defaultMessage;
}
}
You will need to add the following using statements near the top:
using System.Collections.Generic;
using Amazon.Lambda.Core;
SimpleSerializer.cs
using System;
using System.IO;
using Amazon.Lambda.Core;
using Newtonsoft.Json;
namespace AWSLambdaFunctionAgeInYears
{
public class SimpleSerializer : ILambdaSerializer
{
public T Deserialize<T>(Stream requestStream)
{
string text;
using (var reader = new StreamReader(requestStream))
text = reader.ReadToEnd();
try
{
return JsonConvert.DeserializeObject<T>(text);
}
catch (Exception ex)
{
if (typeof(T) == typeof(System.String))
return (T)Convert.ChangeType(text, typeof(T));
throw ex;
}
}
public void Serialize<T>(T response, Stream responseStream)
{
StreamWriter streamWriter = new StreamWriter(responseStream);
try
{
string text = JsonConvert.SerializeObject(response);
streamWriter.Write(text);
streamWriter.Flush();
}
catch (Exception ex)
{
if (typeof(T) == typeof(System.String))
{
streamWriter.Write(response);
streamWriter.Flush();
return;
}
throw ex;
}
}
}
}
In the Test Project, change line 23 of the FunctionTest.cs
to the following:
var upperCase = function.FunctionHandler(null);
Build your solution - you should have no build errors.
Right Click your project and select Publish to AWS Lambda...
The Upload to AWS Lambda screen will appear. Make sure the correct region is selected. Keep all of the defaults. Then only enter the Function Name: AWSLambdaFunctionAgeInYears and then click next.
On the next page, select AWSLambdaRole for the Role Name field. Click Upload and the function should upload without error.
View Function
window with your function loaded.{
"month": "10",
"day": "28",
"year": "1979"
}