You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
13
+
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`.
14
14
15
-
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.
15
+
Before you begin, ensure you have:
16
+
*[Installed Terraform](/terraform/installing/)
17
+
*[Created an API Token](/fundamentals/api/get-started/create-token/) with permissions to edit resources for this tutorial
16
18
17
-
<Renderfile="v4-code-snippets" />
19
+
<Renderfile="v5-code-snippets" />
18
20
19
-
## 1. Define your first Terraform config file
21
+
## 1. Create your configuration
20
22
21
-
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/).
22
-
23
-
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.
24
-
25
-
:::caution
26
-
27
-
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.
28
-
:::
23
+
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/):
29
24
30
25
```bash
31
-
cat > cloudflare.tf <<'EOF'
32
26
terraform {
33
27
required_providers {
34
28
cloudflare = {
35
-
source = "cloudflare/cloudflare"
36
-
version = "~> 4"
29
+
source= "cloudflare/cloudflare"
30
+
version = "~> 5"
37
31
}
38
32
}
39
33
}
@@ -54,189 +48,135 @@ variable "domain" {
54
48
default = "<YOUR_DOMAIN>"
55
49
}
56
50
57
-
resource "cloudflare_record" "www" {
58
-
zone_id = var.zone_id
51
+
resource "cloudflare_dns_record""www" {
52
+
zone_id = "<YOUR_ZONE_ID>"
59
53
name = "www"
60
-
value = "203.0.113.10"
54
+
content = "203.0.113.10"
61
55
type = "A"
56
+
ttl = 1
62
57
proxied = true
58
+
comment = "Domain verification record"
63
59
}
64
-
EOF
65
60
```
61
+
:::caution
66
62
67
-
## 2. Initialize Terraform and the Cloudflare provider
68
-
69
-
After creating your basic configuration in HCL, initialize Terraform and ask it to apply the configuration to Cloudflare.
70
-
71
-
```sh
72
-
terraform init
73
-
```
74
-
75
-
```sh output
76
-
77
-
Initializing provider plugins...
78
-
- Checking for available provider plugins on https://releases.hashicorp.com...
79
-
- Downloading plugin for provider "cloudflare" (1.0.0)...
80
-
81
-
The following providers do not have any version constraints in configuration,
82
-
so the latest version was installed.
83
-
84
-
To prevent automatic upgrades to new major versions that may contain breaking
85
-
changes, it is recommended to add version = "..." constraints to the
86
-
corresponding provider blocks in configuration, with the constraint strings
87
-
suggested below.
88
-
89
-
* provider.cloudflare: version = "~> 1.0"
90
-
91
-
Terraform has been successfully initialized!
92
-
93
-
You may now begin working with Terraform. Try running "terraform plan" to see
94
-
any changes that are required for your infrastructure. All Terraform commands
95
-
should now work.
63
+
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.
64
+
:::
96
65
97
-
If you ever set or change modules or backend configuration for Terraform,
98
-
rerun this command to reinitialize your working directory. If you forget, other
99
-
commands will detect it and remind you to do so if necessary.
100
-
```
66
+
## 2. Initialize and plan
101
67
102
-
When you run `terraform init`, any plugins required, such as the Cloudflare Terraform provider, are automatically downloaded and saved locally to a `.terraform` directory.
68
+
Initialize Terraform to download the Cloudflare provider:
After installing the Cloudflare provider, review the proposed changes to your Cloudflare account so they match the configuration you previously defined.
74
+
Review what will be created:
119
75
120
76
```sh
121
77
terraform plan
122
78
```
123
-
124
79
```sh output
125
80
126
-
Terraform used the selected providers to generate the following execution plan.
127
-
Resource actions are indicated with the following symbols:
81
+
Terraform used the selected providers to generate the following execution plan. Resource actions are
82
+
indicated with the following symbols:
128
83
+ create
129
84
130
85
Terraform will perform the following actions:
131
86
132
-
# cloudflare_record.www will be created
133
-
+ resource "cloudflare_record""www" {
134
-
+ allow_overwrite = false
135
-
+ created_on = (known after apply)
136
-
+ hostname = (known after apply)
137
-
+ id = (known after apply)
138
-
+ metadata = (known after apply)
139
-
+ modified_on = (known after apply)
140
-
+ name = "www"
141
-
+ proxiable = (known after apply)
142
-
+ proxied = true
143
-
+ ttl = (known after apply)
144
-
+ type = "A"
145
-
+ value = "203.0.113.10"
146
-
+ zone_id = "e2e6491340be87a3726f91fc4148b126"
87
+
# cloudflare_dns_record.example_dns_record will be created
Note: You didn't use the -out option to save this plan, so Terraform can't
154
-
guarantee to take exactly these actions if you run "terraform apply" now.
155
108
```
156
109
157
-
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.
158
-
159
-
## 4. Apply your changes
160
-
161
-
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.
110
+
## 3. Apply and verify
162
111
163
-
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.
112
+
Apply your configuration:
164
113
165
114
```sh
166
-
terraform apply --auto-approve
115
+
terraform apply
167
116
```
168
117
169
-
```sh output
118
+
Type `yes` when prompted.
170
119
171
-
Terraform used the selected providers to generate the following execution plan.
172
-
Resource actions are indicated with the following symbols:
120
+
```sh output
121
+
Terraform used the selected providers to generate the following execution plan. Resource actions are
122
+
indicated with the following symbols:
173
123
+ create
174
124
175
125
Terraform will perform the following actions:
176
126
177
-
# cloudflare_record.www will be created
178
-
+ resource "cloudflare_record""www" {
179
-
+ allow_overwrite = false
180
-
+ created_on = (known after apply)
181
-
+ hostname = (known after apply)
182
-
+ id = (known after apply)
183
-
+ metadata = (known after apply)
184
-
+ modified_on = (known after apply)
185
-
+ name = "www"
186
-
+ proxiable = (known after apply)
187
-
+ proxied = true
188
-
+ ttl = (known after apply)
189
-
+ type = "A"
190
-
+ value = "203.0.113.10"
191
-
+ zone_id = "e2e6491340be87a3726f91fc4148b126"
127
+
# cloudflare_dns_record.example_dns_record will be created
128
+
+ resource "cloudflare_dns_record""www" {
129
+
+ comment = "Domain verification record"
130
+
+ comment_modified_on = (known after apply)
131
+
+ content = "203.0.113.10"
132
+
+ created_on = (known after apply)
133
+
+ id = (known after apply)
134
+
+ meta = (known after apply)
135
+
+ modified_on = (known after apply)
136
+
+ name = "www"
137
+
+ proxiable = (known after apply)
138
+
+ proxied = true
139
+
+ settings = (known after apply)
140
+
+ tags = (known after apply)
141
+
+ tags_modified_on = (known after apply)
142
+
+ ttl = 1
143
+
+ type = "A"
144
+
+ zone_id = "<YOUR_ZONE_ID>"
192
145
}
193
146
194
147
Plan: 1 to add, 0 to change, 0 to destroy.
195
-
cloudflare_record.www: Creation complete after 1s [id=c38d3103767284e7cd14d5dad3ab8668]
Terraform will perform the actions described above.
151
+
Only 'yes' will be accepted to approve.
199
152
200
-
## 5. Verify the results
153
+
Enter a value: yes
201
154
202
-
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.
cloudflare_dns_record.example_dns_record: Creation complete after 0s
203
157
204
-
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`.
0 commit comments