-
-
Save Konboi/afa7a8607ef1742f5ddb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
current_ip := currentExternalIp() | |
resp, err := client.ListResourceRecordSets(&route53.ListResourceRecordSetsInput{ | |
HostedZoneId: aws.String(zone_id), | |
}) | |
if err != nil { | |
panic(err) | |
} | |
for _, record := range resp.ResourceRecordSets { | |
if *record.Type == "A" && *record.Name == domain { | |
if strings.Compare(*record.ResourceRecords[0].Value, current_ip) != 0 { | |
log.Printf("current set ip %s", *record.ResourceRecords[0].Value) | |
log.Printf("current ip %s", current_ip) | |
updateRecordValue(current_ip) | |
} else { | |
log.Println("no update") | |
} | |
} | |
} | |
} | |
func currentExternalIp() (ip string) { | |
resp, err := http.Get("http://myexternalip.com/raw") | |
if err != nil { | |
log.Fatalf("Get ExterlIP Error %s", err.Error()) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatalf("Get ExterlIP Error %s", err.Error()) | |
} | |
ip = strings.TrimSpace(string(body)) | |
return ip | |
} | |
func updateRecordValue(current_ip string) { | |
resource_record := &route53.ResourceRecord{Value: aws.String(current_ip)} | |
resource_records := []*route53.ResourceRecord{resource_record} | |
resource_record_set := &route53.ResourceRecordSet{ | |
Name: aws.String(domain), | |
TTL: aws.Int64(60), | |
Type: aws.String("A"), | |
ResourceRecords: resource_records, | |
} | |
change := &route53.Change{ | |
Action: aws.String("UPSERT"), | |
ResourceRecordSet: resource_record_set, | |
} | |
change_batch := &route53.ChangeBatch{ | |
Changes: []*route53.Change{change}, | |
} | |
change_resource_record_set_input := &route53.ChangeResourceRecordSetsInput{ | |
HostedZoneId: aws.String(zone_id), | |
ChangeBatch: change_batch, | |
} | |
response, err := client.ChangeResourceRecordSets(change_resource_record_set_input) | |
if err != nil { | |
log.Fatalf("Route53 Record Update Error %s", err.Error()) | |
} | |
fmt.Printf("%#v", response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment