Error on NetworkInterfaces in CfnNetworkInterface

Viewed 248

I am trying to generate CloudFormation file with C#. Unfortunately whatever I pass as NetworkInterfaces in CfnInstance I get an error. I Searched through the whole documentation and cannot find a solution to my problem.

Here is my C# Stack.

public class CFStack : Amazon.CDK.Stack
    {
        public CFStack(Construct parent, string id) : base(parent, id)
        {
            var vpc = new CfnVPC(this, "VPC", new CfnVPCProps()
            {
                CidrBlock = "10.0.0.0/16",
                EnableDnsHostnames = true,
                EnableDnsSupport = true,
                InstanceTenancy = "default"
            });

            var sg = new CfnSecurityGroup(this, "SG", new CfnSecurityGroupProps()
            {
                GroupDescription = "CF Security group",
                GroupName = "CF SG",
                VpcId = vpc.Ref,
            });

            var subnet = new CfnSubnet(this, "Subnet", new CfnSubnetProps()
            {
                CidrBlock = "10.0.0.0/24",
                MapPublicIpOnLaunch = true,
                VpcId = vpc.Ref,
            });

            new CfnSecurityGroupIngress(this, "ingress", new CfnSecurityGroupIngressProps()
            {
                GroupId = sg.Ref,
                FromPort = 3389,
                ToPort = 3389,
                CidrIp = "213.155.147.202/32",
                IpProtocol = "tcp"
            });

            new CfnSecurityGroupEgress(this, "egress", new CfnSecurityGroupEgressProps()
            {
                GroupId = sg.Ref,
                FromPort = 80,
                ToPort = 444,
                CidrIp = "0.0.0.0/0",
                IpProtocol = "tcp",
            });

            var blockmapping = new CfnInstance.BlockDeviceMappingProperty()
            {
                DeviceName = "/dev/sdh",
                Ebs = new CfnInstance.EbsProperty()
                {
                    VolumeType = EbsDeviceVolumeType.GP2.ToString(),
                    DeleteOnTermination = true,
                    VolumeSize = 65,
                }
            };

            var networkInterface = new CfnNetworkInterface(this, "NetworkInterface", new CfnNetworkInterfaceProps()
            {
                GroupSet = new[] { sg.ToString() },
                SubnetId = subnet.Ref
            });

            new CfnInstance(this, "EC2", new CfnInstanceProps()
            {
                NetworkInterfaces = new[] { networkInterface },
                BlockDeviceMappings = new[] { blockmapping },
                ImageId = "ami-id",
                InstanceType = "m5.xlarge",
                KeyName = "keyName",
                UserData = "test"
            });
        }
    }

Unfortunately the stacktrace of the exception does not make things clear. Here is the stacktrace itself:

Amazon.JSII.Runtime.JsiiException: Amazon.JSII.Runtime.JsiiException: Resolution error: Resolution error: Trying to resolve() a Construct at /Resources/${Token[MihailStack.EC2.LogicalID.32]}/Properties/networkInterfaces/0/node/_actualNode.
Object creation stack:
  at new Intrinsic (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\private\intrinsic.js:20:44)
  at new PostResolveToken (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\util.js:72:9)
  at Object.ignoreEmpty (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\util.js:32:12)
  at CfnInstance._toCloudFormation (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\cfn-resource.js:214:44)
  at C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\stack.js:833:76
  at Object.findTokens (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\private\resolve.js:126:13)
  at Stack.findTokens (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\stack.js:833:42)
  at Stack.prepare (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\stack.js:544:29)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:51
  at Kernel._wrapSandboxCode (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8298:20)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:25
  at Kernel._ensureSync (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8274:20)
  at Kernel.invoke (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7664:26)
  at KernelHost.processRequest (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7372:28)
  at KernelHost.completeCallback (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7345:25)
  at KernelHost.callbackHandler (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7326:33)
  at Stack.value (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8012:41)
  at Stack.onPrepare (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\construct-compat.js:66:14)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:51
  at Kernel._wrapSandboxCode (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8298:20)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:25
  at Kernel._ensureSync (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8274:20)
  at Kernel.invoke (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7664:26)
  at KernelHost.processRequest (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7372:28)
  at KernelHost.completeCallback (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7345:25)
  at KernelHost.callbackHandler (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7326:33)
  at Stack.value (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8012:41)
  at Node.prepare (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\constructs\lib\construct.js:371:27)
  at Node.synthesize (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\constructs\lib\construct.js:333:14)
  at Function.synth (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\construct-compat.js:165:26)
  at App.synth (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\app.js:71:59)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:51
  at Kernel._wrapSandboxCode (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8298:20)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:25
  at Kernel._ensureSync (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8274:20)
  at Kernel.invoke (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7664:26)
  at KernelHost.processRequest (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7372:28)
  at KernelHost.run (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7312:14)
  at Immediate._onImmediate (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7315:37)
  at processImmediate (internal/timers.js:439:21).
