1 回答
- 最新
- 投票最多
- 评论最多
0
【以下的回答经过翻译处理】 您需要使用自定义资源来调用API以获取端点网络接口的IP地址,因为CloudFormation不会将其公开作为返回值,这意味着CDK无法将其作为属性提供。
(请注意:此代码最初是为CDK ~1.40编写的,但仍应该能够工作或轻松更新。)
假设您像这样创建VPC端点:
// 注意:创建两个端点,因为此VPC有两个私有子网
const VPCEndpoints = new ec2.InterfaceVpcEndpoint(this, "APIEndpoint", {
vpc: my_vpc,
service: ec2.InterfaceVpcEndpointAwsService.APIGATEWAY,
subnets: { subnetType: ec2.SubnetType.PRIVATE },
});
然后,您可以创建一个自定义资源来调用其上的DescribeNetworkEndpoints API。这将是一个Lambda函数,CloudFormation将部署并调用它,函数的返回值将包含API响应。
CDK有一个辅助程序,可以轻松部署一个自定义资源,只需调用AWS API即可,因此我们可以使用它。 (CDK为我们编写一个小的nodeJS函数来执行此操作,这就是我在上面链接到JS SDK的原因。)
const eni = new custom_resources.AwsCustomResource(
this,
"DescribeNetworkInterfaces",
{
onCreate: {
service: "EC2",
action: "describeNetworkInterfaces",
parameters: {
NetworkInterfaceIds: VPCEndpoints.vpcEndpointNetworkInterfaceIds,
},
physicalResourceId: PhysicalResourceId.of(Date.now().toString()),
},
onUpdate: {
service: "EC2",
action: "describeNetworkInterfaces",
parameters: {
NetworkInterfaceIds: VPCEndpoints.vpcEndpointNetworkInterfaceIds,
},
physicalResourceId: PhysicalResourceId.of(Date.now().toString()),
},
policy: {
statements: [
new iam.PolicyStatement({
actions: ["ec2:DescribeNetworkInterfaces"],
resources: ["*"],
}),
],
},
}
);
我们现在可以在 eni 对象上使用 getResponseField () 方法从 API 调用中读取响应值,然后使用它们来创建 ALB 目标组。
// note: two ENIs in our endpoint as above, so we can get two IPs out of the response
const ip1 = eni.getResponseField("NetworkInterfaces.0.PrivateIpAddress");
const ip2 = eni.getResponseField("NetworkInterfaces.1.PrivateIpAddress");
const ALBTargetGroup = new elbv2.ApplicationTargetGroup(this, "ALBTG", {
port: 443,
targetType: elbv2.TargetType.IP,
vpc: my_vpc,
});
ALBTargetGroup.addTarget(new elbv2_targets.IpTarget(ip1));
ALBTargetGroup.addTarget(new elbv2_targets.IpTarget(ip2));
然后,目标组可以像往常一样在 ALB 监听器中使用。
相关内容
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前