[英]Creating an A record to point to S3
I have an S3 bucket named foo.example.com
which is configured for static website hosting, with the endpoint http://foo.example.com.s3-website-us-east-1.amazonaws.com
.我有一个名为
foo.example.com
的 S3 存储桶,它配置为 static 网站托管,端点http://foo.example.com.s3-website-us-east-1.amazonaws.com
。 I'm trying to create an A record in a hosted zone of the same name to point to this S3 bucket.我正在尝试在同名的托管区域中创建一条 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
}
}
}
However I get the following error:但是我收到以下错误:
> InvalidChangeBatch: [Invalid Resource Record: 'FATAL problem:
> ARRDATAIllegalIPv4Address (Value is not a valid IPv4 address)
> encountered with 'http://s3-website-us-east-1.amazonaws.com'']
How do I properly reference the S3 endpoint in the hosted zone?如何正确引用托管区域中的 S3 端点?
You want to create an Alias Record , which allows you to target a DNS name.您想要创建一个Alias Record ,它允许您将 DNS 名称作为目标。 Plain-old
A
records only accept IPv4 addresses.普通的旧
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"),
...
See the examples in the V1 Go SDK repo.请参阅 V1 Go SDK 存储库中的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.