aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.65k stars 3.91k forks source link

Unable to add SQS queue as an event source in C# #2692

Closed mluttrell closed 4 years ago

mluttrell commented 5 years ago

Describe the bug Adding an SQS queue as an event source to a lambda function throws an exception with message "Parameter count mismatch." in C#.

To Reproduce

This example adds the queue as an event source after the function object is created, but I receive the same error if I try to add the queue as an event source at the time of function creation via the "Events" property.

Code:

var app = new App(new AppProps());

var stack = new Stack(app, "MyTestStack", new StackProps());

var function = new Function(stack, "MyTestLambdaFunction", new FunctionProps
{
    Runtime = Runtime.DotNetCore21,
    Handler = "MyTestAssembly::MyTestClass::Handler",
    Code = new CfnParametersCode(new CfnParametersCodeProps
    {
        BucketNameParam = new CfnParameter(stack, "BucketNameParameter", new CfnParameterProps{Type="String"}),
        ObjectKeyParam = new CfnParameter(stack, "ObjectKeyParameter", new CfnParameterProps{Type="String"})
    }),
    FunctionName = "MyTestFunction"
}); 

var queue = new Queue(stack, "MyTestQueue", new QueueProps
{
    QueueName = "MyTestQueue"
});

function.AddEventSource(new SqsEventSource(queue, new SqsEventSourceProps()));

Stacktrace:

An unhandled exception of type 'Amazon.JSII.Runtime.JsiiException' occurred in Amazon.JSII.Runtime.dll: 'Amazon.JSII.Runtime.JsiiException: Amazon.JSII.Runtime.JsiiException: System.Reflection.TargetParameterCountException: Parameter count mismatch.
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Amazon.JSII.Runtime.CallbackExtensions.InvokeMethod(InvokeRequest request, IReferenceMap referenceMap)
   at Amazon.JSII.Runtime.CallbackExtensions.InvokeCallback(Callback callback, IReferenceMap referenceMap, IFrameworkToJsiiConverter converter, String& error)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Deputy.DeputyBase.<InvokeMethodCore>g__GetResult|18_0[T](<>c__DisplayClass18_0`1& )
   at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeMethodCore[T](JsiiMethodAttribute methodAttribute, Object[] arguments, Func`3 beginFunc, Func`3 invokeFunc)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Deputy.DeputyBase.<InvokeMethodCore>g__GetResult|18_0[T](<>c__DisplayClass18_0`1& )
   at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeMethodCore[T](JsiiMethodAttribute methodAttribute, Object[] arguments, Func`3 beginFunc, Func`3 invokeFunc)
   at Amazon.CDK.AWS.Lambda.EventSources.SqsEventSource.Bind(IIFunction target)'
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Deputy.DeputyBase.<InvokeMethodCore>g__GetResult|18_0[T](<>c__DisplayClass18_0`1& )
   at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeMethodCore[T](JsiiMethodAttribute methodAttribute, Object[] arguments, Func`3 beginFunc, Func`3 invokeFunc)
   at Amazon.CDK.AWS.Lambda.FunctionBase.AddEventSource(IIEventSource source)

Expected behavior The SQS queue is added as an event source to the lambda function.

Version:

carlosrfernandez commented 5 years ago

Any updates on this? It happens on the latest preview as well

coreycoto commented 5 years ago

Any updates to this issue? It is still failing in the version 1.9.0-preview, and this is a valuable feature for Lambda functions that I would like to use.

Thanks!

sveinhelge commented 5 years ago

The same problem applies to

someQueue.GrantConsumeMessages(someRole)

This on the other hand works

someQueue.(someRole, "sqs:SendMessage", "sqs:GetQueueAttributes", "sqs:GetQueueUrl")

Get the same error when running

someQueue.GrantConsumeMessages(someRole)

This on the other hand works

someQueue.Grant(someRole,
                "sqs:ChangeMessageVisibility",
                "sqs:DeleteMessage",
                "sqs:ReceiveMessage",
                "sqs:GetQueueAttributes",
                "sqs:GetQueueUrl"
            );
sveinhelge commented 5 years ago

Workaround:

someFunction.AddEventSourceMapping("SomeId", new EventSourceMappingProps
            {
                BatchSize = 10,
                Enabled = true,
                EventSourceArn = someQueue.QueueArn,
                Target = someFunction
            });
RomainMuller commented 4 years ago

This may be fixed in the next CDK release.

RomainMuller commented 4 years ago

This should be fixed in the current release!