Route53:按域名查询域记录

问题描述 投票:0回答:1

使用Go和AWS-SDK

我正在尝试查询Route53 - > Hosted Zones下AWS Console中列出的route53 CNAME和A记录。我能够使用以下代码进行查询,但它需要(隐藏的)HostedZoneId我必须提前知道。

是否有不同的功能,或基于域名的HostedZoneId查找,如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)

编辑:哦,另外,具有值“CNAME”的StartRecordType会抛出验证错误,所以我不确定我应该在那里使用什么。

go amazon-route53
1个回答
1
投票

首先必须进行查找以获取HostedZoneID。这是我为它写的功能。 :

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
}
© www.soinside.com 2019 - 2024. All rights reserved.