Skip to content

Instantly share code, notes, and snippets.

@edwardrf
Created April 5, 2024 04:41
Show Gist options
  • Save edwardrf/83e552a0d9895639f993c319f0a6ccbe to your computer and use it in GitHub Desktop.
Save edwardrf/83e552a0d9895639f993c319f0a6ccbe to your computer and use it in GitHub Desktop.
Getting ALB arn from TargetGroup Arn
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/config"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %v <listenerArn>", os.Args[0])
os.Exit(1)
}
tgArn := os.Args[1]
ctx := context.Background()
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
svc := elbv2.NewFromConfig(cfg)
searchInput := &elbv2.DescribeTargetGroupsInput{TargetGroupArns: []string{tgArn}}
rulesOutput, err := svc.DescribeTargetGroups(ctx, searchInput)
if err != nil {
log.Fatalf("failed to run describe target group: %v", err)
}
for _, tg := range rulesOutput.TargetGroups {
fmt.Printf("TargetGroupArn: %v -> LoadBalancer %v\n", *tg.TargetGroupArn, tg.LoadBalancerArns)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment