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
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
-
:::
21
+
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
22
30
23
```bash
31
-
cat > cloudflare.tf <<'EOF'
32
24
terraform {
33
25
required_providers {
34
26
cloudflare = {
35
-
source = "cloudflare/cloudflare"
36
-
version = "~> 4"
27
+
source= "cloudflare/cloudflare"
28
+
version = "~> 5"
37
29
}
38
30
}
39
31
}
@@ -54,189 +46,135 @@ variable "domain" {
54
46
default = "<YOUR_DOMAIN>"
55
47
}
56
48
57
-
resource "cloudflare_record" "www" {
58
-
zone_id = var.zone_id
49
+
resource "cloudflare_dns_record""www" {
50
+
zone_id = "<YOUR_ZONE_ID>"
59
51
name = "www"
60
-
value = "203.0.113.10"
52
+
content = "203.0.113.10"
61
53
type = "A"
54
+
ttl = 1
62
55
proxied = true
56
+
comment = "Domain verification record"
63
57
}
64
-
EOF
65
-
```
66
-
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
58
```
59
+
:::caution
74
60
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.
61
+
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.
62
+
:::
96
63
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
-
```
64
+
## 2. Initialize and plan
101
65
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.
66
+
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.
72
+
Review what will be created:
119
73
120
74
```sh
121
75
terraform plan
122
76
```
123
-
124
77
```sh output
125
78
126
-
Terraform used the selected providers to generate the following execution plan.
127
-
Resource actions are indicated with the following symbols:
79
+
Terraform used the selected providers to generate the following execution plan. Resource actions are
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
106
```
156
107
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
108
+
## 3. Apply and verify
160
109
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.
162
-
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.
110
+
Apply your configuration:
164
111
165
112
```sh
166
-
terraform apply --auto-approve
113
+
terraform apply
167
114
```
168
115
169
-
```sh output
116
+
Type `yes` when prompted.
170
117
171
-
Terraform used the selected providers to generate the following execution plan.
172
-
Resource actions are indicated with the following symbols:
118
+
```sh output
119
+
Terraform used the selected providers to generate the following execution plan. Resource actions are
120
+
indicated with the following symbols:
173
121
+ create
174
122
175
123
Terraform will perform the following actions:
176
124
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"
125
+
# cloudflare_dns_record.www will be created
126
+
+ resource "cloudflare_dns_record""www" {
127
+
+ comment = "Domain verification record"
128
+
+ comment_modified_on = (known after apply)
129
+
+ content = "203.0.113.10"
130
+
+ created_on = (known after apply)
131
+
+ id = (known after apply)
132
+
+ meta = (known after apply)
133
+
+ modified_on = (known after apply)
134
+
+ name = "www"
135
+
+ proxiable = (known after apply)
136
+
+ proxied = true
137
+
+ settings = (known after apply)
138
+
+ tags = (known after apply)
139
+
+ tags_modified_on = (known after apply)
140
+
+ ttl = 1
141
+
+ type = "A"
142
+
+ zone_id = "<YOUR_ZONE_ID>"
192
143
}
193
144
194
145
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.
149
+
Only 'yes' will be accepted to approve.
150
+
151
+
Enter a value: yes
199
152
200
-
## 5. Verify the results
153
+
cloudflare_dns_record.www: Creating...
154
+
cloudflare_dns_record.www: Creation complete after 0s
201
155
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.
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`.
159
+
After creation, verify the DNS record:
205
160
206
161
```sh
207
-
terraform show
162
+
dig www.example.com
208
163
```
209
164
210
-
```sh output
211
-
# cloudflare_record.www:
212
-
resource "cloudflare_record""www" {
213
-
id = "c38d3103767284e7cd14d5dad3ab8668"
214
-
created_on = "2023-04-08T00:37:33.76321Z"
215
-
data = []
216
-
domain = "example.com"
217
-
hostname = "www.example.com"
218
-
metadata = [
219
-
{
220
-
auto_added = false
221
-
managed_by_apps = false
222
-
}
223
-
]
224
-
modified_on = "2023-04-08T00:37:33.76321Z"
225
-
name = "www"
226
-
priority = 0
227
-
proxiable = true
228
-
proxied = true
229
-
ttl = 1
230
-
type = "A"
231
-
value = "203.0.113.10"
232
-
zone_id = "e2e6491340be87a3726f91fc4148b126"
233
-
}
234
-
```
165
+
Test the web server response:
235
166
236
167
```sh
237
168
curl https://www.example.com
238
169
```
239
-
240
170
```sh output
241
171
Hello, this is 203.0.113.10!
242
172
```
173
+
174
+
To see the full results returned from the API call:
175
+
176
+
```sh
177
+
terraform show
178
+
```
179
+
180
+
You can also check the [Cloudflare dashboard](https://dash.cloudflare.com) and go to **DNS** > **Records**.
0 commit comments