-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
84 lines (74 loc) · 1.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/tidwall/gjson"
)
var (
hostedZoneID = "" // NOTE: add your hosted zone id
)
func main() {
sess := session.Must(session.NewSession())
awsConf := aws.NewConfig()
// ARN of the IAM role to assume
// Remove the next two lines if you are not working with assumed roles or add your role
creds := stscreds.NewCredentials(sess, "")
awsConf.WithCredentials(creds)
svc := route53.New(sess, awsConf)
rrs, err := ListAllRecordSets(svc, hostedZoneID)
if err != nil {
log.Fatal(err)
}
b, err := json.Marshal(rrs)
json := string(b)
if err != nil {
log.Fatal(err)
}
if !gjson.Valid(json) {
log.Fatal("invalid json")
}
data := gjson.Parse(json)
data.ForEach(func(key, value gjson.Result) bool {
raw := value.Raw
name := getKey(&raw, "Name")
typ := getKey(&raw, "Type")
aliasTarget := getKey(&raw, "AliasTarget.DNSName")
// INFO: Only prints A type records
if typ == "A" {
fmt.Println(name, aliasTarget)
}
return true // keep iterating
})
}
// ListAllRecordSets returns a ResourceRecordSet containing all records
func ListAllRecordSets(r53 *route53.Route53, id string) (rrsets []*route53.ResourceRecordSet, err error) {
req := route53.ListResourceRecordSetsInput{
HostedZoneId: &id,
}
// iterate over pagination
for {
var resp *route53.ListResourceRecordSetsOutput
resp, err = r53.ListResourceRecordSets(&req)
if err != nil {
return
} else {
rrsets = append(rrsets, resp.ResourceRecordSets...)
if *resp.IsTruncated {
req.StartRecordName = resp.NextRecordName
req.StartRecordType = resp.NextRecordType
req.StartRecordIdentifier = resp.NextRecordIdentifier
} else {
break
}
}
}
return
}
func getKey(json *string, key string) string {
return gjson.Get(*json, key).String()
}