Load balancing traffic with weighted DNS routing
TypeScriptTo achieve load balancing traffic with weighted DNS routing, you can use Amazon Route 53, AWS's Domain Name System (DNS) web service. The AWS
aws.route53.TrafficPolicy
resource can be used to route traffic based on various routing policies, including weighted policies. In a weighted routing policy, you can assign weights to different DNS records to manage the proportion of traffic that goes to each endpoint.For this example, we assume that you have two different application endpoints and you want to balance the traffic between them, sending 80% of the requests to one and 20% to the other. These endpoints might be addresses to different Amazon Elastic Compute Cloud (EC2) instances, Elastic Load Balancer Addresses, or globally distributed AWS resources.
Below is a Pulumi program written in TypeScript that will create a DNS zone and set up the weighted routing policy:
import * as aws from "@pulumi/aws"; // Create a new Route 53 zone const zone = new aws.route53.Zone("myZone", { name: "example.com" }); // Define the IP addresses for your application endpoints const primaryAppEndpoint = "1.2.3.4"; // The IP address for the primary application endpoint const secondaryAppEndpoint = "5.6.7.8"; // The IP address for the secondary application endpoint // Create two Route 53 records in the zone for the two application endpoints const primaryRecord = new aws.route53.Record("primaryRecord", { zoneId: zone.zoneId, name: "primary.example.com", type: "A", ttl: 300, records: [primaryAppEndpoint], }); const secondaryRecord = new aws.route53.Record("secondaryRecord", { zoneId: zone.zoneId, name: "secondary.example.com", type: "A", ttl: 300, records: [secondaryAppEndpoint], }); // Define the traffic policy document const trafficPolicyDocument = { "RecordType": "A", "StartRule": "weighted_rule", "EndPoints": { "primary": { "Value": primaryRecord.fqdn, "Weight": 80, }, "secondary": { "Value": secondaryRecord.fqdn, "Weight": 20, }, }, }; // Create a traffic policy const trafficPolicy = new aws.route53.TrafficPolicy("weightedTrafficPolicy", { name: "WeightedTrafficPolicy", document: JSON.stringify(trafficPolicyDocument), }); // Create a traffic policy instance const trafficPolicyInstance = new aws.route53.TrafficPolicyInstance("myWeightedPolicyInstance", { trafficPolicyId: trafficPolicy.id, trafficPolicyVersion: trafficPolicy.latestVersion, hostedZoneId: zone.zoneId, ttl: 60, recordType: "A", name: "api.example.com", }); // Export the DNS name of the record that points to the weighted traffic policy instance export const weightedDnsName = trafficPolicyInstance.name;
This program starts by importing the necessary AWS package for Pulumi, which provides resources to interact with AWS services.
- We define a new DNS zone with the domain name
example.com
usingaws.route53.Zone
. - Next, we create two A records in this zone with mock IP addresses, representing two application endpoints.
- We then create the traffic policy document as a JSON object directly in the code. It defines the weight for each endpoint, with an 80:20 split between primary and secondary endpoints.
- Using the traffic policy document, we create an AWS Route 53 traffic policy resource named
weightedTrafficPolicy
. - Then we instantiate the traffic policy within the zone created earlier, effectively applying the weighted routing policy. This instance will respond with either IP based on the assigned weights when queries come for the
api.example.com
record. - Finally, we export the DNS name of the weighted record so it can be used outside of Pulumi.
Any incoming DNS requests to
api.example.com
would now be subject to the weights defined, routing traffic across the two A records accordingly.Please replace the example IP addresses with your actual application endpoints and domain names as needed. You should ensure the AWS CLI is configured with appropriate credentials and default region for this Pulumi program to work as expected.
- We define a new DNS zone with the domain name