Search code examples
node.jsamazon-web-servicesamazon-s3amazon-route53

AWS - route 53 pointing to s3 bucket , Error is the alias target name does not lie within the target zone


Full Error message - InvalidChangeBatch: [Tried to create an alias that targets s3-website-us-west-2.amazonaws.com., type A in zone ----- , but the alias target name does not lie within the target zone]

Trying to create route 53 new record and point to s3 bucket from nodejs request

I have created a s3 bucket with same name test.mydomain.com

var route53 = new AWS.Route53({
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey,
    region: region
});
let subDomainName = "test.mydomain.com";
let HostedZoneId = "idssss" (copied from route 53 where added mydomain)
var params = {
    ChangeBatch: {
        Changes: [
            {
                Action: "CREATE",
                ResourceRecordSet: {
                    AliasTarget: {
                        DNSName: "s3-website-us-west-2.amazonaws.com",
                        EvaluateTargetHealth: false,
                        HostedZoneId: HostedZoneId
                    },
                    Name: subDomainName,
                    Type: "A"
                }
            }
        ]
    },
    HostedZoneId: HostedZoneId
};
route53.changeResourceRecordSets(params, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data);
});

What is the mistake here? I am using same HostedZoneId two times , one is under AliasTarget , Is that correct ?


Solution

  • The AliasTarget documentation indicates that HostedZoneId should be set as follows for an Amazon S3 bucket configured as a static website:

    Specify the hosted zone ID for the region that you created the bucket in.

    You can find a list of S3 regional information at Amazon S3 website endpoints. The Website Endpoint for US West (Oregon) is s3-website-us-west-2.amazonaws.com and the Hosted Zone ID is Z3BJ6K6RIION7M.

    Also, see Routing traffic to a website that is hosted in an Amazon S3 bucket.

    Note that after you make these Route 53 changes, they will be in status PENDING which indicates that the changes in the request aren't yet propagated to all Route 53 DNS servers. This is the initial status of all change batch requests. The status will change to INSYNC at some point to indicate that the changes have been propagated to all Route 53 DNS servers.