Skip to content

Commit 7b51696

Browse files
authored
RM-22227: Update initialize-terraform.mdx (#24538)
* RM-22227: Update initialize-terraform.mdx * Update initialize-terraform.mdx * Update initialize-terraform.mdx
1 parent d6ae155 commit 7b51696

File tree

1 file changed

+85
-147
lines changed

1 file changed

+85
-147
lines changed

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

Lines changed: 85 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,22 @@ 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" product="terraform" />
19+
## 1. Create your configuration
1820

19-
## 1. Define your first Terraform config file
20-
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-
:::
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/):
2922

3023
```bash
31-
cat > cloudflare.tf <<'EOF'
3224
terraform {
3325
required_providers {
3426
cloudflare = {
35-
source = "cloudflare/cloudflare"
36-
version = "~> 4"
27+
source = "cloudflare/cloudflare"
28+
version = "~> 5"
3729
}
3830
}
3931
}
@@ -54,189 +46,135 @@ variable "domain" {
5446
default = "<YOUR_DOMAIN>"
5547
}
5648

57-
resource "cloudflare_record" "www" {
58-
zone_id = var.zone_id
49+
resource "cloudflare_dns_record" "www" {
50+
zone_id = "<YOUR_ZONE_ID>"
5951
name = "www"
60-
value = "203.0.113.10"
52+
content = "203.0.113.10"
6153
type = "A"
54+
ttl = 1
6255
proxied = true
56+
comment = "Domain verification record"
6357
}
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
7358
```
59+
:::caution
7460

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

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
10165

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

10468
```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
69+
terraform init
11470
```
11571

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.
72+
Review what will be created:
11973

12074
```sh
12175
terraform plan
12276
```
123-
12477
```sh output
12578

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
80+
indicated with the following symbols:
12881
+ create
12982

13083
Terraform will perform the following actions:
13184

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

149105
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.
155106
```
156107

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
160109

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

165112
```sh
166-
terraform apply --auto-approve
113+
terraform apply
167114
```
168115

169-
```sh output
116+
Type `yes` when prompted.
170117

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:
173121
+ create
174122

175123
Terraform will perform the following actions:
176124

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>"
192143
}
193144

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

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

200-
## 5. Verify the results
153+
cloudflare_dns_record.www: Creating...
154+
cloudflare_dns_record.www: Creation complete after 0s
201155

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.
156+
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
157+
```
203158

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

206161
```sh
207-
terraform show
162+
dig www.example.com
208163
```
209164

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

236167
```sh
237168
curl https://www.example.com
238169
```
239-
240170
```sh output
241171
Hello, this is 203.0.113.10!
242172
```
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

Comments
 (0)