This message was deleted.
# aws
s
This message was deleted.
w
Copy code
import * as awsx from "@pulumi/awsx";                                              
import { project, baseTags } from '../config';                                     

const vpc = awsx.ec2.Vpc.getDefault();                                             

const alb = new awsx.lb.ApplicationLoadBalancer(`${project}-lb`, {                 
  vpc,                                                                             
  tags: baseTags,                                                                  
  external: true,                                                                                                                                               
});                                                                                

export const http = alb.createListener(`${project}-http`, {                                                              
  defaultActions: [{                                                               
    fixedResponse: {                                                               
      contentType: "text/plain",                                                   
      messageBody: "404 Site Not Found",                                           
      statusCode: "404",                                                           
    },                                                                             
    type: "fixed-response",                                                        
  }],                                        
  external: true,                                                               
  port: 80,                                                                     
  protocol: "HTTP",                                                             
});                                                                             

export const nginxTarget = alb.createTargetGroup(`${project}-nginx-tg`, {       
  port: 80,                                                                     
  protocol: "HTTP",                                                             
  tags: baseTags,                                                               
});                                                                             

export const helloTarget = alb.createTargetGroup(`${project}-hello-tg`, {       
  port: 80,                                                                     
  protocol: "HTTP",                                                             
  tags: baseTags,                                                               
});                                                                             

const nginxRule = http.addListenerRule(`${project}-nginx-lr`, {                 
  actions: [{                                                                   
    targetGroupArn: nginxTarget.targetGroup.arn.apply(v => v),                  
    type: "forward",                                                            
  }],                                                                           
  conditions: [{                                                                
    pathPattern: {                                                              
      values: ["/nginx"],                                                       
    }},{                                                                          
    hostHeader: {                                                               
      values: [`*`],                                                            
    },                                                                          
  }],                                                                           
  priority: 1,                                                                  
});                                                                             

const helloRule = http.addListenerRule(`${project}-hello-lr`, {                 
  actions: [{                                                                   
    targetGroupArn: helloTarget.targetGroup.arn.apply(v => v),                  
    type: "forward",                                                            
  }],                                                                           
  conditions: [{                                                                
    pathPattern: {                                                              
      values: ["/hello"],                                                       
    }},{                                                                          
    hostHeader: {                                                               
      values: [`*`],                                                            
    },                                                                          
  }],                                                                           
  priority: 1,                                                                  
});                                                                             

const cluster = new awsx.ecs.Cluster("cluster", {                                  
  vpc,                                                                             
  tags: baseTags,                                                                  
});                                                                                

const nginxService = new awsx.ecs.FargateService("backend-nginx", {                
    cluster,                                                                       
    taskDefinitionArgs: {                                                          
        containers: {                                                              
            nginx: {                                                               
                image: "nginx",                                                    
                cpu: 128,                                                          
                memory: 128,                                                       
                portMappings: [ nginxTarget ],                                     
            },                                                                     
        },                                                                         
    },                                                                             
    desiredCount: 3,                                                               
});                                                                                

const img = awsx.ecs.Image.fromPath("hello-img", "./app");                         

const helloService = new awsx.ecs.FargateService("backend-hello", {                
    cluster,                                                                       
    taskDefinitionArgs: {                                                          
        container: {                                                               
            image: img,                                                            
            cpu: 128,                                                              
            memory: 128,                                                           
            portMappings: [ helloTarget ],                                         
        },                                                                         
    },                                                                             
    desiredCount: 5,                                                               
});
l
I'm checking the code at https://github.com/pulumi/pulumi-awsx/blob/4119d2b693ea61987208de9543d19cf534046adf/nodejs/awsx/lb/application.ts#L102 and I don't see that it gets a TargetGroupAttachment. It looks like you do that via the listener's attachTarget method.
w
Thank you for the quick reply 👍 In the same file Line 80, the method I called will add itself to the targetGroup loadBalancer.
I don't know if I understand you correctly, but the error that the targetGroup
does not have an associated load balancer
shouldn't happen.
l
Yes the load balancer has a target group, but as I understand it, the target group also need a load balancer listener. I can't tell if the error message is misleading, so I thought trying to finish the circle and add in the missing target group attachment might eliminate some possibilities.
w
I don't understand, in the code the
listeners
in
ApplicationTargetGroup
is set to
[]
and seems not used.
l
To get traffic from a load-balanced port (a listener) to a load-balanced set of machines (a target group), you need to attach these two things. There is a targetGroupAttachment resource that accomplishes this. This might not be the problem in this case, but you don't yet seem to have one, so I wondered if you'd like to create the missing resource in order to rule that out as a cause of this problem.
In the awsx.lb package, this is achieved by calling Listener.attachTarget().
w
Thanks, Do you know what parameter I should give to
Listener.attachTarget()
?
l
The args parameter has just a mandatory targetId (the ECS container ID). You can optionally include an availabilityZone and port, but those are only needed if you're not using the defaults set up on the listener.
w
Sorry for the confusion, problem solved. It turns out to be a wrong configuration in the listenerRules. The error message was really mis-leading. Thank you @little-cartoon-10569 👍
👍 1