Skip to content

Commit 786e7d9

Browse files
committed
Add Amazon.Lambda.SQSEvents event package
1 parent 0486da6 commit 786e7d9

File tree

12 files changed

+285
-1
lines changed

12 files changed

+285
-1
lines changed

Libraries/Libraries.sln

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TestFunctionFSharp", "test\
6767
EndProject
6868
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFunctionCSharp", "test\TestFunctionFSharp\TestFunctionCSharp\TestFunctionCSharp.csproj", "{2810A66B-3A03-4889-A4D0-0E8838474A4A}"
6969
EndProject
70+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.SQSEvents", "src\Amazon.Lambda.SQSEvents\Amazon.Lambda.SQSEvents.csproj", "{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}"
71+
EndProject
7072
Global
7173
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7274
Debug|Any CPU = Debug|Any CPU
@@ -177,6 +179,10 @@ Global
177179
{2810A66B-3A03-4889-A4D0-0E8838474A4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
178180
{2810A66B-3A03-4889-A4D0-0E8838474A4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
179181
{2810A66B-3A03-4889-A4D0-0E8838474A4A}.Release|Any CPU.Build.0 = Release|Any CPU
182+
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183+
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
184+
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
185+
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F}.Release|Any CPU.Build.0 = Release|Any CPU
180186
EndGlobalSection
181187
GlobalSection(SolutionProperties) = preSolution
182188
HideSolutionNode = FALSE
@@ -209,6 +215,7 @@ Global
209215
{8C9EA16D-6055-421B-ABE7-1B0E35259DFC} = {BB021F02-D90F-469C-8D56-919771B7F4DE}
210216
{CB38FA21-5814-4573-94C7-3672D61BC8B8} = {BB021F02-D90F-469C-8D56-919771B7F4DE}
211217
{2810A66B-3A03-4889-A4D0-0E8838474A4A} = {BB021F02-D90F-469C-8D56-919771B7F4DE}
218+
{74BFF877-D276-43BF-AC3A-E65BF26E4E2F} = {AAB54E74-20B1-42ED-BC3D-CE9F7BC7FD12}
212219
EndGlobalSection
213220
GlobalSection(ExtensibilityGlobals) = postSolution
214221
SolutionGuid = {503678A4-B8D1-4486-8915-405A3E9CF0EB}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\..\..\buildtools\common.props" />
4+
5+
<PropertyGroup>
6+
<Description>Amazon Lambda .NET Core support - SQSEvents package.</Description>
7+
<TargetFramework>netstandard1.3</TargetFramework>
8+
<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>
9+
<AssemblyTitle>Amazon.Lambda.SQSEvents</AssemblyTitle>
10+
<VersionPrefix>1.0.0</VersionPrefix>
11+
<AssemblyName>Amazon.Lambda.SQSEvents</AssemblyName>
12+
<PackageId>Amazon.Lambda.SQSEvents</PackageId>
13+
<PackageTags>AWS;Amazon;Lambda</PackageTags>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="System.Runtime" Version="4.1.0" />
18+
</ItemGroup>
19+
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
[assembly: AssemblyTitle("Amazon.Lambda.SNSEvents")]
6+
[assembly: AssemblyDescription("Lambda event interfaces for SNS event source.")]
7+
[assembly: AssemblyProduct("Amazon Web Services Lambda Interface for .NET")]
8+
[assembly: AssemblyCompany("Amazon.com, Inc")]
9+
[assembly: AssemblyCopyright("Copyright 2009-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.")]
10+
[assembly: ComVisible(false)]
11+
[assembly: System.CLSCompliant(true)]
12+
[assembly: AssemblyVersion("1.0")]
13+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Amazon.Lambda.SQSEvents
2+
3+
This package contains classes that can be used as input types for Lambda functions that process Amazon Simple Queue Service (Amazon SQS) events.
4+
5+
# Sample Function
6+
7+
Below is a sample class and Lambda function that illustrates how an SQSEvent can be used. The function logs a summary of the events it received, including the event source, the timestamp, and the message of each event. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)
8+
9+
```csharp
10+
public class Function
11+
{
12+
public string Handler(SQSEvent sqsEvent)
13+
{
14+
foreach (var record in sqsEvent.Records)
15+
{
16+
Console.WriteLine($"[{record.EventSource}] Body = {record.Body}");
17+
}
18+
}
19+
}
20+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
namespace Amazon.Lambda.SQSEvents
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
/// <summary>
8+
/// Simple Queue Service event
9+
/// </summary>
10+
public class SQSEvent
11+
{
12+
13+
/// <summary>
14+
/// Get and sets the Records
15+
/// </summary>
16+
public List<SQSMessage> Records { get; set; }
17+
18+
/// <summary>
19+
/// Class containing the data for message attributes
20+
/// </summary>
21+
public class MessageAttribute
22+
{
23+
/// <summary>
24+
/// Get and sets value of message attribute of type String or type Number
25+
/// </summary>
26+
public string StringValue { get; set; }
27+
28+
/// <summary>
29+
/// Get and sets value of message attribute of type Binary
30+
/// </summary>
31+
public MemoryStream BinaryValue { get; set; }
32+
33+
/// <summary>
34+
/// Get and sets the list of String values of message attribute
35+
/// </summary>
36+
public List<string> StringListValues { get; set; }
37+
38+
/// <summary>
39+
/// Get and sets the list of Binary values of message attribute
40+
/// </summary>
41+
public List<MemoryStream> BinaryListValues { get; set; }
42+
43+
/// <summary>
44+
/// Get and sets the dataType of message attribute
45+
/// </summary>
46+
public string DataType { get; set; }
47+
}
48+
49+
/// <summary>
50+
/// Class representing a SQS message event coming into a Lambda function
51+
/// </summary>
52+
public class SQSMessage
53+
{
54+
55+
/// <summary>
56+
/// Get and sets the message id
57+
/// </summary>
58+
public string MessageId { get; set; }
59+
60+
/// <summary>
61+
/// Get and sets the receipt handle
62+
/// </summary>
63+
public string ReceiptHandle { get; set; }
64+
65+
/// <summary>
66+
/// Get and sets the Body
67+
/// </summary>
68+
public string Body { get; set; }
69+
70+
/// <summary>
71+
/// Get and sets the Md5OfBody
72+
/// </summary>
73+
public string Md5OfBody { get; set; }
74+
75+
/// <summary>
76+
/// Get and sets the Md5OfMessageAttributes
77+
/// </summary>
78+
public string Md5OfMessageAttributes { get; set; }
79+
80+
/// <summary>
81+
/// Get and sets the EventSourceArn
82+
/// </summary>
83+
public string EventSourceArn { get; set; }
84+
85+
/// <summary>
86+
/// Get and sets the EventSource
87+
/// </summary>
88+
public string EventSource { get; set; }
89+
90+
/// <summary>
91+
/// Get and sets the AwsRegion
92+
/// </summary>
93+
public string AwsRegion { get; set; }
94+
95+
/// <summary>
96+
/// Get and sets the Attributes
97+
/// </summary>
98+
public Dictionary<string, string> Attributes { get; set; }
99+
100+
/// <summary>
101+
/// Get and sets the MessageAttributes
102+
/// </summary>
103+
public Dictionary<string, MessageAttribute> MessageAttributes { get; set; }
104+
}
105+
}
106+
}

Libraries/src/Amazon.Lambda.Serialization.Json/Amazon.Lambda.Serialization.Json.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<AssemblyName>Amazon.Lambda.Serialization.Json</AssemblyName>
1212
<PackageId>Amazon.Lambda.Serialization.Json</PackageId>
1313
<PackageTags>AWS;Amazon;Lambda</PackageTags>
14-
<Version>1.2.0</Version>
14+
<Version>1.2.1</Version>
1515
</PropertyGroup>
1616

1717
<ItemGroup>

Libraries/src/Amazon.Lambda.Serialization.Json/AwsResolver.cs

+14
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ protected override IList<JsonProperty> CreateProperties(Type type, MemberSeriali
6666
}
6767
}
6868
}
69+
else if (type.FullName.Equals("Amazon.Lambda.SQSEvents.SQSEvent+MessageAttribute", StringComparison.Ordinal))
70+
{
71+
foreach (JsonProperty property in properties)
72+
{
73+
if (property.PropertyName.Equals("BinaryValue", StringComparison.Ordinal))
74+
{
75+
property.MemberConverter = new JsonToMemoryStreamDataConverter();
76+
}
77+
else if (property.PropertyName.Equals("BinaryListValues", StringComparison.Ordinal))
78+
{
79+
property.MemberConverter = new JsonToMemoryStreamListDataConverter();
80+
}
81+
}
82+
}
6983

7084
return properties;
7185
}

Libraries/test/EventsTests/EventTests.cs

+65
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Amazon.Lambda.Tests
99
using Amazon.Lambda.ConfigEvents;
1010
using Amazon.Lambda.SimpleEmailEvents;
1111
using Amazon.Lambda.SNSEvents;
12+
using Amazon.Lambda.SQSEvents;
1213
using Amazon.Lambda.APIGatewayEvents;
1314
using Amazon.Lambda.LexEvents;
1415
using Amazon.Lambda.KinesisFirehoseEvents;
@@ -337,6 +338,70 @@ private static void Handle(SNSEvent snsEvent)
337338
}
338339
}
339340

341+
[Fact]
342+
public void SQSTest()
343+
{
344+
using (var fileStream = File.OpenRead("sqs-event.json"))
345+
{
346+
var serializer = new JsonSerializer();
347+
var sqsEvent = serializer.Deserialize<SQSEvent>(fileStream);
348+
349+
Assert.Equal(sqsEvent.Records.Count, 1);
350+
var record = sqsEvent.Records[0];
351+
Assert.Equal("MessageID", record.MessageId);
352+
Assert.Equal("MessageReceiptHandle", record.ReceiptHandle);
353+
Assert.Equal("Message Body", record.Body);
354+
Assert.Equal("fce0ea8dd236ccb3ed9b37dae260836f", record.Md5OfBody );
355+
Assert.Equal("582c92c5c5b6ac403040a4f3ab3115c9", record.Md5OfMessageAttributes);
356+
Assert.Equal("arn:aws:sqs:us-west-2:123456789012:SQSQueue", record.EventSourceArn);
357+
Assert.Equal("aws:sqs", record.EventSource);
358+
Assert.Equal("us-west-2", record.AwsRegion);
359+
Assert.Equal("2", record.Attributes["ApproximateReceiveCount"]);
360+
Assert.Equal("1520621625029", record.Attributes["SentTimestamp"]);
361+
Assert.Equal("AROAIWPX5BD2BHG722MW4:sender", record.Attributes["SenderId"]);
362+
Assert.Equal("1520621634884", record.Attributes["ApproximateFirstReceiveTimestamp"]);
363+
364+
Assert.Equal(2, record.MessageAttributes.Count);
365+
{
366+
var attribute1 = record.MessageAttributes["Attribute1"];
367+
Assert.NotNull(attribute1);
368+
369+
Assert.Equal("123", attribute1.StringValue);
370+
Assert.Equal("Smaug", new StreamReader(attribute1.BinaryValue).ReadToEnd());
371+
Assert.Equal(2, attribute1.StringListValues.Count);
372+
Assert.Equal("a1", attribute1.StringListValues[0]);
373+
Assert.Equal("a2", attribute1.StringListValues[1]);
374+
375+
Assert.Equal(2, attribute1.BinaryListValues.Count);
376+
Assert.Equal("Vermithrax", new StreamReader(attribute1.BinaryListValues[0]).ReadToEnd());
377+
Assert.Equal("Pejorative", new StreamReader(attribute1.BinaryListValues[1]).ReadToEnd());
378+
379+
Assert.Equal("Number", attribute1.DataType);
380+
}
381+
382+
{
383+
var attribute2 = record.MessageAttributes["Attribute2"];
384+
Assert.NotNull(attribute2);
385+
Assert.Equal("AttributeValue2", attribute2.StringValue);
386+
Assert.Equal(2, attribute2.StringListValues.Count);
387+
Assert.Equal("b1", attribute2.StringListValues[0]);
388+
Assert.Equal("b2", attribute2.StringListValues[1]);
389+
390+
Assert.Equal("String", attribute2.DataType);
391+
}
392+
393+
Handle(sqsEvent);
394+
}
395+
}
396+
397+
private static void Handle(SQSEvent sqsEvent)
398+
{
399+
foreach (var record in sqsEvent.Records)
400+
{
401+
Console.WriteLine($"[{record.EventSource}] Body = {record.Body}");
402+
}
403+
}
404+
340405
[Fact]
341406
public void APIGatewayProxyRequestTest()
342407
{

Libraries/test/EventsTests/EventsTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<ProjectReference Include="..\..\src\Amazon.Lambda.SimpleEmailEvents\Amazon.Lambda.SimpleEmailEvents.csproj" />
3838
<ProjectReference Include="..\..\src\Amazon.Lambda.SNSEvents\Amazon.Lambda.SNSEvents.csproj" />
3939
<ProjectReference Include="..\..\src\Amazon.Lambda.APIGatewayEvents\Amazon.Lambda.APIGatewayEvents.csproj" />
40+
<ProjectReference Include="..\..\src\Amazon.Lambda.SQSEvents\Amazon.Lambda.SQSEvents.csproj" />
4041
</ItemGroup>
4142

4243
<ItemGroup>
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "MessageID",
5+
"receiptHandle": "MessageReceiptHandle",
6+
"body": "Message Body",
7+
"md5OfBody": "fce0ea8dd236ccb3ed9b37dae260836f",
8+
"md5OfMessageAttributes": "582c92c5c5b6ac403040a4f3ab3115c9",
9+
"eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:SQSQueue",
10+
"eventSource": "aws:sqs",
11+
"awsRegion": "us-west-2",
12+
"attributes": {
13+
"ApproximateReceiveCount": "2",
14+
"SentTimestamp": "1520621625029",
15+
"SenderId": "AROAIWPX5BD2BHG722MW4:sender",
16+
"ApproximateFirstReceiveTimestamp": "1520621634884"
17+
},
18+
"messageAttributes": {
19+
"Attribute1": {
20+
"stringValue": "123",
21+
"binaryValue": "U21hdWc=",
22+
"stringListValues": [ "a1", "a2" ],
23+
"binaryListValues": [ "VmVybWl0aHJheA==", "UGVqb3JhdGl2ZQ==" ],
24+
"dataType": "Number"
25+
},
26+
"Attribute2": {
27+
"stringValue": "AttributeValue2",
28+
"stringListValues": [ "b1", "b2" ],
29+
"binaryListValues": [ ],
30+
"dataType": "String"
31+
}
32+
}
33+
}
34+
]
35+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ These are the packages and their README.md files:
2424
* [Amazon.Lambda.S3Events](Libraries/src/Amazon.Lambda.S3Events) - [README.md](Libraries/src/Amazon.Lambda.S3Events/README.md)
2525
* [Amazon.Lambda.SimpleEmailEvents](Libraries/src/Amazon.Lambda.SimpleEmailEvents) - [README.md](Libraries/src/Amazon.Lambda.SimpleEmailEvents/README.md)
2626
* [Amazon.Lambda.SNSEvents](Libraries/src/Amazon.Lambda.SNSEvents) - [README.md](Libraries/src/Amazon.Lambda.SNSEvents/README.md)
27+
* [Amazon.Lambda.SQSEvents](Libraries/src/Amazon.Lambda.SQSEvents) - [README.md](Libraries/src/Amazon.Lambda.SQSEvents/README.md)
2728

2829
### Amazon.Lambda.Tools
2930

buildtools/build.proj

+2
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@
262262
WorkingDirectory="..\Libraries\src\Amazon.Lambda.SimpleEmailEvents"/>
263263
<Exec Command="$(Command)"
264264
WorkingDirectory="..\Libraries\src\Amazon.Lambda.SNSEvents"/>
265+
<Exec Command="$(Command)"
266+
WorkingDirectory="..\Libraries\src\Amazon.Lambda.SQSEvents"/>
265267
<Exec Command="$(Command)"
266268
WorkingDirectory="..\Libraries\src\Amazon.Lambda.TestUtilities"/>
267269

0 commit comments

Comments
 (0)