diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/blueprint-manifest.json b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/blueprint-manifest.json new file mode 100644 index 000000000..b527e15b1 --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/blueprint-manifest.json @@ -0,0 +1,8 @@ +{ + "display-name":"Simple SNS Function", + "system-name":"SimpleSNSFunction", + "description": "A project for responding to SNS Event notifications", + "sort-order" : 101, + "hidden-tags" : ["C#","LambdaProject"], + "tags":["SNS", "Simple"] +} \ No newline at end of file diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/.template.config/template.json b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/.template.config/template.json new file mode 100644 index 000000000..1b2a5a63b --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/.template.config/template.json @@ -0,0 +1,40 @@ +{ + "author": "AWS", + "classifications": ["AWS", "Lambda", "Function"], + "name": "Lambda Simple SNS Function", + "identity": "AWS.Lambda.Simple.SNS.CSharp", + "groupIdentity": "AWS.Lambda.Simple.SNS", + "shortName": "lambda.SNS", + "tags": { + "language": "C#", + "type": "project" + }, + "sourceName": "BlueprintBaseName.1", + "preferNameDirectory": true, + "symbols": { + "profile": { + "type": "parameter", + "description" : "The AWS credentials profile set in aws-lambda-tools-defaults.json and used as the default profile when interacting with AWS.", + "datatype": "string", + "replaces" : "DefaultProfile", + "defaultValue": "" + }, + "region": { + "type": "parameter", + "description" : "The AWS region set in aws-lambda-tools-defaults.json and used as the default region when interacting with AWS.", + "datatype": "string", + "replaces" : "DefaultRegion", + "defaultValue": "" + }, + "safe-sourcename": { + "type": "generated", + "generator": "coalesce", + "parameters": { + "sourceVariableName": "safe_namespace", + "fallbackVariableName": "safe_name" + }, + "replaces": "BlueprintBaseName._1" + } + }, + "primaryOutputs": [ { "path": "./src/BlueprintBaseName.1/BlueprintBaseName.1.csproj" } ] +} diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/BlueprintBaseName.1.csproj b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/BlueprintBaseName.1.csproj new file mode 100644 index 000000000..b105e2edf --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/BlueprintBaseName.1.csproj @@ -0,0 +1,12 @@ + + + netcoreapp2.1 + true + Lambda + + + + + + + \ No newline at end of file diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/Function.cs b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/Function.cs new file mode 100644 index 000000000..f2c53587b --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/Function.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Amazon.Lambda.Core; +using Amazon.Lambda.SNSEvents; + + +// 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 BlueprintBaseName._1 +{ + public class Function + { + /// + /// Default constructor. This constructor is used by Lambda to construct the instance. When invoked in a Lambda environment + /// the AWS credentials will come from the IAM role associated with the function and the AWS region will be set to the + /// region the Lambda function is executed in. + /// + public Function() + { + + } + + + /// + /// This method is called for every Lambda invocation. This method takes in an SNS event object and can be used + /// to respond to SNS messages. + /// + /// + /// + /// + public async Task FunctionHandler(SNSEvent evnt, ILambdaContext context) + { + foreach(var record in evnt.Records) + { + await ProcessRecordAsync(record, context); + } + } + + private async Task ProcessRecordAsync(SNSEvent.SNSRecord record, ILambdaContext context) + { + context.Logger.LogLine($"Processed record {record.Sns.Message}"); + + // TODO: Do interesting work based on the new message + await Task.CompletedTask; + } + } +} diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/Readme.md b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/Readme.md new file mode 100644 index 000000000..e6d6137bb --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/Readme.md @@ -0,0 +1,51 @@ +# AWS Lambda Simple SNS Function Project + +This starter project consists of: +* Function.cs - class file containing a class with a single function handler method +* aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS + +You may also have a test project depending on the options selected. + +The generated function handler responds to events on an Amazon SNS service. + +After deploying your function you must configure an Amazon SNS service as an event source to trigger your Lambda function. + +## Here are some steps to follow from Visual Studio: + +To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*. + +To view your deployed function open its Function View window by double-clicking the function name shown beneath the AWS Lambda node in the AWS Explorer tree. + +To perform testing against your deployed function use the Test Invoke tab in the opened Function View window. + +To configure event sources for your deployed function use the Event Sources tab in the opened Function View window. + +To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window. + +To view execution logs of invocations of your function use the Logs tab in the opened Function View window. + +## Here are some steps to follow to get started from the command line: + +Once you have edited your template and code you can deploy your application using the [Amazon.Lambda.Tools Global Tool](https://github.com/aws/aws-extensions-for-dotnet-cli#aws-lambda-amazonlambdatools) from the command line. + +Install Amazon.Lambda.Tools Global Tools if not already installed. +``` + dotnet tool install -g Amazon.Lambda.Tools +``` + +If already installed check if new version is available. +``` + dotnet tool update -g Amazon.Lambda.Tools +``` + +Execute unit tests +``` + cd "BlueprintBaseName.1/test/BlueprintBaseName.1.Tests" + dotnet test +``` + +Deploy function to AWS Lambda +``` + cd "BlueprintBaseName.1/src/BlueprintBaseName.1" + dotnet lambda deploy-function +``` diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/aws-lambda-tools-defaults.json b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/aws-lambda-tools-defaults.json new file mode 100644 index 000000000..29803d938 --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/src/BlueprintBaseName.1/aws-lambda-tools-defaults.json @@ -0,0 +1,19 @@ +{ + "Information": [ + "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", + "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", + + "dotnet lambda help", + + "All the command line options for the Lambda command can be specified in this file." + ], + + "profile":"DefaultProfile", + "region" : "DefaultRegion", + "configuration": "Release", + "framework": "netcoreapp2.0", + "function-runtime": "dotnetcore2.0", + "function-memory-size": 256, + "function-timeout": 30, + "function-handler": "BlueprintBaseName.1::BlueprintBaseName._1.Function::FunctionHandler" +} diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/test/BlueprintBaseName.1.Tests/BlueprintBaseName.1.Tests.csproj b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/test/BlueprintBaseName.1.Tests/BlueprintBaseName.1.Tests.csproj new file mode 100644 index 000000000..2b16f3df4 --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/test/BlueprintBaseName.1.Tests/BlueprintBaseName.1.Tests.csproj @@ -0,0 +1,16 @@ + + + netcoreapp2.1 + + + + + + + + + + + + + \ No newline at end of file diff --git a/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/test/BlueprintBaseName.1.Tests/FunctionTest.cs b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/test/BlueprintBaseName.1.Tests/FunctionTest.cs new file mode 100644 index 000000000..b4d4e03bf --- /dev/null +++ b/Blueprints/BlueprintDefinitions/Msbuild-NETCore_2_1/SimpleSNSFunction/template/test/BlueprintBaseName.1.Tests/FunctionTest.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Xunit; +using Amazon.Lambda.TestUtilities; +using Amazon.Lambda.SNSEvents; + +using BlueprintBaseName._1; + +namespace BlueprintBaseName._1.Tests +{ + public class FunctionTest + { + [Fact] + public async Task TestSQSEventLambdaFunction() + { + var snsEvent = new SNSEvent + { + Records = new List + { + new SNSEvent.SNSRecord + { + Sns = new SNSEvent.SNSMessage() + { + Message = "foobar" + } + } + } + }; + + var logger = new TestLambdaLogger(); + var context = new TestLambdaContext + { + Logger = logger + }; + + var function = new Function(); + await function.FunctionHandler(snsEvent, context); + + Assert.Contains("Processed record foobar", logger.Buffer.ToString()); + } + } +}