This tutorial shows you how to get started with Terraform. The tutorial uses an example scenario where you have a web server for your domain, accessible on 203.0.113.10, and you just signed up your domain (example.com) on Cloudflare to manage everything in Terraform.
Terraform will process any files with a .tf extension. As the configuration becomes more complex, you will want to split the config into separate files and modules. For now, proceed with a single file.
2. Initialize Terraform and the Cloudflare provider
After creating your basic configuration in HCL, initialize Terraform and ask it to apply the configuration to Cloudflare.
When you run terraform init, any plugins required, such as the Cloudflare Terraform provider, are automatically downloaded and saved locally to a .terraform directory.
3. Review the execution plan
After installing the Cloudflare provider, review the proposed changes to your Cloudflare account so they match the configuration you previously defined.
As displayed in the execution plan, Terraform will create a new DNS record. The output shows the values that you explicitly specified, such as the value of the A record (203.0.113.10). Values shown as (known after apply) are derived based on other API calls (for example, looking up the metadata), or the values are returned after the object is created.
4. Apply your changes
The plan command is important because it allows you to preview the changes for accuracy before actually making them. After you review the execution plan, apply your changes.
You can use --auto-approve on the command line for a briefer output. Without this flag, Terraform will display the output of the Terraform plan and then ask for confirmation before applying it.
5. Verify the results
Log in to the Cloudflare dashboard ↗ and go to DNS > Records. The record created by Terraform appears in the records list.
To see the full results returned from the API call, including the default values that you did not specify but let Terraform compute, run terraform show.