https://pulumi.com logo
Title
r

red-football-97286

05/25/2021, 5:35 PM
I'm using TypeScript and creating some AWS listeners. I want two different ports 443 and 22. I need a clean way to create these without duplicating the new construct. The port property only takes an single string and not an array. Any ideas?
b

billowy-army-68599

05/25/2021, 5:58 PM
can you share your code?
s

shy-waiter-84958

05/25/2021, 6:10 PM
If you're just trying to be DRY, something like this may work. If I have resources with mostly identical config, but differences in ports/sg/etc... I'll create loops like this to avoid recreating the resource over and over.
const listenerPorts = [22, 443];
const albListerners: aws.lb.Listener[] = [];

for (
  const range = { value: 0 };
  range.value < listenerPorts.length;
  range.value++
) {
  albListerners.push(
    new aws.lb.Listener(`listener-${range.value}`, {
      loadBalancerArn: pulumi.output(alb).arn,
      port: listenerPorts[range.value],
      protocol: "HTTPS",
      sslPolicy: "ELBSecurityPolicy-TLS-1-2-2017-01",
      certificateArn: pulumi.output(acmCert).arn,
      defaultActions: [
        {
          targetGroupArn: pulumi.output(albTargetGroup).arn,
          type: "forward",
        },
      ],
    })
  );
}
r

red-football-97286

05/25/2021, 6:14 PM
That looks interesting, thanks @shy-waiter-84958. Yes, trying to keep it DRY.