Environment Setup

💡
Note: You will need Terraform and Cf-Terraforming installed to follow these labs. Mac users are likely to find using brew to be the easiest way to do so.

Ensure you are working in a fresh directory. It is advisable to create a new directory for these labs.

The examples here are in bash; however you can also do this in Powershell if you are using Windows. The syntax for the Terraform configuration is generally same, but please note the command examples in this guide are for bash, so you will need to use a Powershell equivalent if necessary.

Task

In this section we will set up your lab environment ready for the next steps. This will involve:

  • Creating the basic Terraform configuration for your provider
  • Telling Terraform to use the Cloudflare Provider
  • Initialising the environment
  • Setting secrets

Before continuing, please create an API Token with DNS: Edit permission against the zone you are using for these labs. We will add more permissions to this token as we progress.

While there is nothing stopping you using your Global API key for these labs, this is considered bad practice. It is not something we would ever reccomend to customers, and therefore we encourage you to create an API token as a matter of best practice.

If you are unfamiliar with how to do this, see: https://developers.cloudflare.com/fundamentals/api/get-started/create-token/

Why

  • Any new Terraform environment needs to be initialised
  • By referencing the Cloudflare Provider in the configuration, Terraform knows what plugins to install
  • We need to use an API Token so that Terraform has permission (these tokens are bound to your user account)

Steps

1. Create Provider Config

From the directory you have created, paste the following into a file called provider.tf and save it:

terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 4.0"
    }
  }
}

provider "cloudflare" {
}

provider.tf

Note: Terraform looks at all files with the .tf extension when running commands

2. Initialise the configuration

Enter the command terraform init to initialise our working Terraform project:

terraform init

This will install the Cloudflare provider and perform other tasks that tell Terraform that this is a Terraform environment.

Look at the contents of the directory (you may need to include hidden files). What do you see? How many files? We'll revisit this later.

3. Configure Authentication

Assign the environment variable CLOUDFLARE_API_TOKEN with the value of the token you created at the start

export CLOUDFLARE_API_TOKEN=<your-token-goes-here>

Validate that the token is now available as an environment variable by using the env command or alternatively:

echo $CLOUDFLARE_API_TOKEN

Terraform knows to look for this token. For now, that's all we need to know - but later we will discuss other ways to do this.

Congratulations! You just created a Terraform project and added your API Token. Please move on to the next task.