Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 85 additions & 147 deletions src/content/docs/terraform/tutorial/initialize-terraform.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,22 @@ head:

import { Render } from "~/components";

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.
This tutorial shows you how to get started with Terraform. You just signed up your domain (`example.com`) on Cloudflare to manage everything in Terraform and now you will create a DNS record pointing `www.example.com` to a web server at `203.0.113.10`.

Before you begin, ensure you have [installed Terraform](/terraform/installing/). You will also need to [create an API Token](/fundamentals/api/get-started/create-token/) with permissions to edit resources for this tutorial.
Before you begin, ensure you have:
* [Installed Terraform](/terraform/installing/)
* [Created an API Token](/fundamentals/api/get-started/create-token/) with permissions to edit resources for this tutorial

<Render file="v4-code-snippets" />
## 1. Create your configuration

## 1. Define your first Terraform config file

Create an initial Terraform config file, filling in your own values for the [API token](/fundamentals/api/get-started/create-token/), [zone ID](/fundamentals/account/find-account-and-zone-ids/), [account ID](/fundamentals/account/find-account-and-zone-ids/), and [domain](/fundamentals/manage-domains/add-site/).

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.

:::caution

To prevent accidentally exposing your Cloudflare credentials, do not save this file in your version control system. The [next tutorial](/terraform/tutorial/track-history/) will cover best practices for passing in your API token.
:::
Create a file named `main.tf`, filling in your own values for the [API token](/fundamentals/api/get-started/create-token/), [zone ID](/fundamentals/account/find-account-and-zone-ids/), [account ID](/fundamentals/account/find-account-and-zone-ids/), and [domain](/fundamentals/manage-domains/add-site/):

```bash
cat > cloudflare.tf <<'EOF'
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4"
source = "cloudflare/cloudflare"
version = "~> 5"
}
}
}
Expand All @@ -54,189 +46,135 @@ variable "domain" {
default = "<YOUR_DOMAIN>"
}

resource "cloudflare_record" "www" {
zone_id = var.zone_id
resource "cloudflare_dns_record" "www" {
zone_id = "<YOUR_ZONE_ID>"
name = "www"
value = "203.0.113.10"
content = "203.0.113.10"
type = "A"
ttl = 1
proxied = true
comment = "Domain verification record"
}
EOF
```

## 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.

```sh
terraform init
```
:::caution

```sh output

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "cloudflare" (1.0.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.cloudflare: version = "~> 1.0"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
To prevent accidentally exposing your Cloudflare credentials, do not save this file in your version control system. The [next tutorial](/terraform/tutorial/track-history/) will cover best practices for passing in your API token.
:::

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
```
## 2. Initialize and plan

When you run `terraform init`, any plugins required, such as the Cloudflare Terraform provider, are automatically downloaded and saved locally to a `.terraform` directory.
Initialize Terraform to download the Cloudflare provider:

```sh
find .terraform/
```

```sh output
.terraform/
.terraform/plugins
.terraform/plugins/darwin_amd64
.terraform/plugins/darwin_amd64/lock.json
.terraform/plugins/darwin_amd64/terraform-provider-cloudflare_v1.0.0_x4
terraform init
```

## 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.
Review what will be created:

```sh
terraform plan
```

```sh output

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create

Terraform will perform the following actions:

# cloudflare_record.www will be created
+ resource "cloudflare_record" "www" {
+ allow_overwrite = false
+ created_on = (known after apply)
+ hostname = (known after apply)
+ id = (known after apply)
+ metadata = (known after apply)
+ modified_on = (known after apply)
+ name = "www"
+ proxiable = (known after apply)
+ proxied = true
+ ttl = (known after apply)
+ type = "A"
+ value = "203.0.113.10"
+ zone_id = "e2e6491340be87a3726f91fc4148b126"
# cloudflare_dns_record.www will be created
+ resource "cloudflare_dns_record" "www" {
+ comment = "Domain verification record"
+ comment_modified_on = (known after apply)
+ content = "203.0.113.10"
+ created_on = (known after apply)
+ id = (known after apply)
+ meta = (known after apply)
+ modified_on = (known after apply)
+ name = "www"
+ proxiable = (known after apply)
+ proxied = true
+ settings = (known after apply)
+ tags = (known after apply)
+ tags_modified_on = (known after apply)
+ ttl = 1
+ type = "A"
+ zone_id = "<YOUR_ZONE_ID>"
}

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
```

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
## 3. Apply and verify

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.
Apply your configuration:

```sh
terraform apply --auto-approve
terraform apply
```

```sh output
Type `yes` when prompted.

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
```sh output
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create

Terraform will perform the following actions:

# cloudflare_record.www will be created
+ resource "cloudflare_record" "www" {
+ allow_overwrite = false
+ created_on = (known after apply)
+ hostname = (known after apply)
+ id = (known after apply)
+ metadata = (known after apply)
+ modified_on = (known after apply)
+ name = "www"
+ proxiable = (known after apply)
+ proxied = true
+ ttl = (known after apply)
+ type = "A"
+ value = "203.0.113.10"
+ zone_id = "e2e6491340be87a3726f91fc4148b126"
# cloudflare_dns_record.www will be created
+ resource "cloudflare_dns_record" "www" {
+ comment = "Domain verification record"
+ comment_modified_on = (known after apply)
+ content = "203.0.113.10"
+ created_on = (known after apply)
+ id = (known after apply)
+ meta = (known after apply)
+ modified_on = (known after apply)
+ name = "www"
+ proxiable = (known after apply)
+ proxied = true
+ settings = (known after apply)
+ tags = (known after apply)
+ tags_modified_on = (known after apply)
+ ttl = 1
+ type = "A"
+ zone_id = "<YOUR_ZONE_ID>"
}

Plan: 1 to add, 0 to change, 0 to destroy.
cloudflare_record.www: Creation complete after 1s [id=c38d3103767284e7cd14d5dad3ab8668]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
```
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

## 5. Verify the results
cloudflare_dns_record.www: Creating...
cloudflare_dns_record.www: Creation complete after 0s

Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and go to **DNS** > **Records**. The record created by Terraform appears in the records list.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
```

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`.
After creation, verify the DNS record:

```sh
terraform show
dig www.example.com
```

```sh output
# cloudflare_record.www:
resource "cloudflare_record" "www" {
id = "c38d3103767284e7cd14d5dad3ab8668"
created_on = "2023-04-08T00:37:33.76321Z"
data = []
domain = "example.com"
hostname = "www.example.com"
metadata = [
{
auto_added = false
managed_by_apps = false
}
]
modified_on = "2023-04-08T00:37:33.76321Z"
name = "www"
priority = 0
proxiable = true
proxied = true
ttl = 1
type = "A"
value = "203.0.113.10"
zone_id = "e2e6491340be87a3726f91fc4148b126"
}
```
Test the web server response:

```sh
curl https://www.example.com
```

```sh output
Hello, this is 203.0.113.10!
```

To see the full results returned from the API call:

```sh
terraform show
```

You can also check the [Cloudflare dashboard](https://dash.cloudflare.com) and go to **DNS** > **Records**.
Loading