- Generate a service account with DNS admin rights and download the json.
- Generate a service for unauthenticated calls
- 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
-
- 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
- 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
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`); | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "home-dns", | |
| "version": "0.0.1", | |
| "dependencies": { | |
| "@google-cloud/dns": "^1.2.1" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "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