How do I automate creating Linodes along with DNS records of the same name?

Linode Staff

I'm trying to automatically create DNS records based on a Linode's name. Is it possible to do this with the Linode DNS Manager, or should I create an application that creates the Linodes and their associated DNS records ?

For example - i have 3 Linodes

  • linode1
  • linode2
  • linode3

My domain is example.com
I want to automate the creation of the following DNS records when the Linodes are created:

  • linode1.example.com
  • linode2.example.com
  • linode3.example.com

1 Reply

While you can't automate this in the DNS Manager, you can do it using the Linode API, or even the Linode CLI. Below you can find a sample bash script that does exactly what you're looking for, using the Linode APIv4:

#!/usr/bin/env bash

## Name the Linode/subdomain to be created
TOKEN="YOUR_LINODE_API_TOKEN_GOES_HERE"
LINODE_NAMES=(
    "linode1"
    "linode2"
    "linode3"
)
DOMAIN_NAME="example.com"

## Get the domain's ID from the Linode APIv4
DOMAIN_ID="$(curl -H "Authorization: Bearer $TOKEN" \
        "https://api.linode.com/v4/domains" | jq -S | \
        grep -A 11 "$DOMAIN_NAME" | grep '"id"' | \
        awk '{print $2}' | sed 's/,//')"

## Create the Linodes
for i in "${LINODE_NAMES[@]}"; do
    curl -H "Content-Type: application/json" \
        -H "Authorization: Bearer $TOKEN" \
        -X POST -d '{
        "backups_enabled": true,
        "swap_size": 512,
        "image": "linode/debian9",
        "root_pass": "aComplexP@ssword",
        "booted": true,
        "label": "'"${i}"'",
        "type": "g6-standard-2",
        "region": "us-east",
        "group": "Linode-Group"
        }' \
        https://api.linode.com/v4/linode/instances
done

## Create the subdomain - this assumes your base domain already exists
## in the Linode DNS Manager
for i in "${LINODE_NAMES[@]}"; do
    curl -H "Content-Type: application/json" \
        -H "Authorization: Bearer $TOKEN" \
        -X POST -d '{
        "type": "A",
        "name": "'"${i}"'",
        "priority": 50,
        "weight": 50,
        "port": 80,
        "service": null,
        "protocol": null,
        "ttl_sec": 604800
        }' \
        "https://api.linode.com/v4/domains/${DOMAIN_ID}/records"
done

Note that some of the options for creating a Linode are optional. You can consult the Linode APIv4 Documentation for more information about what options are available, and which ones you do/don't need for your setup. If you have the Linode CLI installed, you can write a similar script that uses that instead. You can also check out our APIv4 Pyhton Bindings if you wish to use Python instead.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct