创建一条指向S3的A记录



我有一个名为foo.example.com的S3桶,它配置为静态网站托管,端点为http://foo.example.com.s3-website-us-east-1.amazonaws.com。我正试图在同名的托管区域中创建一个A记录,以指向这个S3桶。

package main 

import (
"log"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
)

func main () {
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
})

// Create a Route53 service client
route53Svc := route53.New(sess)

// Set the name of the hosted zone
hostedZoneName := "foo.example.com"

// Set the maximum number of hosted zones to return
maxItems := int64(10)

// Call the ListHostedZonesByName action
result, err := route53Svc.ListHostedZonesByName(&route53.ListHostedZonesByNameInput{
DNSName:  aws.String(hostedZoneName + "."),
MaxItems: aws.String(strconv.FormatInt(maxItems, 10)),
})
if err != nil {
log.Fatalf("Error listing hosted zones: %s", err)
}

// Iterate through the list of hosted zones
for _, hz := range result.HostedZones {

// Check if the hosted zone name matches the specified name
if *hz.Name == hostedZoneName + "." {

// Get the hosted zone ID
hostedZoneID := aws.StringValue(hz.Id)

val := "http://s3-website-us-east-1.amazonaws.com"

// Create an A record for the bucket in the desired hosted zone
_, err = route53Svc.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(hostedZoneID),
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{
{
Action: aws.String("CREATE"),
ResourceRecordSet: &route53.ResourceRecordSet{
Type: aws.String("A"),
Name: aws.String(hostedZoneName),
ResourceRecords: []*route53.ResourceRecord{
{
Value: aws.String(val),
},
},
TTL: aws.Int64(300),
},
},
},
},
})
if err != nil {
log.Fatalf("%s",err)
}
break
} 
}
}

但是我得到以下错误:

> InvalidChangeBatch: [Invalid Resource Record: 'FATAL problem:
> ARRDATAIllegalIPv4Address (Value is not a valid IPv4 address)
> encountered with 'http://s3-website-us-east-1.amazonaws.com'']

如何正确引用托管区域中的S3端点?

您想要创建一个别名记录,它允许您以DNS名称为目标。普通A记录只接受IPv4地址

Action: aws.String("CREATE"),
ResourceRecordSet: &route53.ResourceRecordSet{
AliasTarget: &route53.AliasTarget{
DNSName:              aws.String("foo.example.com.s3-website-us-east-1.amazonaws.com"),
EvaluateTargetHealth: aws.Bool(true),
HostedZoneId:         aws.String("Z3AADJGX6KTTL2"),
},
Name:          aws.String("foo.example.com"),
Type:          aws.String("A"),
...

参见V1 Go SDK repo中的示例。

最新更新