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.

Medium Python 3.9+ Aug 9, 2026 Automation & scripting 13 views 0 copies

Python code

40 lines
Python 3.9+
import 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

stdout
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

  1. Use Flask or FastAPI for a more feature-rich mock with routing and validation
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.