https://pulumi.com logo
m

millions-furniture-75402

05/27/2020, 2:26 PM
How can I get an existing ApplicationLoadBalancer?
Copy code
// has missing properties error when I try to use it
const alb = pulumi.output(<http://aws.lb|aws.lb>.getLoadBalancer({
  // @ts-ignore
  arn: stackSandbox.getOutput("albArn")
}));

// declares a new ALB
const alb = new <http://awsx.lb|awsx.lb>.ApplicationLoadBalancer("test-default-lb", {
  // @ts-ignore
  arn: stackSandbox.getOutput("albArn"),
  subnets: stackSandbox.getOutput("publicSubnetIds").apply(v => v),
  vpc
});
a

ancient-megabyte-79588

05/27/2020, 2:36 PM
<http://awsx.lb|awsx.lb>.ApplicationLoadBalancer.get()
You'll need a name parameter and an id parameter if the AWS sdk is the same as the Azure sdk
m

millions-furniture-75402

05/27/2020, 2:40 PM
Copy code
TS2339: Property 'get' does not exist on type 'typeof ApplicationLoadBalancer'.
a

ancient-megabyte-79588

05/27/2020, 2:42 PM
Copy code
// Get reference to pre-existing Azure ResourceGroup
// get the Azure Resource Group
var resouceGroupId:pulumi.Output<string> = pulumi.interpolate `/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}`;
const resourceGroup = azure.core.ResourceGroup.get(resourceGroupName, resouceGroupId);
This is my code for getting an existing Resource Group in Azure
b

billowy-army-68599

05/27/2020, 2:43 PM
@millions-furniture-75402 you could also try the import https://www.pulumi.com/docs/guides/adopting/import/
m

millions-furniture-75402

05/27/2020, 2:43 PM
Thank you both, I will explore these options and get back to you
I assume I’ll be in a similar situation for the Listener I created in another stack as well
Would this be the correct syntax?
Copy code
const alb = new <http://awsx.lb|awsx.lb>.ApplicationLoadBalancer("test-default-lb", {
  // @ts-ignore
  arn: stackSandbox.getOutput("albArn"),
  subnets: stackSandbox.getOutput("publicSubnetIds").apply(v => v),
  vpc
}, { import: stackSandbox.getOutput("albArn") });
seems odd that I have to redeclare the vpc and subnets if I’m specifying the arn
a

ancient-megabyte-79588

05/27/2020, 2:48 PM
That looks like the right syntax. You may be able to leave the
options
object null or
{}
👍 1
b

billowy-army-68599

05/27/2020, 2:48 PM
your options have to match the current resource, or the import will fail
👍 1
m

millions-furniture-75402

05/27/2020, 2:50 PM
Is there a clever way to keep this DRYer? I’m guessing defining custom ComponentResources that hide this dirty work?
a

ancient-megabyte-79588

05/27/2020, 2:53 PM
I'm not sure importing a resource into a second stack is the right approach.
b

billowy-army-68599

05/27/2020, 2:53 PM
what exactly are you working on right now?
a

ancient-megabyte-79588

05/27/2020, 2:53 PM
If the resource is defined/created in the first stack, you should just
.get()
it in the second stack. the
import
directive will bring that resource into the second stack and start to manage it.
My resourceGroup example, that resource group is not managed by a Pulumi stack at all.
m

millions-furniture-75402

05/27/2020, 2:56 PM
I have a shared-infra stack that creates and ALB with default http and https listeners. Then I have an application stack where I want to create an application TargetGroup and attach it to the ALB, and add a ListenerRule to handle host-based routing.
The only thing I’m not declaring in the shared-infra stack is the VPC which is pre-existing.
a

ancient-megabyte-79588

05/27/2020, 2:57 PM
Your application stack shouldn't use the import directive imho
m

millions-furniture-75402

05/27/2020, 2:58 PM
@ancient-megabyte-79588 I’m not sure what to do if there is no .get() on the ApplicationLoadBalancer, other than redeclare it all without Crosswalk, which seems like a pain.
a

ancient-megabyte-79588

05/27/2020, 2:58 PM
@billowy-army-68599 should have some guidance! 😄
👍 1
I don't know why there would be no
.get()
method on the AWS SDK. 😞
b

billowy-army-68599

05/27/2020, 3:01 PM
yes, @ancient-megabyte-79588 is correct, you probably don't want to use import in this case. Our usual recommendation in this scenarios is to output the IDs/properties of the resources you declare in the shared infra stacks and use them as inputs in your dependent stacks, a bit like this https://github.com/pulumi/examples/tree/master/aws-stackreference-architecture
m

millions-furniture-75402

05/27/2020, 3:06 PM
yeah, that’s what I’m doing, but I need the ALB instance object to pass to my TargetGroup creation, for example.
Copy code
const appTargetGroup = new <http://awsx.lb|awsx.lb>.ApplicationTargetGroup(`${appName}-tg`, {
  loadBalancer: alb,
  ...
Similarly, I’ll have to use the instance of the https listener to add a rule
Copy code
const appListenerRule = new <http://awsx.lb|awsx.lb>.ListenerRule(`${appName}-lr`, httpsListener, {
  actions: [{
    targetGroupArn: appTargetGroup.targetGroup.arn,
    type: "forward",
  }],
 conditions: [{
    hostHeader: {
      values: [`${appName}.*`],
    },
  }],
  priority: 1,
});
Though against best practices, I tried the suggestion of using import, and pulumi is still trying to recreate the ALB:
Copy code
const lbSecurityGroup = new awsx.ec2.SecurityGroup(`${appName}-default-http-and-https`, {
  vpc
}, {
  // @ts-ignore
  import: stackSandbox.getOutput("lbSecurityGroupArn")
});

const alb = new <http://awsx.lb|awsx.lb>.ApplicationLoadBalancer("test-default-lb", {
  external: true,
  securityGroups: [lbSecurityGroup],
  // @ts-ignore
  subnets: stackSandbox.getOutput("publicSubnetIds").apply(v => v),
  vpc,
}, {
  // @ts-ignore
  import: stackSandbox.getOutput("albArn").apply(v => v)
});
I guess that’s because ApplicationLoadBalancer is an abstraction of many resources, and doesn’t have it’s own ID… so it has the resource IDs… but then I’m back to almost redefining the entire ApplicationLoadBalancer.
I guess for now, I’ll just have to spin up multiple load balancers as awsx does not seem to consider the use case for sharing an ALB with many applications.
a

ancient-megabyte-79588

05/27/2020, 4:48 PM
Why don't you spin up the ALB in the application since it is directly tied to the application more so than the infrastructure?
No point in having an ALB with nothing to run it against. 😄
m

millions-furniture-75402

05/27/2020, 5:02 PM
Was hoping to use 1 ALB for many application stacks that don’t see a lot of traffic
👍 1
f

famous-garage-15683

07/18/2020, 12:53 AM
I'd like to do this too. I want to send traffic to one of two application servers based on path. I'm new to Pulumi, so maybe I'm missing something obvious. But based on this thread it doesn't seem possible, so I filed a feature request https://github.com/pulumi/pulumi/issues/5024
👍 1
m

millions-furniture-75402

07/20/2020, 12:48 PM
Last I checked, this slack community will cull message to 10,000, so just be aware this conversation could disappear.
f

famous-garage-15683

07/20/2020, 8:37 PM
Thanks for the heads up. I sent I PR for this: https://github.com/pulumi/pulumi-awsx/pull/548. Now just need to wait for a review...
👍 1