我试图使用UDP端口时添加端口映射为网络负载平衡Fargate服务,但cdk
构建失败与以下错误。
Error: Container '**-ECS-Task-Container' has no mapping for port undefined and protocol tcp. Did you call "container.addPortMappings()"?
at FargateTaskDefinition._validateTarget (/../node_modules/aws-cdk-lib/aws-ecs/lib/base/task-definition.js:1:7371)
at FargateService.loadBalancerTarget (/../node_modules/aws-cdk-lib/aws-ecs/lib/base/base-service.js:1:9940)
at FargateService.get defaultLoadBalancerTarget [as defaultLoadBalancerTarget]
相关代码:
const container = taskDefinition.addContainer(props.stackName + '-ECS-Task-Container', {
containerName: props.stackName + '-ECS-Container',
image: serviceImage,
memoryLimitMiB: 512,
cpu: 256,
portMappings: [
{
containerPort: 7950,
hostPort: 7950,
protocol: EcsProtocol.UDP
},
]
});
}
or,
container.addPortMappings({
containerPort: 7950,
hostPort: 7950,
protocol: EcsProtocol.UDP
}
);
发现其他几个也遇到了同样的问题:https://github.com/aws/containers-roadmap/issues/445 issuecomment - 714930539
是否可以使用配置UDP端口为网络负载平衡Fargate服务使用CDK?
我找到了一个工作,最初在容器映射中将端口添加为TCP端口,然后使用Cfn进行覆盖以更改为UDP。
container.addPortMappings({
containerPort: 7950,
hostPort: 7950,
protocol: EcsProtocol.TCP
}
);
const td = this.service.taskDefinition.node.defaultChild as CfnTaskDefinition;
td.addPropertyOverride('ContainerDefinitions.0.PortMappings.0.Protocol', 'udp');