Home > Enterprise >  Creating an A record to point to S3
Creating an A record to point to S3

Time:01-02

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. I'm trying to create an A record in a hosted zone of the same name to point to this S3 bucket.

    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?

CodePudding user response:

You want to create an Alias Record, which allows you to target a DNS name. Plain-old A records only accept IPv4 addresses.

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.

  • Related