How to Build a Mock Route53 DNS API in Python
Create a mock DNS API server in Python that simulates Route53 record lookups and updates using the standard library.
Python code
40 linesimport json
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
class DNSUpdateHandler(BaseHTTPRequestHandler):
records = {"example.com": "1.2.3.4"}
def do_GET(self):
domain = parse_qs(urlparse(self.path).query).get("domain", [""])[0]
if domain in self.records:
self._send(200, {"domain": domain, "ip": self.records[domain]})
else:
self._send(404, {"error": "Record not found"})
def do_PUT(self):
content_length = int(self.headers.get("Content-Length", 0))
body = json.loads(self.rfile.read(content_length) or b"{}")
domain = body.get("domain")
ip = body.get("ip")
if not domain or not ip:
self._send(400, {"error": "domain and ip required"})
return
self.records[domain] = ip
self._send(200, {"domain": domain, "ip": ip, "status": "updated"})
def _send(self, status, payload):
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(payload).encode())
def log_message(self, format, *args):
pass
if __name__ == "__main__":
server = HTTPServer(("localhost", 8080), DNSUpdateHandler)
print("Mock Route53 DNS API running on port 8080")
server.serve_forever()
Output
Running server:
Mock Route53 DNS API running on port 8080
GET /?domain=example.com
{"domain": "example.com", "ip": "1.2.3.4"}
PUT with body {"domain": "example.com", "ip": "5.6.7.8"}
{"domain": "example.com", "ip": "5.6.7.8", "status": "updated"}
How it works
The mock server uses http.server from the standard library to handle HTTP requests without external dependencies. The do_GET method parses query parameters with urllib.parse and looks up the domain in an in-memory dictionary. The do_PUT method reads the JSON request body and updates the record. The _send helper standardizes JSON responses with proper HTTP status codes. In-memory storage makes it ideal for testing DNS automation workflows locally.
Common mistakes
- Forgetting to parse query strings before accessing GET parameters
- Not reading `Content-Length` before parsing the PUT body
- Returning 200 instead of 404 for missing records
- Hardcoding server host as localhost when the mock needs network access
Variations
- Use Flask or FastAPI for a more feature-rich mock with routing and validation
- Add DELETE support to simulate record removal in Route53
Real-world use cases
- Testing DNS automation scripts that update real Route53 records without touching production infrastructure.
- Simulating DNS provider APIs in CI/CD pipelines for integration tests of infrastructure-as-code tools.
- Building a local development sandbox to validate DNS record change workflows before deployment.
Sponsored
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
Keep learning
Related tutorials and quizzes for this topic.