Last active
October 18, 2016 01:16
-
-
Save oremj/83f8600b8dec5abd9623620cf2334966 to your computer and use it in GitHub Desktop.
migrates a zonefile to a route53 changeset
This file contains hidden or 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
| package main | |
| import ( | |
| "bufio" | |
| "encoding/json" | |
| "flag" | |
| "log" | |
| "os" | |
| "strings" | |
| "github.com/aws/aws-sdk-go/aws" | |
| "github.com/aws/aws-sdk-go/aws/session" | |
| "github.com/aws/aws-sdk-go/service/route53" | |
| ) | |
| var zoneId = flag.String("zone-id", "", "the zone to modify") | |
| func main() { | |
| scanner := bufio.NewScanner(os.Stdin) | |
| changeBatch := &route53.ChangeBatch{ | |
| Changes: make([]*route53.Change, 0), | |
| } | |
| for scanner.Scan() { | |
| record := scanner.Text() | |
| fields := strings.Fields(record) | |
| typeIdx := 2 | |
| if len(fields) == 5 { | |
| typeIdx = 3 | |
| } | |
| name, recType, alias := fields[0], fields[typeIdx], fields[typeIdx+1] | |
| changeBatch.Changes = append(changeBatch.Changes, makeChange(name, recType, alias)) | |
| } | |
| flag.Parse() | |
| if *zoneId == "" { | |
| enc := json.NewEncoder(os.Stdout) | |
| enc.SetIndent("", " ") | |
| enc.Encode(changeBatch) | |
| os.Exit(0) | |
| } | |
| r53 := route53.New(session.New()) | |
| _, err := r53.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{ | |
| ChangeBatch: changeBatch, | |
| HostedZoneId: aws.String(*zoneId), | |
| }) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| } | |
| func makeChange(name, recType, alias string) *route53.Change { | |
| change := &route53.Change{ | |
| Action: aws.String(route53.ChangeActionCreate), | |
| } | |
| recSet := &route53.ResourceRecordSet{ | |
| Name: aws.String(name), | |
| } | |
| if strings.HasSuffix(alias, "cloudfront.net.") { | |
| recSet.Type = aws.String(route53.RRTypeA) | |
| recSet.AliasTarget = &route53.AliasTarget{ | |
| DNSName: aws.String(alias), | |
| EvaluateTargetHealth: aws.Bool(false), | |
| HostedZoneId: aws.String("Z2FDTNDATAQYW2"), | |
| } | |
| } else { | |
| recSet.TTL = aws.Int64(60) | |
| recSet.Type = aws.String(recType) | |
| recSet.ResourceRecords = []*route53.ResourceRecord{ | |
| {Value: aws.String(alias)}, | |
| } | |
| } | |
| change.ResourceRecordSet = recSet | |
| return change | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment