aws / aws/aws-cdk

Amazon.JSII.Runtime.JsiiException while synthesizing Amazon.CDK.AWS.S3.INotificationKeyFilter[]

Open
#18,848 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-s3 bug language/dotnet p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### What is the problem?

Hi CDK Team,

unfortunately, I am unable to synthesize the an intance of **S3EventSource** containing an array of **NotificationKeyFilter** using CDK2 (latest 2.10.0) and C# language.

The follogin code snipped can be found in the [CDK2 documentation ](https://docs.aws.amazon.com/cdk/api/v2/dotnet/api/Amazon.CDK.AWS.Lambda.EventSources.S3EventSource.html):
```
using Amazon.CDK.AWS.Lambda.EventSources;
using Amazon.CDK.AWS.S3;

Function fn;

Bucket bucket = new Bucket(this, "Bucket");
fn.AddEventSource(new S3EventSource(bucket, new S3EventSourceProps {
Events = new [] { EventType.OBJECT_CREATED, EventType.OBJECT_REMOVED },
Filters = new [] { new NotificationKeyFilter { Prefix = "subdir/" } }
}));
```

_cdk synth_ fails with error message _Unable to cast Amazon.JSII.Runtime.Deputy.AnonymousObject into Amazon.CDK.AWS.S3.INotificationKeyFilter[]_

### Reproduction Steps

### Example code (complete project attached):
```
using Amazon.CDK;
using Amazon.CDK.AWS.EC2;
using Amazon.CDK.AWS.IAM;
using Amazon.CDK.AWS.Lambda;
using Amazon.CDK.AWS.Lambda.EventSources;
using Amazon.CDK.AWS.S3;
using System.Collections.Generic;

namespace Cdk2Error
{
public sealed class CdkErrorLambda : Function
{
internal CdkErrorLambda(
Constructs.Construct scope,
IVpc vpc,
Bucket bucket,
SecurityGroup securityGroup,
Role role)
: base(scope, "cdk-error-lambda", new FunctionProps {
FunctionName = "test-fnc",
Vpc = vpc,
VpcSubnets = new SubnetSelection
{
Subnets = vpc.PrivateSubnets
},
AllowAllOutbound = true,
SecurityGroups = new[]
{
securityGroup
},
Handler = "assembly::Cdk2Error.Cdk2Error::FunctionHandler",
Runtime = Runtime.DOTNET_CORE_3_1,
Architecture = Architecture.X86_64,
Code = Code.FromBucket(bucket, "test.zip"),
RetryAttempts = 2,
MemorySize = 512,
Environment = new Dictionary()
{
{ "TEST_ARG", "arg" },
},
Timeout = Duration.Minutes(1)
})
{
this.AddEventSource(new S3EventSource(bucket, new S3EventSourceProps
{
Events = new[] { EventType.OBJECT_CREATED },

// Here comes the error!
Filters = new[] { new NotificationKeyFilter { Prefix = "test/" } }
}));
}
}
}
```

### Complete cdk project
[cdk2-error.zip](https://github.com/aws/aws-cdk/files/8014091/cdk2-error.zip)

### What did you expect to happen?

cdk synth should not raise an error

### What actually happened?

### The Process fails with the following error message:
Unhandled exception. Amazon.JSII.Runtime.JsiiException: Amazon.JSII.Runtime.JsiiException: System.InvalidCastException: Unable to cast Amazon.JSII.Runtime.Deputy.AnonymousObject into Amazon.CDK.AWS.S3.INotificationKeyFilter[]
at Amazon.JSII.Runtime.Deputy.DeputyBase.System.IConvertible.ToType(Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Amazon.JSII.Runtime.Services.Converters.JsiiToFrameworkConverter.TryConvertClass(Type type, IReferenceMap referenceMap, Object value, Object& result)
at Amazon.JSII.Runtime.Services.Converters.ValueConverter.TryConvertCustomType(Type type, IReferenceMap referenceMap, Object value, Boolean isOptional, String fullyQualifiedName, Object& result)

### CDK CLI Version

2.10.0

### Framework Version

Dotnet Core 3.1

### Node.js Version

16.13.2

### OS

Windows 10

### Language

.NET

### Language Version

C#

### Other information

The issue came up when migrating from CDK1 to CDK2.
Same code worked under CDK1.

Contributor guide

Open the contributing guide

Research direction

The attached cdk2-error.zip is the reproduction; first run cdk synth with CDK CLI 2.10.0 in the supplied .NET/C# project and inspect the NotificationKeyFilter passed to S3EventSource. Done means the example synthesizes without the reported AnonymousObject-to-INotificationKeyFilter[] exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, csharp, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.