1. Working with CloudFlare DNS in python

Working with CloudFlare DNS in python

Last week I wrote about aDNS discovery feature in Etcd. As a step in the whole process we need to create DNS records in twindb.com zone. CloudFlare provides rich API to work with it. We wrapped it into a Python module twindb_cloudflare and opensourced it:

In the post I will show how to use twindb_cloudflare module.

CloudFlare API credentials

First of all you need to get credentials to work with CloudFlare. Visit https://www.cloudflare.com/a/account/my-account and get “API Key”.

Installing twindb_cloudflare

The module is available in PyPi. You can install it with pip:

Creating A record

To create an A record

The script runs a minute or two:

Updating A record

Updating a record is pretty straightforward either. In real applications you have to take into account that a DNS response may be cached and CloudFlare changes aren’t available momentarily.

The change is visible after a minute

Deleting A record

And finally let’s delete the record we’ve created. Here I won’t wait until the change is propagated and will assume that if there was no exception then we are good.

Final notes

ClaudFlare API provides many more actions than the module implements. However I wanted to start with something small and incrementally implementing more feature as they are needed.

I encourage you to file bugs and feature requests on https://github.com/twindb/twindb_cloudflare/issues.
Also, pull requests are welcomed 🙂

Previous Post Next Post