EventPattern.builder generates incorrect JSON in java CDK
The EventPattern object generated by CDK has incorrect fields:
Generated JSON:
{"account":null,"detail":"{\"State\":[\"running\",\"shutting-down\",\"stopped\"]}","detailType":["EC2 Instance State-change Notification"],"id":["eventForLambda"],"region":["us-west-1"],"resources":null,"source":["aws.ec2"],"time":null,"version":null}
Correct JSON:
{"source":["aws.ec2"],"detail-type": ["EC2 Instance State-change Notification"],"detail": {"state": ["shutting-down","stopped","terminated"]}}
As can be seen the extra fields should not be there, the Java Code to get the incorrect file used was:
EventPattern evntPattern = EventPattern.builder()
.withDetail(json.toString())
.withSource(Arrays.asList("aws.ec2"))
.withId(Arrays.asList("eventForLambda"))
.withDetailType(Arrays.asList("EC2 Instance State-change Notification"))
.build();
System.out.print("Event Pattern: "+evntPattern.$jsii$toJson()+"\n");
evntRule.addEventPattern(evntPattern);
evntRule.addTarget((IEventRuleTarget) tarjet);
Lastly the addTarget requires cast to the Interface where all other parts of the code there is no need to cast.
Upon further investigation I have found that EventPattern object requires that all attributes be set, even though they are not needed out used.
The following Java code works:
EventPattern evntPattern = EventPattern.builder()
.withDetail(Arrays.asList(json.toString()))
.withRegion(Arrays.asList("us-west-1"))
.withSource(Arrays.asList("aws.ec2"))
.withId(Arrays.asList("eventForLambda"))
.withDetailType(Arrays.asList("EC2 Instance State-change Notification"))
.withAccount(Arrays.asList("951467776731"))
.withResources(Arrays.asList(""))
.withTime(Arrays.asList(""))
.withVersion(Arrays.asList("1"))
.build();
As can be seen a lot of the attributes are simply empty, but they should not be mandatory.
This was solved in 0.28.0