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 23 commits into
base: dev
Choose a base branch
from

Conversation

PankajRawat333
Copy link

Issue #, if available:
#1938

Description of changes:

  • Added AWS AppSyncEvent to consume and process event directly on Lambda function.
  • Added unit tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ashishdhingra
Copy link
Contributor

@PankajRawat333
Copy link
Author

@ashishdhingra I have updated type according to above link.

@ashishdhingra
Copy link
Contributor

@ashishdhingra I have updated type according to above link.

@PankajRawat333 Thanks for addressing comments for the PR. Discussed this PR with the team. @normj would have a high level look. Meanwhile, could you also please follow up with the Lambda team to get the schema details and data types for each field (since these are not documented in detail)? We would need to make sure to use the correct types for each field to avoid making any breaking changes in future for missed scenario.

@PankajRawat333
Copy link
Author

@ashishdhingra Received the AppSync schema from the internal group in the aws-powertool repository. All type information was accurate, but I renamed a few classes based on the schema.

@ashishdhingra
Copy link
Contributor

@ashishdhingra Received the AppSync schema from the internal group in the aws-powertool repository. All type information was accurate, but I renamed a few classes based on the schema.

@PankajRawat333 Provided some review comments. Also update test cases (and JSON file names) accordingly while addressing the review comments.

/// <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>
/// Represents the event payload received from AWS AppSync.
/// </summary>
public class AppSyncEvent<TArguments>
Copy link
Contributor

Choose a reason for hiding this comment

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

This appears to be a Resolver event. Should we change the name to AppSyncResolverEvent<TArguments>? Refer:

We would also need to change documentation comment for this class, update Readme and it's example.

Is AppSyncAuthorizerEvent outside the scope for this PR for now?

Copy link
Contributor

Choose a reason for hiding this comment

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

@PankajRawat333 Please advise if AppSyncAuthorizerEvent outside the scope for this PR for now.

Copy link
Author

@PankajRawat333 PankajRawat333 Mar 7, 2025

Choose a reason for hiding this comment

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

Updated. Added all type of appsync authorization

Copy link
Contributor

Choose a reason for hiding this comment

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

I was asking to validate if AppSyncAuthorizerEvent (and AppSyncAuthorizerResult) it out of scope for now, refer such event in https://www.npmjs.com/package/@types/aws-lambda?activeTab=code.

Copy link
Author

Choose a reason for hiding this comment

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

@ashishdhingra Initially, I was considering not including this, but after reviewing the documentation, I don't see any problem. Added AppSyncAuthorizerEvent & AppSyncAuthorizerResult now.

/// <summary>
/// Represents Amazon Cognito User Pools authorization identity for AppSync
/// </summary>
public class AppSyncCognitoIdentity
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.

@PankajRawat333 Please advise on this.

Copy link
Author

Choose a reason for hiding this comment

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

Added AppSyncOidcIdentity and AppSyncLambdaIdentity


# Sample Function

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

Choose a reason for hiding this comment

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

Please change AppSync to AppSync resolver

Copy link
Author

Choose a reason for hiding this comment

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

Updated

@ashishdhingra
Copy link
Contributor

@PankajRawat333 PR builds line locally and AppSync tests are successful. Please advise on the following comments:

Also,

