Skip to content

Commit 54f20f0

Browse files
Add sample for Route53 DNS failover (#221)
* Add sample for Route53 DNS failover * Add readme and make small fixes * Add makefile * minor changes to enable running under MacOS/M1 --------- Co-authored-by: Waldemar Hummer <[email protected]>
1 parent 476cf61 commit 54f20f0

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

route53-dns-failover/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: run
2+
3+
run:
4+
bash run_demo.sh

route53-dns-failover/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This sample demonstrates the Route53 DNS failover based on health check in LocalStack.
2+
3+
We suggest taking a look at [run_demo.sh](run_demo.sh) script to understand the setup.
4+
5+
To run the demo:
6+
7+
```
8+
$ LOCALSTACK_API_KEY=<your-api-key> docker-compose up -d
9+
```
10+
11+
```
12+
$ make run
13+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: "3.8"
2+
3+
services:
4+
localstack:
5+
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
6+
image: localstack/localstack-pro:2.1.0
7+
networks:
8+
sweet_mahavira:
9+
ports:
10+
- "127.0.0.1:53:53"
11+
- "127.0.0.1:53:53/udp"
12+
- "127.0.0.1:4566:443"
13+
- "127.0.0.1:4510:4510"
14+
environment:
15+
- LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY}
16+
volumes:
17+
- "/var/run/docker.sock:/var/run/docker.sock"
18+
19+
# alternative 1:
20+
http_echo:
21+
container_name: http_echo
22+
image: ealen/echo-server
23+
networks:
24+
sweet_mahavira:
25+
ports:
26+
- 5678:80
27+
28+
# alternative 2:
29+
# http_echo:
30+
# container_name: http_echo
31+
# image: mendhak/http-https-echo
32+
# networks:
33+
# sweet_mahavira:
34+
# ports:
35+
# - "127.0.0.1:8080:8080"
36+
37+
networks:
38+
sweet_mahavira:

route53-dns-failover/run_demo.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bash
2+
3+
# This is a demo script that showcases Route53 DNS failover in LocalStack
4+
# Make sure the Docker Compose setup is running before executing this script
5+
6+
set -eux
7+
8+
HOSTED_ZONE_NAME=hello-localstack.com
9+
10+
# Create a hosted zone
11+
HOSTED_ZONE_ID=$(awslocal route53 create-hosted-zone --name $HOSTED_ZONE_NAME --caller-reference foo | jq -r .HostedZone.Id)
12+
13+
# Create a health check that runs against the `http_echo` container
14+
HEALTH_CHECK_ID=$(awslocal route53 create-health-check --caller-reference foobar --health-check-config '{
15+
"FullyQualifiedDomainName": "http_echo",
16+
"Port": 80,
17+
"ResourcePath": "/",
18+
"Type": "HTTP",
19+
"RequestInterval": 10
20+
}' | jq -r .HealthCheck.Id)
21+
22+
# Create RRSets
23+
awslocal route53 change-resource-record-sets --hosted-zone ${HOSTED_ZONE_ID#/hostedzone/} --change-batch '{
24+
"Changes": [
25+
{
26+
"Action": "CREATE",
27+
"ResourceRecordSet": {
28+
"Name": "target1.'$HOSTED_ZONE_NAME'",
29+
"Type": "CNAME",
30+
"TTL": 60,
31+
"ResourceRecords": [{"Value": "target1.example.com"}]
32+
}
33+
},
34+
{
35+
"Action": "CREATE",
36+
"ResourceRecordSet": {
37+
"Name": "target2.'$HOSTED_ZONE_NAME'",
38+
"Type": "CNAME",
39+
"TTL": 60,
40+
"ResourceRecords": [{"Value": "target2.example.com"}]
41+
}
42+
}
43+
]}'
44+
awslocal route53 change-resource-record-sets --hosted-zone-id ${HOSTED_ZONE_ID#/hostedzone/} --change-batch '{
45+
"Changes": [
46+
{
47+
"Action": "CREATE",
48+
"ResourceRecordSet": {
49+
"Name": "test.'$HOSTED_ZONE_NAME'",
50+
"Type": "CNAME",
51+
"SetIdentifier": "target1",
52+
"AliasTarget": {
53+
"HostedZoneId": "'${HOSTED_ZONE_ID#/hostedzone/}'",
54+
"DNSName": "target1.'$HOSTED_ZONE_NAME'",
55+
"EvaluateTargetHealth": true
56+
},
57+
"HealthCheckId": "'${HEALTH_CHECK_ID}'",
58+
"Failover": "PRIMARY"
59+
}
60+
},
61+
{
62+
"Action": "CREATE",
63+
"ResourceRecordSet": {
64+
"Name": "test.'$HOSTED_ZONE_NAME'",
65+
"Type": "CNAME",
66+
"SetIdentifier": "target2",
67+
"AliasTarget": {
68+
"HostedZoneId": "'${HOSTED_ZONE_ID#/hostedzone/}'",
69+
"DNSName": "target2.'$HOSTED_ZONE_NAME'",
70+
"EvaluateTargetHealth": true
71+
},
72+
"Failover": "SECONDARY"
73+
}
74+
}
75+
]}'
76+
77+
# Get the IP address of the LocalStack container on the Docker bridge
78+
LOCALSTACK_DNS_SERVER=$(docker inspect localstack_main | jq -r '.[0].NetworkSettings.Networks."route53-dns-failover_sweet_mahavira".IPAddress')
79+
LOCALSTACK_DNS_SERVER=localhost
80+
81+
# This IP address is used to query the LocalStack DNS server
82+
# This should return `target1.example.com` as the healthcheck is currently passing
83+
dig @$LOCALSTACK_DNS_SERVER +noall +answer test.hello-localstack.com CNAME
84+
85+
# Make the healthcheck fail by pointing it to a nonexistent host
86+
awslocal route53 update-health-check --health-check-id ${HEALTH_CHECK_ID} --fully-qualified-domain-name bad-host-p45e8eG94rK.com
87+
88+
# Wait for the healthcheck to refresh
89+
sleep 12
90+
91+
# This should return the failover `target2.example.com`
92+
dig @$LOCALSTACK_DNS_SERVER +noall +answer test.hello-localstack.com CNAME

0 commit comments

Comments
 (0)