This post aims to help you manage your Netlify resources declarativley, using terraform you will be alble to declare and/or import Netlify dns records, this post is devided in three parts first, we will go through setting up the Netlify provider, then we see how to create records , and finally we see how to import existing records to terraform.
Setup
I assume you have terraform v1.5 or above installed, and you know basic terraform terminology and commands.
First thing we do is specify the the provider name and version in the terraform.tf
file.
Provider initialization
It’s best practice, to fix the version of providers, so that you have control over what changes can affect your terraform code base.
Make sure you are in the right folder (where your .tf
files exist), run the command to initialize the project.
After we initialized the project, now we need to tell terraform where how to connect with Netlify APIs, we need to setup credentials.
Credentials setup
Let’s create first a variable that represent the api token make sure to flag it as sensitive so it doesn’t appear in CD outputs, or specular plans. under variables.tf
.
Go to your netlify account in the upper right corner, click on your user avatar then:
- Go to User settings
- Under Applications click on OAuth
- Click on New access token
- Give a description like (terraform api key) & set an exiration date (after 30days)
- Copy the api token
Set the token you copied, in your shell profile as an environment variable, Note that the naming of this variable should match the name of the terraform variable TF_VAR_<name_of_terraform_variable>
.
Source the profile file into the terminal you are working in, and test if the variable is populated using echo
.
After you successfully generated the api key, now it’s time to configure the provider to use it, under the providers.tf
add this config:
Verify your permissions, using the plan command if there is any permissions issue it will appear in this phase.
Manage netlify resources
Project structure
Create DNS records
Creating dns records requires fist having the dns_zone id, simply write the
Import existing dns records
To import dns records to Netlify we need the record id, from what I saw there is no UI to show the id of each record that’s why I use this way to get them.
Getting the zone id
We already retieved the zone id using data "netlify_dns_zone"
we need to search for it in the terraform state, execute this command and identify the id.
Getting the record id
Now we can have all records with their respective ids using Netlify API using this command will retieve all records for a givin zoneid, replace <zone-id>
with what you found on terraform state.
After you found the record id, you can import the record using this block, create a file called import.tf
and add this block, replace the id
with the zone-id:record-id
, do this to all records you want to import.
Once finished, you can apply/plan your changes, you will see that terrsform is telling about importing these resources.