Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a template/blueprint for SNS #457

Merged
merged 2 commits into from
Jun 19, 2019
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
Original file line number Diff line number Diff line change
@@ -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"]
}
Original file line number Diff line number Diff line change
@@ -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" } ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.5.0" />
<PackageReference Include="Amazon.Lambda.SNSEvents" Version="1.1.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
public Function()
{

}


/// <summary>
/// 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.
/// </summary>
/// <param name="evnt"></param>
/// <param name="context"></param>
/// <returns></returns>
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\BlueprintBaseName.1\BlueprintBaseName.1.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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<SNSEvent.SNSRecord>
{
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());
}
}
}