Skip to content

Commit ffbca5f

Browse files
authored
RM-22227: Update initialize-terraform.mdx
1 parent 7c5a750 commit ffbca5f

File tree

1 file changed

+86
-146
lines changed

1 file changed

+86
-146
lines changed

src/content/docs/terraform/tutorial/initialize-terraform.mdx

Lines changed: 86 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,24 @@ head:
1010

1111
import { Render } from "~/components";
1212

13-
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`.
1414

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
1618

17-
<Render file="v4-code-snippets" />
19+
<Render file="v5-code-snippets" />
1820

19-
## 1. Define your first Terraform config file
21+
## 1. Create your configuration
2022

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/):
2924

3025
```bash
31-
cat > cloudflare.tf <<'EOF'
3226
terraform {
3327
required_providers {
3428
cloudflare = {
35-
source = "cloudflare/cloudflare"
36-
version = "~> 4"
29+
source = "cloudflare/cloudflare"
30+
version = "~> 5"
3731
}
3832
}
3933
}
@@ -54,189 +48,135 @@ variable "domain" {
5448
default = "<YOUR_DOMAIN>"
5549
}
5650

57-
resource "cloudflare_record" "www" {
58-
zone_id = var.zone_id
51+
resource "cloudflare_dns_record" "www" {
52+
zone_id = "<YOUR_ZONE_ID>"
5953
name = "www"
60-
value = "203.0.113.10"
54+
content = "203.0.113.10"
6155
type = "A"
56+
ttl = 1
6257
proxied = true
58+
comment = "Domain verification record"
6359
}
64-
EOF
6560
```
61+
:::caution
6662

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+
:::
9665

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
10167

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:
10369

10470
```sh
105-
find .terraform/
106-
```
107-
108-
```sh output
109-
.terraform/
110-
.terraform/plugins
111-
.terraform/plugins/darwin_amd64
112-
.terraform/plugins/darwin_amd64/lock.json
113-
.terraform/plugins/darwin_amd64/terraform-provider-cloudflare_v1.0.0_x4
71+
terraform init
11472
```
11573

116-
## 3. Review the execution plan
117-
118-
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:
11975

12076
```sh
12177
terraform plan
12278
```
123-
12479
```sh output
12580

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:
12883
+ create
12984

13085
Terraform will perform the following actions:
13186

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
88+
+ resource "cloudflare_dns_record" "www" {
89+
+ comment = "Domain verification record"
90+
+ comment_modified_on = (known after apply)
91+
+ content = "203.0.113.10"
92+
+ created_on = (known after apply)
93+
+ id = (known after apply)
94+
+ meta = (known after apply)
95+
+ modified_on = (known after apply)
96+
+ name = "www"
97+
+ proxiable = (known after apply)
98+
+ proxied = true
99+
+ settings = (known after apply)
100+
+ tags = (known after apply)
101+
+ tags_modified_on = (known after apply)
102+
+ ttl = 1
103+
+ type = "A"
104+
+ zone_id = "<YOUR_ZONE_ID>"
147105
}
148106

149107
Plan: 1 to add, 0 to change, 0 to destroy.
150-
151-
------------------------------------------------------------------------
152-
153-
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.
155108
```
156109

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
162111

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:
164113

165114
```sh
166-
terraform apply --auto-approve
115+
terraform apply
167116
```
168117

169-
```sh output
118+
Type `yes` when prompted.
170119

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:
173123
+ create
174124

175125
Terraform will perform the following actions:
176126

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>"
192145
}
193146

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

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

200-
## 5. Verify the results
153+
Enter a value: yes
201154

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.
155+
cloudflare_dns_record.example_dns_record: Creating...
156+
cloudflare_dns_record.example_dns_record: Creation complete after 0s
203157

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`.
158+
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
159+
```
160+
161+
After creation, verify the DNS record:
205162

206163
```sh
207-
terraform show
164+
dig www.example.com
208165
```
209166

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-
```
167+
Test the web server response:
235168

236169
```sh
237170
curl https://www.example.com
238171
```
239-
240172
```sh output
241173
Hello, this is 203.0.113.10!
242174
```
175+
176+
To see the full results returned from the API call:
177+
178+
```sh
179+
terraform show
180+
```
181+
182+
You can also check the [Cloudflare dashboard](https://dash.cloudflare.com) and go to **DNS** > **Records**.

0 commit comments

Comments
 (0)