简体   繁体   中英

CDK: Use a UDP port mapping with NLB and ECS/Fargate service

I am trying to use UDP port when adding port mapping for a Network load balanced Fargate service but cdk build fails with below error.

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] 

Relevant code:

        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
            }
        );

Found that the same issue was encountered with couple others: https://github.com/aws/containers-roadmap/issues/445#issuecomment-714930539

Is it possible to use configure UDP port for Network load balanced Fargate service using CDK?

I found a work around, initially in container mappings added the port as TCP port, then done an override using Cfn to change to 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM