Automating DNS with Pulumi + Cloudflare: YAML-Driven Simplicity
Declarative DNS Management for the Cloud-Native Age
Managing DNS records manually is error-prone, tedious, and just not scalable. Thatβs why we built Cloudflare DNS Record Management with Pulumi β a lightweight infrastructure-as-code (IaC) solution that allows you to declaratively manage your DNS records using YAML, powered by Pulumi and Cloudflare.
π§ What Is This Project?
This project automates the creation and maintenance of DNS A and CNAME records in Cloudflare, all defined through clean YAML configuration files. It uses Pulumi (Python) to orchestrate infrastructure and integrates seamlessly with GitLab CI/CD.
No more logging into Cloudflareβs dashboard for routine DNS tweaks.
β¨ Key Features
- π Declarative Management: Define DNS records in simple YAML files (
arecord.yaml,cname.yaml) - π Idempotent Logic: Already-existing records? Skipped cleanly.
- π§ Intelligent Checks: Avoid duplicates and configuration errors
- βοΈ CI/CD Friendly: Out-of-the-box GitLab pipeline integration
- π Cloudflare Secure: Built on official Cloudflare Pulumi provider
- π’ Transparent Output: Record creation stats reported at the end
π Project Layout
cloudflare-dns-record/
βββ Pulumi.yaml # Pulumi project metadata
βββ __main__.py # Python code for resource creation
βββ requirements.txt # Dependencies
βββ resources/
β βββ arecord.yaml # A record entries
β βββ cname.yaml # CNAME entries
βββ .gitlab-ci.yml # CI/CD pipeline config
βοΈ Getting Started
1. Install Prerequisites
- Python 3.7+
- Pulumi CLI
- Cloudflare account (API token)
- GitLab account (optional for pipeline)
2. Setup Your Environment
Install dependencies:
pip install -r requirements.txt
Add your DNS entries:
# resources/arecord.yaml
- name: "example.com"
content: "192.0.2.1"
type: "A"
proxied: true
# resources/cname.yaml
- name: "www.example.com"
content: "example.com"
type: "CNAME"
proxied: true
π How to Deploy
Option A: Manual (Local)
pulumi up --stack dev
Option B: GitLab CI/CD
The .gitlab-ci.yml automates:
- Validation & linting
- YAML file presence checks
pulumi preview- Deployment to Cloudflare
π‘οΈ Error Handling You Can Trust
| Issue | Handled? | Notes |
|---|---|---|
| Record already exists | β | Skips creation |
| YAML file missing | β | Logs a warning |
| API timeout | β | Retries automatically |
| Invalid config | β | Clear, readable errors |
π Sample Output
Outputs:
arecord_records_created: 2
cname_records_created : 1
Simple. Transparent. Trackable.
β Best Practices
- Commit YAML configs for traceability
- Preview before applying with
pulumi preview - Export DNS as backup before rollout
- Use minimal API scopes for Cloudflare tokens
π§© Troubleshooting
| Problem | Solution |
|---|---|
Record already exists |
Expected β it's idempotent |
Missing YAML file |
Add it under resources/ or remove reference |
API timeout |
Handled automatically, but check internet/API limits |
Invalid YAML |
Validate YAML format and required fields |
π License
Open-sourced under MIT License. Modify freely and deploy confidently.
π Bonus
If you're looking to go further:
- Add MX or TXT record support
- Hook it into a DNS zone backup pipeline
- Integrate it with Slack for deploy summaries
Ready to version-control your DNS?
π Clone the project repo and try pulumi up.