Object creation stack:
  at new Intrinsic (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\private\intrinsic.js:20:44)
  at new PostResolveToken (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\util.js:72:9)
  at CfnInstance._toCloudFormation (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\cfn-resource.js:212:39)
  at C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\stack.js:833:76
  at Object.findTokens (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\private\resolve.js:126:13)
  at Stack.findTokens (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\stack.js:833:42)
  at Stack.prepare (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\stack.js:544:29)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:51
  at Kernel._wrapSandboxCode (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8298:20)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:25
  at Kernel._ensureSync (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8274:20)
  at Kernel.invoke (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7664:26)
  at KernelHost.processRequest (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7372:28)
  at KernelHost.completeCallback (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7345:25)
  at KernelHost.callbackHandler (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7326:33)
  at Stack.value (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8012:41)
  at Stack.onPrepare (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\construct-compat.js:66:14)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:51
  at Kernel._wrapSandboxCode (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8298:20)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:25
  at Kernel._ensureSync (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8274:20)
  at Kernel.invoke (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7664:26)
  at KernelHost.processRequest (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7372:28)
  at KernelHost.completeCallback (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7345:25)
  at KernelHost.callbackHandler (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7326:33)
  at Stack.value (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8012:41)
  at Node.prepare (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\constructs\lib\construct.js:371:27)
  at Node.synthesize (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\constructs\lib\construct.js:333:14)
  at Function.synth (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\construct-compat.js:165:26)
  at App.synth (C:\Users\hmh\AppData\Local\Temp\jsii-kernel-lGLo4x\node_modules\@aws-cdk\core\lib\app.js:71:59)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:51
  at Kernel._wrapSandboxCode (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8298:20)
  at C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7665:25
  at Kernel._ensureSync (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:8274:20)
  at Kernel.invoke (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7664:26)
  at KernelHost.processRequest (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7372:28)
  at KernelHost.run (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7312:14)
  at Immediate._onImmediate (C:\Users\hmh\AppData\Local\Temp\ys3l4mjc.q1n\jsii-runtime.js:7315:37)
  at processImmediate (internal/timers.js:439:21)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.Invoke(InvokeRequest request)
   at Amazon.JSII.Runtime.Services.Client.Invoke(ObjectReference objectReference, String method, Object[] arguments)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.<>c__DisplayClass17_0`1.<InvokeInstanceMethod>b__1(IClient client, Object[] args)
   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.Deputy.DeputyBase.InvokeInstanceMethod[T](Type[] parameterTypes, Object[] arguments, String methodName)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeInstanceVoidMethod(Type[] parameterTypes, Object[] arguments, String methodName)
   at Amazon.CDK.Stack.Prepare()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.Invoke(InvokeRequest request)
   at Amazon.JSII.Runtime.Services.Client.Invoke(ObjectReference objectReference, String method, Object[] arguments)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.<>c__DisplayClass17_0`1.<InvokeInstanceMethod>b__1(IClient client, Object[] args)
   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.Deputy.DeputyBase.InvokeInstanceMethod[T](Type[] parameterTypes, Object[] arguments, String methodName)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeInstanceVoidMethod(Type[] parameterTypes, Object[] arguments, String methodName)
   at Amazon.CDK.Construct.OnPrepare()

I have also tried to pass

NetworkInterfaces = networkInterface
NetworkInterfaces = networkInterface.ToString()
NetworkInterfaces = new[] {networkInterface}
NetworkInterfaces = new[] {networkInterface.ToString()}

If I remove NetworkInteraces property from CfnNetworkInterface the above code works fine.

I cannot understand what the problem is. Everything I change results the same exception from the above. Any help is greatly appreciated.

Thank you in advance.

1 Answers

I was able to recreate and fix the API problem using TypeScript. I was able to synthesize the stack. I believe the same is applicable in C#.

new CfnInstance(this, "EC2", {
  networkInterfaces: [{
    deviceIndex: '0',
    groupSet: [sg.toString()],
    subnetId: subnet.ref
  }],
  imageId: "ami-id",
  instanceType: "t2.micro",
  keyName: "keyName",
  userData: "test"
});

The problem lies with the use of CfnNetworkInterface in CfnInstance. The property networkInterfaces in CfnInstance expects an array of type CfnInstance.NetworkInterfaceProperty or cdk.IResolvable. What you are providing to it right now (CfnNetworkInterface) mismatches with the expected type. I was also baffled to see that I wasn't able to import CfnInstance.NetworkInterfaceProperty in my TS code above, but was able to coerce object notation to IResolvable.

Also note that, when you use CfnInstance.NetworkInterfaceProperty you also have to provide the deviceIndex. Hopefully, this will solve your problem in C# code.

PS: I have removed the blockDeviceMappings from my approach for brevity

Related