Ошибка в NetworkInterfaces в CfnNetworkInterface - PullRequest
2 голосов
/ 01 апреля 2020

Я пытаюсь сгенерировать файл CloudFormation с C#. К сожалению, все, что я передаю как NetworkInterfaces в CfnInstance, я получаю сообщение об ошибке. Я просмотрел всю документацию и не смог найти решение своей проблемы.

Вот мой 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()}

Если я удаляю NetworkInteraces свойство из CfnNetworkInterface приведенный выше код работает нормально.

Я не могу понять, в чем проблема. Все, что я изменяю, приводит к тому же исключению из вышесказанного. Любая помощь с благодарностью.

Заранее спасибо.

1 Ответ

3 голосов
/ 02 апреля 2020

Мне удалось воссоздать и исправить проблему с API, используя TypeScript. Я был в состоянии синтезировать стек. Я считаю, что то же самое применимо в C#.

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

Проблема заключается в использовании CfnNetworkInterface в CfnInstance. Свойство networkInterfaces в CfnInstance ожидает массив типа CfnInstance.NetworkInterfaceProperty или cdk.IResolvable. То, что вы предоставляете ему прямо сейчас (CfnNetworkInterface), не соответствует ожидаемому типу. Я также был озадачен, увидев, что я не смог импортировать CfnInstance.NetworkInterfaceProperty в своем коде TS выше, но смог привести нотацию объекта к IResolvable.

Также обратите внимание, что при использовании CfnInstance.NetworkInterfaceProperty вы также должны указать deviceIndex. Надеюсь, это решит вашу проблему в C# коде.

PS: я удалил blockDeviceMappings из моего подхода для краткости

...