Skip to content

Commit

Permalink
Add README and new elb-name tool
Browse files Browse the repository at this point in the history
  • Loading branch information
hamstah committed Sep 25, 2017
1 parent 9847e66 commit d5ce33c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions elb-name/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d5ce33c

Please sign in to comment.