首页 > 解决方案 > AWS CDK 如何处理假设返回列表而不是字符串的 Fn.getAtt

问题描述

我有一个 AWS::EC2::VPCEndpoint 资源类型,我想根据https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpcendpoint 获取它的 DnsEntries 值。 html#aws-resource-ec2-vpcendpoint-return-values是 DNS 条目的列表。我希望能够从列表中选择第一项,所以我尝试了这样的事情:

const vpcEndpoint = new ec2.CfnVPCEndpoint(this, "vpcendpoint", {
    serviceName: "com.amazonaws.vpce.us-west-2.vpce-svc-xxxxxx",
    vpcId: "vpc-123",
    privateDnsEnabled: false,
    subnetIds: ["subnet-123"],
    vpcEndpointType: "Interface",
});
const fisrtDnsEntry = cdk.Fn.select(0, cdk.Fn.getAtt(vpcEndpoint.logicalId, "DnsEntries"))

这不起作用,因为Fn.select需要一个字符串数组但Fn.getAtt返回IResolvable并且只有toString()方法。

知道我还能做什么吗?

标签: aws-cdk

解决方案


关于这个有一个未解决的问题 - https://github.com/aws/aws-cdk/issues/3627

目前,您可以使用以下代码片段:

const firstEntry = cdk.Fn.select(0, vpcEndpoint.attrDnsEntries);
const entryParts = cdk.Fn.split(':', firstEntry);
const primaryDNSName = cdk.Fn.select(1, entryParts);

new cdk.CfnOutput(this, 'primaryDNSName', { value: primaryDNSName });

CDK 输出:

在此处输入图像描述

用户界面输出:

在此处输入图像描述


推荐阅读