Skip to content

AWS Route53 velke mazani

Export dat z AWS

# zjistim id zony
aws route53 list-hosted-zones
export ZONE_ID=XXXXXXXXXX

# export zaznamu do json
aws route53 list-resource-record-sets \
  --hosted-zone-id ${ZONE_ID} \
  --max-items 10000 \
  --output json > AWS_export-$(date +%F).json
 ```

## Script pro konverzi do formatu pro delete batch
```golang
package main

import (
    "encoding/json"
    "fmt"
    "html/template"
    "io/ioutil"
    "os"
)

type Record struct {
    Name  string        `json:"Name"`
    Type  string        `json:"Type"`
    TTL   int           `json:"TTL"`
    Value []interface{} `json:"ResourceRecords"`
    // Value string `json:"[0]Value"`
}

type SourceJson struct {
    Records []Record `json:"ResourceRecordSets"`
}

func main() {
    debug := false

    // read data from the given file
    inFilePath := os.Args[1]
    inFile, err := os.Open(inFilePath)
    if err != nil {
        panic(err)
    }
    defer inFile.Close()

    // load data to map
    var inData SourceJson // map[string]interface{}
    inByte, _ := ioutil.ReadAll(inFile)
    json.Unmarshal([]byte(inByte), &inData)

    //fmt.Println(inData["ResourceRecordSets"])
    if debug {
        for _, r := range inData.Records {
            fmt.Println(r)
            fmt.Println("............:")

        }
    }

    // Generate output
    t, err := template.ParseFiles("delete.template")
    if err != nil {
        fmt.Println(err)
    }

    t.Execute(os.Stdout, inData.Records)

}

Pouzita sablonu delete.template vypada takto

{
    "Comment": "Delete records",
    "Changes": [
{{ range . }}
        {
            "Action": "DELETE",
            "ResourceRecordSet": {
                "Name": "{{.Name}}",
                "Type": "{{.Type}}",
                "TTL": {{.TTL}},
                "ResourceRecords": [
                    {{- range .Value }}
                    {
                        "Value": "{{.Value}}"
                    }
                    {{- end }}
                ]
            }
        },
{{- end }}
    ]
}

Vygeneruji cely batch, ktery rozsekam na soubory po cca 10 tis radcich (800 zaznamu)

go run delete_records.go ./AWS_easyproject.com-backup.json > complete_delete_bulk.json

Samotne deleni je dobre delat v chytrem a vykonem editoru, lze to i scriptovat, ale toto je zrovna rychlejsi udelat hloupe

Samotne mazani

tento script volam pro kazdy soubor. Typicky se pri provadeni objevuji chyby, ktere je nutne resit bud opravou syntaxe, nebo mazanim sekvenci ze souboru a naslednem rucnim mazani v AWS consoli.

aws route53 change-resource-record-sets --hosted-zone-id ${ZONE_ID} --change-batch file:///Users/marek/tmp/delete_X.json