aws / aws-lambda-dotnet

Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
Apache License 2.0
1.58k stars 478 forks source link

Parameter of type Amazon.Lambda.SQSEvents.SQSEvent passing is not supported #1245

Closed Sparafusile closed 1 month ago

Sparafusile commented 2 years ago

Describe the bug

I am unable to create a function with the following signature:

[LambdaFunction, HttpApi( LambdaHttpMethod.Get, "/sqsevent" )]
public async Task SqsEvent( SQSEvent evnt, ILambdaContext context )

Expected Behavior

I would like to be able to write a endpoint that accepts a SQS message.

Current Behavior

log.txt

Severity    Code    Description Project File    Line    Suppression State
Error   AWSLambda0001   This is a bug. Please run the build with detailed verbosity (dotnet build --verbosity detailed) and file a bug at https://github.com/aws/aws-lambda-dotnet with the build output and stack trace evnt parameter of type Amazon.Lambda.SQSEvents.SQSEvent passing is not supported.   at Amazon.Lambda.Annotations.SourceGenerator.Templates.LambdaFunctionTemplate.TransformText()  API C:\Users\foobar\API\CSC 1

Reproduction Steps

using Amazon.Lambda.Core;
using Amazon.Lambda.SQSEvents;
using Amazon.Lambda.Annotations;

[assembly: LambdaSerializer( typeof( Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer ) )]

namespace API
{
    public class Functions
    {
        public Functions()
        {
        }

        [LambdaFunction, HttpApi( LambdaHttpMethod.Get, "/sqsevent" )]
        public async Task SqsEvent( SQSEvent evnt, ILambdaContext context )
        {
        }
    }
}

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.Annotations - 0.5.0 preview Amazon.Lambda.GatewayEvents - 2.4.1 Amazon.Lambda.Core - 2.1.0 Amazon.Lambda.S3Events - 2.0.1 Amazon.Lambda.Serialization.SystemTextJson - 2.3.0 Amazon.Lambda.SQSEvents - 2.1.0 AWSSDK.DynamoDBv2 - 3.7.3.53

Targeted .NET Platform

.NET 6

Operating System and version

Windows 10

ashishdhingra commented 2 years ago

Might be related to https://github.com/aws/aws-lambda-dotnet/pull/1203.

Sparafusile commented 2 years ago

If it's any help, the compiler succeeds if I change the function definition to this:

[LambdaFunction]
public bool SqsEvent( SQSEvent evnt, ILambdaContext context )
{
    return true;
}

or this:

[LambdaFunction]
public async Task<bool> SqsEvent( SQSEvent evnt, ILambdaContext context )
{
    return true;
}

What I needed to do is remove the HttpApiAttribute (which defeats the purpose of the project) and make the method return a value (even if it's useless).

normj commented 2 years ago

@Sparafusile I'm trying to understand what you want to accomplish. The HttpApi attribute is used for exposing the Lambda function as a API Gateway rest endpoint. Where is the SQS event supposed to come from in your scenario with HttpApi?

We would like to add additional attributes to the library that handle subscribing an SQS queue as the event source of the Lambda function. Is that the feature you are looking for?

Sparafusile commented 2 years ago

@normj My specific use case is to consolidate a list of Lambda function which are each a separate project in my Visual Studio solution. My goal is to make it easier to deploy all lambda functions at once. Not all my lambda functions do have an API endpoint, but some do. This post above was simply the first function I tried.

A better example would probably be the custom identity provider I'm using for a SFTP server running in AWS Transfer:

[HttpApi( ... )]
[LambdaFunction( ... )]
public Response FunctionHandler( Credentials c, ... )
{
    // ...
}

public class Credentials
{
    ...
}

The API endpoint is required in this case, but it will throw an error because it doesn't know what to do with the Credientials parameter (and neither do I - AWS transfer documentation doesn't seem to specify how these are provided to the lambda). Using [FromBody] or [FromQuery] on the Credentials parameter does not solve the compile time issue that I listed in my initial post.

normj commented 2 years ago

@Sparafusile In this example is Credentials supposed to come from the response body?

Sparafusile commented 2 years ago

@normj I'm not entirely sure. Here is the definition of the entry point for the stand-alone lambda project that I have in production:

public Response FunctionHandler( Credentials c, ILambdaContext context )
{
}
public class Credentials
{
    public string ServerID { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

Here is a link to the documentation I used: https://docs.aws.amazon.com/transfer/latest/userguide/custom-identity-provider-users.html#authentication-api-gateway

Looking at the yml that is used to set this up, it looks like the credentials come from the query parameters.

normj commented 1 month ago

Closing because this is not a scenario that Lambda or Annotations supports having a single Lambda function responded to both an HTTP and SQS because the serialization would have no idea what type of event is coming in to serialize.

github-actions[bot] commented 1 month ago

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.