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

How to set the subnet_id when creating an EFS Mount Target with AWS CDK?

当我将子网 ID 传递到我的装载目标时,我收到 "jsii.errors.JSIIError: Expected Scalar, got {"$jsii.byref":"@aws-cdk/core.Reference@10024"}" 错误。这是代码:

  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

感谢您的帮助和建议。

问题不在于 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