-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Can't get a CfnInstance with instance.getInstance() #4080
Comments
If I pass the
|
hmm ... I'm starting to think maybe |
you need to show us what your |
@jeshan: here's the whole kit n' caboodle: (ns cdk.app
(:import (software.amazon.awscdk.core App
CfnOutput
CfnOutputProps
Environment
Stack
StackProps)
(software.amazon.awscdk.services.ec2 GenericWindowsImage
Instance
InstanceClass
InstanceProps
InstanceSize
InstanceType
Port
SubnetConfiguration
SubnetSelection
SubnetType
Vpc
VpcProps
WindowsImage
WindowsVersion)
(software.amazon.awscdk.services.iam ManagedPolicy
ServicePrincipal
Role
RoleProps)
(software.amazon.awscdk.services.ssm CfnMaintenanceWindow
CfnMaintenanceWindowProps
CfnMaintenanceWindowTarget
CfnMaintenanceWindowTargetProps)))
(def keypair-name "my-keypair")
(def default-cidr "172.16.0.0/16")
(def windows-instance-type
(InstanceType/of InstanceClass/BURSTABLE3 InstanceSize/LARGE))
(def windows-rdp-port (Port/tcp 3389))
(def latest-windows-ami
(new WindowsImage WindowsVersion/WINDOWS_SERVER_2019_ENGLISH_FULL_BASE))
(def pinned-windows-ami
(new GenericWindowsImage {"us-east-2" "ami-017894519635aafd2"}))
(def public-subnet-selection
(-> (SubnetSelection/builder)
(.subnetType SubnetType/PUBLIC)
.build))
(defn instance-props [vpc role]
(-> (InstanceProps/builder)
(.instanceType windows-instance-type)
(.keyName keypair-name)
(.machineImage pinned-windows-ami)
(.role role)
(.vpc vpc)
(.vpcSubnets public-subnet-selection)
.build))
(def instance-role-props
(-> (RoleProps/builder)
(.assumedBy (new ServicePrincipal "ec2.amazonaws.com"))
(.managedPolicies [(ManagedPolicy/fromAwsManagedPolicyName
"service-role/AmazonEC2RoleforSSM")
(ManagedPolicy/fromAwsManagedPolicyName
"AmazonSSMFullAccess")])
.build))
(def stack-props
(-> (StackProps/builder)
(.env (-> (Environment/builder)
(.region "us-east-2")
.build))
.build))
(def subnet-configuration
(-> (SubnetConfiguration/builder)
(.name "Public")
(.subnetType SubnetType/PUBLIC)
.build))
(def vpc-props
(-> (VpcProps/builder)
(.cidr default-cidr)
(.subnetConfiguration [subnet-configuration])
.build))
(def maintenance-window-props
(-> (CfnMaintenanceWindowProps/builder)
(.allowUnassociatedTargets false)
(.cutoff 2)
(.schedule "cron(* 17 * * ? *)")
(.description
"Maintenance window to allow for patching windows instances")
(.duration 6)
(.name "example-windows-maintenance-window")
.build))
(defn maintenance-window-target-props [instance maintenance-window]
(-> (CfnMaintenanceWindowTargetProps/builder)
(.ownerInformation "Example")
(.description "Maintenance Window for Example instance")
(.resourceType "INSTANCE")
(.targets [{"key" "InstanceIds" "values" []}])
(.name "example-patch-targets")
(.windowId (.getRef maintenance-window))
.build))
;; entry-point
(defn -main []
(let [app (new App)
stack (new Stack app "example" stack-props)
vpc (new Vpc stack "example-vpc" vpc-props)
instance-patching-role (new Role stack "patching-role"
instance-role-props)
maintenance-window (new CfnMaintenanceWindow stack "maintenance-window"
maintenance-window-props)
instance (new Instance stack "server" (instance-props
vpc
instance-patching-role))
maintenance-window-target (new CfnMaintenanceWindowTarget maintenance-window "maintenance-window-target"
(maintenance-window-target-props instance maintenance-window))]
(doto (.getConnections instance)
(.allowFromAnyIpv4 windows-rdp-port "RDP"))
(.synth app))) |
note: I was trying to shove instance in another way with |
it looks like I am able to finagle my way to success with this:
|
ideally this would just work: |
This also resolves: |
One note about So it is expected that you have to pass ARN into |
As for the other error, looks like JSII problem. Here is the repro in Java: package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.AmazonLinuxImage;
import software.amazon.awscdk.services.ec2.CfnInstance;
import software.amazon.awscdk.services.ec2.Instance;
import software.amazon.awscdk.services.ec2.InstanceProps;
import software.amazon.awscdk.services.ec2.InstanceType;
import software.amazon.awscdk.services.ec2.Vpc;
public class HelloStack extends Stack {
public HelloStack(final Construct parent, final String id) {
this(parent, id, null);
}
public HelloStack(final Construct parent, final String id, final StackProps props) {
super(parent, id, props);
Vpc vpc = new Vpc(this, "VPC");
Instance inst = new Instance(this, "Inst", InstanceProps.builder()
.instanceType(new InstanceType("t2.micro"))
.machineImage(new AmazonLinuxImage())
.vpc(vpc)
.build());
CfnInstance inner = inst.getInstance();
System.out.println(inner);
}
}
|
The reason probably being that the Java code contains an overloaded setter that Jackson can't pick between: /**
* `AWS::EC2::Instance.BlockDeviceMappings`.
*
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-blockdevicemappings
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.External)
public void setBlockDeviceMappings(final software.amazon.awscdk.core.IResolvable value) {
this.jsiiSet("blockDeviceMappings", value);
}
/**
* `AWS::EC2::Instance.BlockDeviceMappings`.
*
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-blockdevicemappings
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.External)
public void setBlockDeviceMappings(final java.util.List<java.lang.Object> value) {
this.jsiiSet("blockDeviceMappings", value);
} |
Yeah @rix0rrr is probably right... Somehow the overloads make Jackson angry... It's odd however how this hasn't "always" been an issue, since those aren't entirely new... And the current Java code shouldn't even attempt to use Jackson on those (although I have to be validating this assumption). |
Hacky workaround: JsiiObjectMapper.INSTANCE.setAnnotationIntrospector(object : AnnotationIntrospector() {
override fun version(): Version {
return Version.unknownVersion()
}
override fun resolveSetterConflict(config: MapperConfig<*>?, setter1: AnnotatedMethod, setter2: AnnotatedMethod): AnnotatedMethod? {
return setter1
}
}) |
Any updates on this? I'd like to get the |
Are you still seeing the Jackson errors? |
Yes, no change as of |
This might require a code generation update. If I don't get any other major issues (🤞🏻) I'll spend some time on this one specifically tomorrow to get to the bottom of it. |
Here's the latest error from
|
Upon deserializing (`ObjectMapper#treeToValue`), Jackson will first validate the `valueType` for whether ti can be deserialized with the "standard" bean deserializer. This will bail out if there are multiple ambiguous setters for a given property (they are ambiguous if they have parameters of non-trivial types, such as `List`, some class, ...). In order to remove the problem, this changes the deserialization path so that instead of asking for deserialization into the needed instance type directly, `JsiiObject` will be requested instead when the declared type is a sub-class of that. Since such types are passed by-reference, the custom de-serializer modifier will correctly determine the "right" class to use (the declared type, or a subtype of it), and return this... But Jackson will not get the chance to be confused or unhappy about that type's structure. This was the root cause of aws/aws-cdk#4080
* fix(java): remove Jackson confusion with certain patterns Upon deserializing (`ObjectMapper#treeToValue`), Jackson will first validate the `valueType` for whether ti can be deserialized with the "standard" bean deserializer. This will bail out if there are multiple ambiguous setters for a given property (they are ambiguous if they have parameters of non-trivial types, such as `List`, some class, ...). In order to remove the problem, this changes the deserialization path so that instead of asking for deserialization into the needed instance type directly, `JsiiObject` will be requested instead when the declared type is a sub-class of that. Since such types are passed by-reference, the custom de-serializer modifier will correctly determine the "right" class to use (the declared type, or a subtype of it), and return this... But Jackson will not get the chance to be confused or unhappy about that type's structure. This was the root cause of aws/aws-cdk#4080 * add test specifically on structs
I believe this has been fixed in the latest release. |
Oh no it has been reverted. |
(Reintroduce #987). Upon deserializing (`ObjectMapper#treeToValue`), Jackson will first validate the `valueType` for whether ti can be deserialized with the "standard" bean deserializer. This will bail out if there are multiple ambiguous setters for a given property (they are ambiguous if they have parameters of non-trivial types, such as `List`, some class, ...). In order to remove the problem, this changes the deserialization path so that instead of asking for deserialization into the needed instance type directly, `JsiiObject` will be requested instead when the declared type is a sub-class of that. Since such types are passed by-reference, the custom de-serializer modifier will correctly determine the "right" class to use (the declared type, or a subtype of it), and return this... But Jackson will not get the chance to be confused or unhappy about that type's structure. This was the root cause of aws/aws-cdk#4080
(Reintroduce #987). Upon deserializing (`ObjectMapper#treeToValue`), Jackson will first validate the `valueType` for whether ti can be deserialized with the "standard" bean deserializer. This will bail out if there are multiple ambiguous setters for a given property (they are ambiguous if they have parameters of non-trivial types, such as `List`, some class, ...). In order to remove the problem, this changes the deserialization path so that instead of asking for deserialization into the needed instance type directly, `JsiiObject` will be requested instead when the declared type is a sub-class of that. Since such types are passed by-reference, the custom de-serializer modifier will correctly determine the "right" class to use (the declared type, or a subtype of it), and return this... But Jackson will not get the chance to be confused or unhappy about that type's structure. This was the root cause of aws/aws-cdk#4080
The fix for this will roll out in the next CDK release. I'll resolve this issue for now, please re-open if you still encounter the problem once the new release (probably |
🐛 Bug Report
What is the problem?
Can't get a
CfnInstance
withinstance.getInstance()
. I was hoping to get aCfnInstance
to see if I can pass it in toTargets
ofCfnMaintenanceWindowTargetProps
.Reproduction Steps
Verbose Log
or
Environment
Other information
The text was updated successfully, but these errors were encountered: