-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
170 lines (143 loc) · 4.44 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
"github.com/hailocab/dns-lambda/aws"
"github.com/hailocab/dns-lambda/lambda"
"github.com/apex/go-apex"
"github.com/apex/go-apex/cloudwatch"
"github.com/k0kubun/pp"
)
var (
configFile string
)
func init() {
flagSet := flag.NewFlagSet("lambda", flag.ContinueOnError)
flagSet.StringVar(&configFile, "config-file", lambda.DefaultConfigFile, "Location of config file")
if err := flagSet.Parse(os.Args[1:]); err != nil {
os.Exit(1)
}
log.SetOutput(os.Stderr)
}
func main() {
log.Printf("Using log file: %s", configFile)
cloudwatch.HandleFunc(func(evt *cloudwatch.Event, ctx *apex.Context) error {
if evt.Source != "aws.autoscaling" {
return errors.New("Not an autoscaling event")
}
var details cloudwatch.AutoScalingGroupDetail
if err := json.Unmarshal(evt.Detail, &details); err != nil {
log.Printf("Unable to unmarshal detail body: %v", err)
return err
}
config, err := lambda.LoadConfig(configFile)
if err != nil {
log.Printf("Config error: %v", err)
return err
}
pp.Print(config)
if config.CreateIPRecords {
ip, err := aws.LookupIPAddress(details.EC2InstanceID, evt.Region, config.Domain)
if err != nil {
log.Printf("Unable to find IP for %q: %v", details.EC2InstanceID, err)
return err
}
record, _ := lambda.IPRecordPattern.Parse(map[string]string{
"IP": strings.Replace(ip, ".", "-", -1),
"Region": evt.Region,
"Domain": config.Domain,
})
ipRecordConfig := &aws.IPRecordConfig{
Zone: config.HostedZoneID,
Record: record,
Value: ip,
InstanceID: details.EC2InstanceID,
Domain: config.Domain,
Region: evt.Region,
}
switch lambda.DetermineAutoScalingEventType(evt.DetailType) {
case lambda.AutoScalingEventLaunch:
log.Printf("Creating IP based DNS record for %q", details.EC2InstanceID)
if err := aws.CreateIPRecord(ipRecordConfig); err != nil {
log.Printf("Unable to create record: %q: %v", record, err)
return err
}
case lambda.AutoScalingEventTerminate:
log.Printf("Removing IP based DNS record for %q", details.EC2InstanceID)
if err := aws.DeleteIPRecord(ipRecordConfig); err != nil {
log.Printf("Unable to create record: %q: %v", record, err)
return err
}
}
}
log.Printf("Finding instances for %q in %q", details.AutoScalingGroupName, evt.Region)
resource, err := aws.FindInstances(details.AutoScalingGroupName, evt.Region)
if err != nil {
log.Printf("Unable to find instances for %q: %v", details.AutoScalingGroupName, err)
return err
}
var (
azIPs = map[string][]string{}
allIPs []string
role string
)
roleRE := regexp.MustCompile(fmt.Sprintf("-%s", config.EnvironmentName))
role = roleRE.ReplaceAllLiteralString(details.AutoScalingGroupName, "")
for az, instances := range resource.InstancesByAvailabilityZone {
azIPs[az] = []string{}
for _, i := range instances {
log.Printf("Found instance %q (%s) in %q", i.InstanceID, i.PrivateIPAddress, az)
azIPs[az] = append(azIPs[az], i.PrivateIPAddress)
allIPs = append(allIPs, i.PrivateIPAddress)
}
p, ok := config.Patterns["az"]
if ok {
dns, err := p.Parse(map[string]string{
"AutoScalingGroup": details.AutoScalingGroupName,
"Role": role,
"AvailabilityZone": az,
"EnvironmentName": config.EnvironmentName,
})
if err != nil {
return err
}
if len(azIPs[az]) == 0 {
log.Printf("Tring to delete DNS record for: %s", dns)
aws.DeleteRecord(config.HostedZoneID, dns)
} else {
if err := aws.CreateRecord(config.HostedZoneID, dns, azIPs[az]); err != nil {
return fmt.Errorf("Unable to create AZ record %q: %v (%v)", dns, azIPs[az], err)
}
}
}
}
p, ok := config.Patterns["region"]
if ok {
dns, err := p.Parse(map[string]string{
"AutoScalingGroup": details.AutoScalingGroupName,
"Role": role,
"Region": evt.Region,
"EnvironmentName": config.EnvironmentName,
})
if err != nil {
return err
}
if len(allIPs) == 0 {
log.Printf("Tring to delete DNS record for: %s", dns)
err := aws.DeleteRecord(config.HostedZoneID, dns)
log.Printf("Delete: %v", err)
} else {
if err := aws.CreateRecord(config.HostedZoneID, dns, allIPs); err != nil {
return fmt.Errorf("Unable to create region record %q: %v ", dns, allIPs)
}
}
}
return nil
})
}