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 AWS AppSyncEvent #1939

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 11 additions & 4 deletions Libraries/Libraries.sln
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestMinimalAPIApp", "test\T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.MQEvents", "src\Amazon.Lambda.MQEvents\Amazon.Lambda.MQEvents.csproj", "{BF85932E-2DFF-41CD-8090-A672468B8FBB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Amazon.Lambda.LexV2Events", "src\Amazon.Lambda.LexV2Events\Amazon.Lambda.LexV2Events.csproj", "{3C6AABF5-0372-41E0-874F-DF18ECCC7FB6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.LexV2Events", "src\Amazon.Lambda.LexV2Events\Amazon.Lambda.LexV2Events.csproj", "{3C6AABF5-0372-41E0-874F-DF18ECCC7FB6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest", "test\Amazon.Lambda.RuntimeSupport.Tests\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest.csproj", "{0BD83939-458C-4EF5-8663-7098AD1200F2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest", "test\Amazon.Lambda.RuntimeSupport.Tests\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest\CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest.csproj", "{0BD83939-458C-4EF5-8663-7098AD1200F2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestExecutableServerlessApp", "test\TestExecutableServerlessApp\TestExecutableServerlessApp.csproj", "{DD378063-C54A-44C7-9A6F-32A6A1AE94B3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestServerlessApp.NET8", "test\TestServerlessApp.NET8\TestServerlessApp.NET8.csproj", "{7300983D-8FCE-42EA-9B9E-B1C5347D15D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnapshotRestore.Registry", "src\SnapshotRestore.Registry\SnapshotRestore.Registry.csproj", "{7261A438-8C1D-47AD-98B0-7678F72E4382}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SnapshotRestore.Registry", "src\SnapshotRestore.Registry\SnapshotRestore.Registry.csproj", "{7261A438-8C1D-47AD-98B0-7678F72E4382}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnapshotRestore.Registry.Tests", "test\SnapshotRestore.Registry.Tests\SnapshotRestore.Registry.Tests.csproj", "{A699E183-D0D4-4F26-A0A7-88DA5607F455}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SnapshotRestore.Registry.Tests", "test\SnapshotRestore.Registry.Tests\SnapshotRestore.Registry.Tests.csproj", "{A699E183-D0D4-4F26-A0A7-88DA5607F455}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.AppSyncEvents", "src\Amazon.Lambda.AppSyncEvents\Amazon.Lambda.AppSyncEvents.csproj", "{99F39E49-1FD0-4EF5-BF4B-8F2473FB8198}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -369,6 +371,10 @@ Global
{A699E183-D0D4-4F26-A0A7-88DA5607F455}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A699E183-D0D4-4F26-A0A7-88DA5607F455}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A699E183-D0D4-4F26-A0A7-88DA5607F455}.Release|Any CPU.Build.0 = Release|Any CPU
{99F39E49-1FD0-4EF5-BF4B-8F2473FB8198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99F39E49-1FD0-4EF5-BF4B-8F2473FB8198}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99F39E49-1FD0-4EF5-BF4B-8F2473FB8198}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99F39E49-1FD0-4EF5-BF4B-8F2473FB8198}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -435,6 +441,7 @@ Global
{7300983D-8FCE-42EA-9B9E-B1C5347D15D8} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
{7261A438-8C1D-47AD-98B0-7678F72E4382} = {AAB54E74-20B1-42ED-BC3D-CE9F7BC7FD12}
{A699E183-D0D4-4F26-A0A7-88DA5607F455} = {1DE4EE60-45BA-4EF7-BE00-B9EB861E4C69}
{99F39E49-1FD0-4EF5-BF4B-8F2473FB8198} = {AAB54E74-20B1-42ED-BC3D-CE9F7BC7FD12}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {503678A4-B8D1-4486-8915-405A3E9CF0EB}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\..\buildtools\common.props" />

<PropertyGroup>
<Description>Amazon Lambda .NET Core support - AWS AppSync package.</Description>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net8.0</TargetFrameworks>
<AssemblyTitle>Amazon.Lambda.AppSyncEvents</AssemblyTitle>
<Version>1.0.0</Version>
<AssemblyName>Amazon.Lambda.AppSyncEvents</AssemblyName>
<PackageId>Amazon.Lambda.AppSyncEvents</PackageId>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<WarningsAsErrors>IL2026,IL2067,IL2075</WarningsAsErrors>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>
</Project>
103 changes: 103 additions & 0 deletions Libraries/src/Amazon.Lambda.AppSyncEvents/AppSyncEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AppSyncEvents
{
/// <summary>
/// Represents the event payload received from AWS AppSync.
/// </summary>
public class AppSyncEvent
{
/// <summary>
/// Gets or sets the input arguments for the GraphQL operation.
/// </summary>
public Dictionary<string, object> Arguments { get; set; }

/// <summary>
/// An object that contains information about the caller.
/// Returns null for API_KEY authorization.
/// Returns AppSyncIdentityIAM for AWS_IAM authorization.
/// Returns AppSyncIdentityCognito for AMAZON_COGNITO_USER_POOLS authorization.
/// For AWS_LAMBDA authorization, returns the object returned by your Lambda authorizer function.
/// </summary>
/// <remarks>
/// The Identity object type depends on the authorization mode:
/// - For API_KEY: null
/// - For AWS_IAM: <see cref="AppSyncIdentityIAM"/>
/// - For AMAZON_COGNITO_USER_POOLS: <see cref="AppSyncIdentityCognito"/>
/// - For AWS_LAMBDA: Dynamic object returned by Lambda authorizer
/// </remarks>
public object Identity { get; set; }

/// <summary>
/// Gets or sets information about the data source that originated the event.
/// </summary>
public object Source { get; set; }

/// <summary>
/// Gets or sets information about the HTTP request that triggered the event.
/// </summary>
public RequestContext Request { get; set; }

/// <summary>
/// Gets or sets information about the previous state of the data before the operation was executed.
/// </summary>
public object Prev { get; set; }

/// <summary>
/// Gets or sets information about the GraphQL operation being executed.
/// </summary>
public Information Info { get; set; }

/// <summary>
/// Gets or sets additional information that can be passed between Lambda functions during an AppSync pipeline.
/// </summary>
public Dictionary<string, object> Stash { get; set; }

/// <summary>
/// Represents information about the HTTP request that triggered the event.
/// </summary>
public class RequestContext
{
/// <summary>
/// Gets or sets the headers of the HTTP request.
/// </summary>
public Dictionary<string, string> Headers { get; set; }

/// <summary>
/// Gets or sets the domain name associated with the request.
/// </summary>
public object DomainName { get; set; }
}

/// <summary>
/// Represents information about the GraphQL operation being executed.
/// </summary>
public class Information
{
/// <summary>
/// Gets or sets the name of the GraphQL field being executed.
/// </summary>
public string FieldName { get; set; }

/// <summary>
/// Gets or sets a list of fields being selected in the GraphQL operation.
/// </summary>
public List<string> SelectionSetList { get; set; }

/// <summary>
/// Gets or sets the GraphQL selection set for the operation.
/// </summary>
public string SelectionSetGraphQL { get; set; }

/// <summary>
/// Gets or sets the variables passed to the GraphQL operation.
/// </summary>
public Dictionary<string, object> Variables { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per https://www.npmjs.com/package/@types/aws-lambda?activeTab=code, the data type for value is any. So we are good for it to be object.


/// <summary>
/// Gets or sets the parent type name for the GraphQL operation.
/// </summary>
public string ParentTypeName { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AppSyncEvents
{
/// <summary>
/// Represents Amazon Cognito User Pools authorization identity for AppSync
/// </summary>
public class AppSyncIdentityCognito
{
/// <summary>
/// The source IP address of the caller received by AWS AppSync
/// </summary>
public List<string> SourceIp { get; set; }

/// <summary>
/// The username of the authenticated user
/// </summary>
public string Username { get; set; }

/// <summary>
/// The UUID of the authenticated user
/// </summary>
public string Sub { get; set; }

/// <summary>
/// The claims that the user has
/// </summary>
public Dictionary<string, object> Claims { get; set; }

/// <summary>
/// The default authorization strategy for this caller (ALLOW or DENY)
/// </summary>
public string DefaultAuthStrategy { get; set; }

/// <summary>
/// List of OIDC groups
/// </summary>
public List<string> Groups { get; set; }

/// <summary>
/// The token issuer
/// </summary>
public string Issuer { get; set; }
}

}
50 changes: 50 additions & 0 deletions Libraries/src/Amazon.Lambda.AppSyncEvents/AppSyncIdentityIAM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AppSyncEvents
{
/// <summary>
/// Represents AWS IAM authorization identity for AppSync
/// </summary>
public class AppSyncIdentityIAM
{
/// <summary>
/// The source IP address of the caller received by AWS AppSync
/// </summary>
public List<string> SourceIp { get; set; }

/// <summary>
/// The username of the authenticated user (IAM user principal)
/// </summary>
public string Username { get; set; }

/// <summary>
/// The AWS account ID of the caller
/// </summary>
public string AccountId { get; set; }

/// <summary>
/// The Amazon Cognito identity pool ID associated with the caller
/// </summary>
public string CognitoIdentityPoolId { get; set; }

/// <summary>
/// The Amazon Cognito identity ID of the caller
/// </summary>
public string CognitoIdentityId { get; set; }

/// <summary>
/// The ARN of the IAM user
/// </summary>
public string UserArn { get; set; }

/// <summary>
/// Either authenticated or unauthenticated based on the identity type
/// </summary>
public string CognitoIdentityAuthType { get; set; }

/// <summary>
/// A comma separated list of external identity provider information used in obtaining the credentials used to sign the request
/// </summary>
public string CognitoIdentityAuthProvider { get; set; }
}
}
32 changes: 32 additions & 0 deletions Libraries/src/Amazon.Lambda.AppSyncEvents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Amazon.Lambda.AppSyncEvents

This package contains classes that can be used as input types for Lambda functions that process AppSync events.

# Sample Function

The following is a sample class and Lambda function that receives AppSync event record data as an `appSyncEvent` and logs some of the incoming event data. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)

```csharp
public void Handler(AppSyncEvent appSyncEvent, ILambdaContext context)
{
foreach (var item in appSyncEvent.Arguments)
{
Console.WriteLine($"AppSync request key - {item.Key}.");
}

if (appSyncEvent.Identity != null)
{
// Create an instance of the serializer
var lambdaSerializer = new DefaultLambdaJsonSerializer();

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(appSyncEvent.Identity.ToString()!)))
{
// When using AMAZON_COGNITO_USER_POOLS authorization
var cognitoIdentity = lambdaSerializer.Deserialize<AppSyncIdentityCognito>(stream);

// When using AWS_IAM authorization
var iamIdentity = lambdaSerializer.Deserialize<AppSyncIdentityIAM>(stream);
}
}
}
```
1 change: 1 addition & 0 deletions Libraries/test/EventsTests.NET6/EventsTests.NET6.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Amazon.Lambda.AppSyncEvents\Amazon.Lambda.AppSyncEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.MQEvents\Amazon.Lambda.MQEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.CloudWatchEvents\Amazon.Lambda.CloudWatchEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.ApplicationLoadBalancerEvents\Amazon.Lambda.ApplicationLoadBalancerEvents.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Amazon.Lambda.AppSyncEvents\Amazon.Lambda.AppSyncEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.MQEvents\Amazon.Lambda.MQEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.CloudWatchEvents\Amazon.Lambda.CloudWatchEvents.csproj" />
<ProjectReference Include="..\..\src\Amazon.Lambda.ApplicationLoadBalancerEvents\Amazon.Lambda.ApplicationLoadBalancerEvents.csproj" />
Expand Down
Loading