Skip to content

Instantly share code, notes, and snippets.

@teadur
Forked from btamas/README.md
Created July 14, 2025 15:27
Show Gist options
  • Select an option

  • Save teadur/7e58e1b6cf8af747b61ed70115d7487a to your computer and use it in GitHub Desktop.

Select an option

Save teadur/7e58e1b6cf8af747b61ed70115d7487a to your computer and use it in GitHub Desktop.
Mikrotik Dynamic DNS (DDNS) update with Google Cloud (GCP) function on Google Cloud DNS
  1. Generate a service account with DNS admin rights and download the json.
  2. Generate a service for unauthenticated calls
  3. Create a cloud function and upload the index.js, package.json and service acount json
    • 128MB RAM is enough
    • HTTP trigger should be set
    • Unauthenticated call should be set
      1. service account should be selected
    • Add the following environment variables:
      • PROJECT_NAME: name of the project, where the DNS is defined
      • RECORD_NAME: name of the DNS record, what should be updated. A . is required in the end, like: home.example.com.
      • SECRET: a secret string that should be passed during api call in query parameter
      • GOOGLE_APPLICATION_CREDENTIALS: name of your 1. service account json file, like ./project-123.json
  4. Set a sceduler in mikrotik router with the following command: /tool fetch url="https://europe-west3-projectid.cloudfunctions.net/functionname\?secret=hereisyoursecret" keep-result=no
const { DNS } = require('@google-cloud/dns');
const dns = new DNS();
const { PROJECT_NAME, RECORD_NAME, SECRET } = process.env;
exports.app = async (req, res) => {
const ip = req.headers['x-forwarded-for'];
if ([PROJECT_NAME, RECORD_NAME, SECRET].some(environmentVariable => typeof environmentVariable === 'undefined')) {
return res.end('Incorrectly configured environment');
}
if (req.query.secret !== SECRET) {
return res.end('Incorrect secret');
}
const zone = dns.zone(PROJECT_NAME);
const [record] = await zone.getRecords({ name: RECORD_NAME, type: 'A' });
const change = {};
if (record) {
change.delete = record;
}
change.add = zone.record('A', {
name: RECORD_NAME,
data: ip,
ttl: 300
});
await zone.createChange(change);
return res.end(`Your ip is ${JSON.stringify(ip)} and it's been set`);
};
{
"name": "home-dns",
"version": "0.0.1",
"dependencies": {
"@google-cloud/dns": "^1.2.1"
}
}
{
"type": "service_account",
"project_id": "project",
"private_key_id": "abc",
"private_key": "-----BEGIN PRIVATE KEY-----\ndef\n-----END PRIVATE KEY-----\n",
"client_email": "xxx",
"client_id": "123",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment