使用AWS CDK创建EFS挂载目标时如何设置subnet_id?

时间:2019-12-16 13:43:22

标签: python amazon-web-services aws-cdk amazon-efs

我收到一个“ jsii.errors.JSIIError:预期的标量,得到了{” $ jsii.byref“:” @ aws-cdk/core.Reference@10024“}”当我将子网ID传递给Mount Target时出错。这是代码:

  mountTarget = efs.CfnMountTarget(self, "EfsMounttarget",
    file_system_id=filesystem.get_att("FileSystemId"),
    security_groups= [ sg_asg ],
    subnet_id=vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC).subnet_ids[0]
    )

当我打印出select_subnets方法时,我得到了正确的ID作为字符串。

print(type(vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC).subnet_ids[0]))
print(vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC).subnet_ids[0])

输出:

<class 'str'>
subnet-9b1f3bd3

该参数需要一个字符串,因此我被困在这里。

这是完整的错误消息:

jsii.errors.JavaScriptError: 
  Error: Expected Scalar, got {"$jsii.byref":"@aws-cdk/core.Reference@10024"}
      at Object.deserialize (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6630:23)
      at Kernel._toSandbox (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8111:61)
      at /home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6841:29
      at mapValues (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7101:27)
      at Object.deserialize (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6837:20)
      at Kernel._toSandbox (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8111:61)
      at /home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8164:33
      at Array.map (<anonymous>)
      at Kernel._boxUnboxParameters (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8164:19)
      at Kernel._wrapSandboxCode (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8205:19)
      at Kernel._create (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7749:26)
      at Kernel.create (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7503:21)
      at KernelHost.processRequest (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7293:28)
      at KernelHost.run (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7233:14)
      at Immediate._onImmediate (/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7236:37)
      at processImmediate (internal/timers.js:439:21)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 9, in <module>
    HelloCdkStack(app, "HelloCdkStack2", env={'region': 'eu-west-1', 'account': '000000000000'})
  File "/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/home/username/dev/tests/hello-cdk/hello_cdk/hello_cdk_stack.py", line 92, in __init__
    subnet_id=vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC).subnet_ids[0]
  File "/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/aws_cdk/aws_efs/__init__.py", line 385, in __init__
    jsii.create(CfnMountTarget, self, [scope, id, props])
  File "/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_kernel/__init__.py", line 223, in create
    interfaces=[iface.__jsii_type__ for iface in getattr(klass, "__jsii_ifaces__", [])],
  File "/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_kernel/providers/process.py", line 333, in create
    return self._process.send(request, CreateResponse)
  File "/home/username/dev/tests/hello-cdk/.env/lib/python3.7/site-packages/jsii/_kernel/providers/process.py", line 318, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Expected Scalar, got {"$jsii.byref":"@aws-cdk/core.Reference@10024"}
Subprocess exited with error 1

感谢您的帮助和建议。

1 个答案:

答案 0 :(得分:0)

问题不是subnet_id,而是该代码有效的file_system_id:

mountTarget = efs.CfnMountTarget(self, "EfsMounttarget"+str(i),
            file_system_id=filesystem.ref,
            security_groups= [ sg_efs.security_group_id ],
            subnet_id=vpc.select_subnets(subnet_type=ec2.SubnetType.PRIVATE).subnets[0].subnet_id
        )       

文件系统是CfnFileSystem