Route53:按域名查询域记录

Using Go and AWS-SDK

I'm attempting to query route53 CNAME and A records as listed in the AWS Console under Route53 -> Hosted Zones. I'm able to query using the following code, but it requires the (cryptic) HostedZoneId I have to know ahead of time.

Is there a different function, or a HostedZoneId lookup based on the Domain Name such as XXX.XXX.com ?

    AWSLogin(instance)

    svc := route53.New(instance.AWSSession)

    listParams := &route53.ListResourceRecordSetsInput{
        HostedZoneId: aws.String("Z2798GPJN9CUFJ"), // Required
        // StartRecordType: aws.String("CNAME"),
    }
    respList, err := svc.ListResourceRecordSets(listParams)

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // Pretty-print the response data.
    fmt.Println("All records:")
    fmt.Println(respList)

edit: oh, additionally, the StartRecordType with the value "CNAME" throws a validation error, so I'm not sure what I should be using there.

You first have to do a lookup to get the HostedZoneID. Here is the func I wrote for it. :

func GetHostedZoneIdByNameLookup(awsSession string, HostedZoneName string) (HostedZoneID string, err error) {

    svc := route53.New(awsSession)

    listParams := &route53.ListHostedZonesByNameInput{
        DNSName: aws.String(HostedZoneName), // Required
    }
    req, resp := svc.ListHostedZonesByNameRequest(listParams)
    err = req.Send()
    if err != nil {
        return "", err
    }

    HostedZoneID = *resp.HostedZones[0].Id

    // remove the /hostedzone/ path if it's there
    if strings.HasPrefix(HostedZoneID, "/hostedzone/") {
        HostedZoneID = strings.TrimPrefix(HostedZoneID, "/hostedzone/")
    }

    return HostedZoneID, nil
}