@PankajRawat333 PankajRawat333 changed the base branch from master to dev March 7, 2025 12:39
@PankajRawat333 PankajRawat333 changed the base branch from dev to master March 7, 2025 13:56
@PankajRawat333 PankajRawat333 changed the base branch from master to dev March 7, 2025 13:56
"Projects": [
{
"Name": "Amazon.Lambda.AppSyncEvents",
"Type": "Patch",
Copy link
Contributor

Choose a reason for hiding this comment

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

The increment type should have been Major instead of Patch since this is a new package. You should have used --increment-type Major parameter while using autover (It's not well documented though, 😞)

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I was asking to validate if validate if AppSyncAuthorizerEvent (and AppSyncAuthorizerResult) are out of scope for now, refer such event and result type at https://www.npmjs.com/package/@types/aws-lambda?activeTab=code.

Copy link
Contributor

Choose a reason for hiding this comment

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

The increment type should have been Major instead of Patch since this is a new package. You should have used --increment-type Major parameter while using autover (It's not well documented though, 😞)

@philasmar Please review comment #1939 (comment). Should we be specifying version in .csproj file for 1st version? Also should the --increment-type be Major for this use case.

<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>
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm unsure if we should specify version when using 1st autover change file. CCing @philasmar for inputs.

Copy link
Contributor

Choose a reason for hiding this comment

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

You should specify a version below 1.0.0, like 0.0.1 and then use the increment type Major which would bump it to 1.0.0.

Copy link
Author

Choose a reason for hiding this comment

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

@philasmar Help me to understand your comment. My current .csproj file version is 0.0.1. Running autover with Major increment type won't automatically change the version in the .csproj file and just add new changefile.

Copy link
Contributor

Choose a reason for hiding this comment

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

@PankajRawat333 Which autover command are you using?
The actual versioning of the project will happen when we start a release. You do not need to worry about incrementing it to 1.0.0 yourself. The 2 things that need to be done from your end prior to us merging this PR is set your .csproj Version to 0.0.1, which it is, and have a change file included in the PR with an increment type set to Major, which you don't. You just need to update that from Patch to Major. If you wanted to recreate the file, you could run:

autover change --increment-type Major --project-name Amazon.Lambda.AppSyncEvents -m "Your changelog message"

Make sure to delete the old change file if you decide to create a new one.

From our end, once the PR is merged in, we will create a release which will automatically run:

autover version
autover changelog

This will look at the available change files and perform the needed version increment for the projects.

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
Author

Choose a reason for hiding this comment

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

Thanks @philasmar, Now created new changefile with increment-type Major and updated readme.

public class AppSyncAuthorizerResult
{
// Indicates if the request is authorized
#if NETSTANDARD_2_0
Copy link
Author

@PankajRawat333 PankajRawat333 Mar 12, 2025

Choose a reason for hiding this comment

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

@ashishdhingra Added JsonProperty attribute, since AWS AppSync is case sensitive. I hope this is correct way to support both type of serializer.

Copy link
Contributor

@ashishdhingra ashishdhingra Mar 19, 2025

Choose a reason for hiding this comment

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

Almost there.

Please follow approach at

. Amazon.Lambda.KinesisEvents also targets netstandard2.0;netcoreapp3.1;net8.0.

Also, readme doesn't demonstrate AppSyncAuthorizerEvent and AppSyncAuthorizerResult. Perhaps, we should add it as well (refer https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html). And also update test cases.

Once this is done, I will pull PR branch locally and validate.

Copy link
Contributor

@ankushjain358 ankushjain358 Mar 23, 2025

Choose a reason for hiding this comment

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

@ashishdhingra - Pushed the following changes:

  • Updated README file to show how to use custom lambda authorizer
  • Used #if NETCOREAPP3_1_OR_GREATER directive in AppSyncAuthorizerResult class
  • Added unit tests for AppSyncAuthorizerEvent & AppSyncAuthorizerResult classes
  • Updated XML documentation comments
  • Simplified csproj file as per other event projects

Added new tests, updated readme and incorporated review comments](9b8f7ae)

public class AppSyncAuthorizerResult
{
// Indicates if the request is authorized
#if NETSTANDARD_2_0
Copy link
Contributor

@ashishdhingra ashishdhingra Mar 19, 2025

Choose a reason for hiding this comment

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

Almost there.

Please follow approach at

. Amazon.Lambda.KinesisEvents also targets netstandard2.0;netcoreapp3.1;net8.0.

Also, readme doesn't demonstrate AppSyncAuthorizerEvent and AppSyncAuthorizerResult. Perhaps, we should add it as well (refer https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html). And also update test cases.

Once this is done, I will pull PR branch locally and validate.

## Example of Custom Lambda Authorizer
This example demonstrates how to implement a custom Lambda authorizer for AppSync using the AppSync Events package. The authorizer function receives an `AppSyncAuthorizerEvent` containing the authorization token and request context. It returns an `AppSyncAuthorizerResult` that determines whether the request is authorized and includes additional context.

The function also provides some data in the `resolverContext` object. This information is available in the AppSync resolver’s context `identity` object.
Copy link
Contributor

Choose a reason for hiding this comment

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

This information is available in the AppSync resolver’s context identity object.

What is the identity object we are talking about? ResolverContext in the AppSyncAuthorizerResult is a Dictionary. Or is it some other source which might be missing here?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ashishdhingra - Update the above explanation.

@ashishdhingra
Copy link
Contributor

Tests pass locally with latest changes from PR.

@ashishdhingra
Copy link
Contributor

@PankajRawat333 / @ankushjain358 Please review comment #1939 (comment).

Else PR looks good. Test pass locally. CC @philasmar / @normj for 2nd review.

Copy link
Contributor

@ashishdhingra ashishdhingra left a comment

Choose a reason for hiding this comment

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

Looks complete to me. Need to have 2nd review as well.

@@ -139,6 +139,10 @@
{
"Name": "Amazon.Lambda.TestTool",
"Path": "Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Amazon.Lambda.TestTool.csproj"
},
{
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: fix indentation

Copy link
Author

Choose a reason for hiding this comment

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

fixed

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

<PropertyGroup>
<Description>Amazon Lambda .NET Core support - AWS AppSync package.</Description>
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we don't need to mention "Core"

Copy link
Author

Choose a reason for hiding this comment

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

Removed


<PropertyGroup>
<Description>Amazon Lambda .NET Core support - AWS AppSync package.</Description>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net8.0</TargetFrameworks>
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is a new package, we should only target net8.0

Copy link
Author

Choose a reason for hiding this comment

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

NET 8 test was written in the 'EventsTests.NET6' project, so I included both net6.0 and net8.0

<PackageTags>AWS;Amazon;Lambda</PackageTags>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the condition if we only target net8.0

Copy link
Author

Choose a reason for hiding this comment

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

Removed

/// Indicates if the request is authorized
/// </summary>
[DataMember(Name = "isAuthorized")]
#if NETCOREAPP3_1_OR_GREATER
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed once we target net8

Copy link
Author

Choose a reason for hiding this comment

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

Removed

@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AppSyncEvents
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use top level statement

Copy link
Author

Choose a reason for hiding this comment

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

Updated

namespace Amazon.Lambda.AppSyncEvents
{
/// <summary>
/// Represents AWS LAMBDA authorization identity for AppSync
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: why is "LAMBDA" all caps?

Copy link
Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AppSyncEvents
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use top level statement

Copy link
Author

Choose a reason for hiding this comment

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

Updated

namespace Amazon.Lambda.AppSyncEvents
{
/// <summary>
/// Represents OPENID CONNECT authorization identity for AppSync
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: why is "OPENID CONNECT" all caps?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

@@ -0,0 +1,104 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AppSyncEvents
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use top level statement

Copy link
Author

Choose a reason for hiding this comment

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

Updated

@ashishdhingra ashishdhingra requested a review from philasmar March 28, 2025 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants