diff --git a/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml b/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml index a9977455de4..30e55b2809a 100644 --- a/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml +++ b/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml @@ -95,8 +95,6 @@ Resources: # If a bucket URL is specified, that means the template exists. awslogs-stream-prefix: copilot PortMappings: - ContainerPort: !Ref ContainerPort - - ContainerPort: 82 - Protocol: tcp - Name: tls Image: 1234567890.dkr.ecr.us-west-2.amazonaws.com/proxy:cicdtest PortMappings: diff --git a/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml b/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml index 8d68f89be06..0baab5c7fb6 100644 --- a/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml +++ b/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml @@ -29,17 +29,14 @@ {{- if eq .WorkloadType "Load Balanced Web Service"}} PortMappings: - ContainerPort: !Ref ContainerPort -{{- if .NLB}} - {{if ne .NLB.Listener.TargetPort .NLB.MainContainerPort}} {{/*No need to add additional port if the target port is the same as image port*/}} - - ContainerPort: {{.NLB.Listener.TargetPort}} - {{- if eq .NLB.Listener.Protocol "UDP" }} - Protocol: udp - {{- else }} - Protocol: tcp - {{- end }} +{{- if .NLB}} {{ $nlbListener := .NLB.Listener }} + {{- if and (eq $nlbListener.TargetContainer .WorkloadName) (ne $nlbListener.TargetPort .NLB.MainContainerPort)}} + {{/*No need to add additional port if the target port is the same as image port*/}} + - ContainerPort: {{$nlbListener.TargetPort}} + Protocol: {{if eq $nlbListener.Protocol "UDP"}}udp{{else}}tcp{{end}} {{- end}} {{- end}} -{{- end}} +{{- end}} {{/* end if eq .WorkloadType "Load Balanced Web Service"*/}} {{- if eq .WorkloadType "Backend Service"}} PortMappings: !If [ExposePort, [{ContainerPort: !Ref ContainerPort}], !Ref "AWS::NoValue"] {{- end}}