Skip to main content

Generate terraform.auto.tfvars from Linode DNS

Quick guide to automatically generate your Terraform configuration from existing Linode DNS records.

What This Does​

The generate-tfvars.sh script:

  • Connects to your Linode account
  • Fetches all DNS records for your domain
  • Generates a ready-to-use terraform.auto.tfvars file

Result: You go from manual DNS records to Infrastructure as Code in 30 seconds! 🚀

Prerequisites​

Install jq (JSON processor):

# macOS
brew install jq

# Ubuntu/Debian
sudo apt-get install jq

# CentOS/RHEL
sudo yum install jq

Setup (One Time)​

Step 1: Get Linode API Token​

  1. Visit https://cloud.linode.com/profile/tokens
  2. Click "Create a Personal Access Token"
  3. Label: "Terraform DNS Generator"
  4. Permissions: Set Domains to Read Only
  5. Click "Create Token"
  6. Copy the token

Step 2: Download the Script​

Save the script from this repository as generate-tfvars.sh

Step 3: Configure​

Edit these lines in generate-tfvars.sh:

DOMAIN_NAME="itisajoke.net"                      # ← Your domain
SOA_EMAIL="vitkovic.tomislav@gmail.com" # ← Your email
DEFAULT_TTL=3600 # ← Default TTL (1 hour)

Step 4: Make Executable​

chmod +x generate-tfvars.sh

Usage​

Generate the tfvars file:​

LINODE_TOKEN=your_token_here ./generate-tfvars.sh

Or use an environment variable:

export LINODE_TOKEN=your_token_here
./generate-tfvars.sh

Expected Output:​

🚀 Linode DNS to Terraform tfvars Generator

🔍 Finding domain ID for itisajoke.net...
✅ Found domain ID: 3266789
🔍 Fetching DNS records from Linode...
✅ Found 35 DNS records
📝 Generating terraform.auto.tfvars...
✓ Generated 15 A records
✓ Generated 11 CNAME records
✓ Generated 5 MX records
✓ Generated 2 TXT records

✅ Successfully generated terraform.auto.tfvars

📋 Summary:
Domain: itisajoke.net
Domain ID: 3266789
Total Records: 35

🎯 Next steps:
1. Review the generated terraform.auto.tfvars
2. Adjust domain_name, soa_email, or default_ttl if needed
3. Run: terraform init
4. Run: terraform plan
5. Run: terraform apply

What Gets Generated​

The script creates terraform.auto.tfvars with this structure:

# terraform.auto.tfvars
# Auto-generated from Linode DNS records on 2025-10-29

domain_name = "itisajoke.net"
soa_email = "vitkovic.tomislav@gmail.com"
default_ttl = 3600

# A Records
a_records = [
{
name = ""
target = "198.185.159.144"
},
{
name = "gallery"
target = "173.230.140.102"
},
# ... all your A records
]

# CNAME Records
cname_records = [
{
name = "www"
target = "ghs.googlehosted.com"
},
# ... all your CNAME records
]

# MX Records
mx_records = [
{
name = ""
target = "aspmx.l.google.com"
priority = 1
},
# ... all your MX records
]

# TXT Records
txt_records = [
{
name = ""
target = "v=spf1 include:_spf.google.com ~all"
},
# ... all your TXT records
]

Next Steps​

After generating the file:

1. Review the File​

cat terraform.auto.tfvars

Check that all records are present and correct.

2. Initialize Terraform​

terraform init

3. Preview Changes​

terraform plan

4. Apply Configuration​

terraform apply

Type yes to confirm.

Common Scenarios​

Scenario 1: First Time Setup​

# Generate configuration
LINODE_TOKEN=your_token ./generate-tfvars.sh

# Review
cat terraform.auto.tfvars

# Apply
terraform init
terraform plan
terraform apply

Scenario 2: Regenerate After Manual Changes​

If you made changes in Linode dashboard and want to sync:

# Script automatically backs up old file
LINODE_TOKEN=your_token ./generate-tfvars.sh

# Review differences
diff terraform.auto.tfvars.backup.* terraform.auto.tfvars

# Apply changes
terraform plan
terraform apply

Scenario 3: Different Domain​

# Temporarily override the domain name
DOMAIN_NAME="example.com" LINODE_TOKEN=your_token ./generate-tfvars.sh

Features​

✅ Automatic Backup - Existing files are backed up before overwriting
✅ Sorted Records - Records sorted alphabetically for easy reading
✅ Smart TTLs - Only includes TTL when it differs from default
✅ Pretty Format - Properly indented HCL syntax
✅ Safety First - Token passed via environment variable

Troubleshooting​

Error: "jq: command not found"​

Install jq:

brew install jq  # macOS

Error: "Domain not found"​

The script shows available domains. Update DOMAIN_NAME in the script:

DOMAIN_NAME="your-actual-domain.com"

Error: "LINODE_TOKEN environment variable not set"​

Make sure to set the token:

LINODE_TOKEN=your_token ./generate-tfvars.sh

Want to Change Email or TTL​

Edit the script configuration:

SOA_EMAIL="your-email@example.com"
DEFAULT_TTL=7200 # 2 hours

Then run the script again.

Security Notes​

🔒 Never commit your API token to Git

✅ Good practices:

  • Use environment variables
  • Use read-only token
  • Delete token after use

❌ Bad practices:

  • Don't hardcode token in script
  • Don't commit token to Git

Full Workflow Example​

Complete workflow from Linode DNS to Terraform Cloud:

# 1. Generate configuration from Linode
LINODE_TOKEN=your_token ./generate-tfvars.sh

# 2. Review the generated file
cat terraform.auto.tfvars

# 3. Initialize Terraform
terraform init

# 4. Check what will be created
terraform plan

# 5. Apply configuration
terraform apply

# 6. Push to GitHub (triggers Terraform Cloud)
git add terraform.auto.tfvars
git commit -m "Initial DNS configuration from Linode"
git push origin main

# 7. Done! Future changes go through PRs

docker logs -f webhook-receiver docker logs -f docs-webhook