CfnNetworkInterface 中的 NetworkInterfaces 错误

Error on NetworkInterfaces in CfnNetworkInterface

我正在尝试使用 C# 生成 CloudFormation 文件。 不幸的是,无论我在 CfnInstance 中作为 NetworkInterfaces 传递什么,我都会收到错误消息。 我搜索了整个文档,但找不到解决我的问题的方法。

这是我的 C# 堆栈。

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"
            });
        }
    }

不幸的是,异常的堆栈跟踪并没有把事情说清楚。 这是堆栈跟踪本身:

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()

我也试过了

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

如果我从 CfnNetworkInterface 中删除 NetworkInteraces 属性 上面的代码工作正常。

我不明白问题出在哪里。 我更改的所有内容都会导致与上述相同的异常。 非常感谢任何帮助。

提前致谢。

我能够使用 TypeScript 重新创建并修复 API 问题。我能够合成堆栈。我相信这同样适用于 C#。

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

问题在于 CfnInstanceCfnNetworkInterface 的使用。 CfnInstance 中的 属性 networkInterfaces 需要一个 CfnInstance.NetworkInterfaceProperty or cdk.IResolvable 类型的数组。您现在提供给它的内容 (CfnNetworkInterface) 与预期类型不匹配。我也很困惑地​​看到我无法在上面的 TS 代码中导入 CfnInstance.NetworkInterfaceProperty,但能够将对象表示法强制转换为 IResolvable

另请注意,当您使用 CfnInstance.NetworkInterfaceProperty 时,您还必须提供 deviceIndex。希望这将解决您在 C# 代码中的问题。

PS:为简洁起见,我已从我的方法中删除了 blockDeviceMappings