From d5ce33c4e99c545b3ef2fdf539081a5fd0f31d80 Mon Sep 17 00:00:00 2001 From: hamstah Date: Mon, 25 Sep 2017 18:18:35 +0100 Subject: [PATCH] Add README and new elb-name tool --- README.md | 9 ++++++ elb-name/main.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 README.md create mode 100644 elb-name/main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..f9cb5ce --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# AWS tools + +Some specialised tools to avoid pulling boto3 + +* `elb`: ELB classic only (no ALB). Given a name returns the zone53 record associated with the ELB, including scheme (https returned if both available) and port. +* `elb-name`: Both ELB classic and ALB. Given a name, returns route53 record associated with the ELB. Does not include scheme or port as it doesn't check listeners. +* `s3-download`: Download a single file from s3 +* `ec2-ip-from-name`: Given an EC2 name, list up to `-max-results` IPs associated with instances with that name (default is 1). +* `ecs`: Run a task definition diff --git a/elb-name/main.go b/elb-name/main.go new file mode 100644 index 0000000..3724516 --- /dev/null +++ b/elb-name/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "flag" + "fmt" + "os" + "strings" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/elbv2" + "github.com/aws/aws-sdk-go/service/route53" +) + +func main() { + + loadBalancerName := flag.String("name", "", "Name of the load balancer") + region := flag.String("region", "eu-west-1", "AWS region") + flag.Parse() + + if len(*loadBalancerName) < 1 { + fmt.Println("Missing load balancer name") + os.Exit(1) + } + + config := aws.Config{Region: aws.String(*region)} + session := session.New(&config) + + elb_svc := elbv2.New(session) + params := &elbv2.DescribeLoadBalancersInput{ + Names: []*string{aws.String(*loadBalancerName)}, + } + resp, err := elb_svc.DescribeLoadBalancers(params) + + if err != nil { + fmt.Println(err.Error()) + os.Exit(3) + } + + l := len(resp.LoadBalancers) + if l < 0 { + fmt.Println("No load balancer found") + os.Exit(2) + } + + dnsName := *resp.LoadBalancers[0].DNSName + dnsNameDot := fmt.Sprintf("%s.", dnsName) + route53_svc := route53.New(session) + + zones, err := route53_svc.ListHostedZones(&route53.ListHostedZonesInput{}) + + if err != nil { + fmt.Println(err.Error()) + os.Exit(3) + } + for _, hostedZone := range zones.HostedZones { + zoneId := hostedZone.Id + + records, err := route53_svc.ListResourceRecordSets(&route53.ListResourceRecordSetsInput{HostedZoneId: zoneId}) + if err != nil { + fmt.Println(err.Error()) + break + } + for _, record := range records.ResourceRecordSets { + if record.AliasTarget == nil || record.AliasTarget.DNSName == nil { + continue + } + if *record.AliasTarget.DNSName == dnsNameDot { + dnsName = strings.TrimRight(*record.Name, ".") + break + } + + } + } + fmt.Println(dnsName) +}