Skip to content

Commit 6a3a1df

Browse files
Ashnayakdcpena
andauthored
RM-22227: Update track-history.mdx (#24638)
* Update track-history.mdx * Update src/content/docs/terraform/tutorial/track-history.mdx Co-authored-by: Denise Peña <[email protected]> * Update track-history.mdx --------- Co-authored-by: Denise Peña <[email protected]>
1 parent 807955c commit 6a3a1df

File tree

1 file changed

+74
-99
lines changed

1 file changed

+74
-99
lines changed

src/content/docs/terraform/tutorial/track-history.mdx

Lines changed: 74 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,131 +10,106 @@ head:
1010

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

13-
In the [Initialize Terraform](/terraform/tutorial/initialize-terraform/) tutorial, you created and applied some basic Cloudflare configuration. Terraform applied this configuration to your zone because you provided your API token at the top of the `cloudflare.tf` file that has access to this zone.
14-
15-
```sh
16-
head -n13 cloudflare.tf | tail -n3
17-
provider "cloudflare" {
18-
api_token = "your-api-token"
19-
}
20-
```
21-
22-
In this tutorial, you will store your configuration in GitHub where it can be tracked, peer-reviewed, and rolled back to as needed. First, you will remove your credentials from the Terraform config file to prevent committing them to a repository.
23-
24-
<Render file="v4-code-snippets" product="terraform" />:
13+
In the [Initialize Terraform](/terraform/tutorial/initialize-terraform/) tutorial, you created and applied basic Cloudflare configuration. Now you'll store this configuration in version control for tracking, peer review, and rollback capabilities.
2514

2615
## 1. Use environment variables for authentication
2716

28-
As a good security practice, remove your Cloudflare credentials from anything that will be committed to a repository. The Cloudflare Terraform provider supports reading the credentials (and other configuration) [from environment variables](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs#schema), as in the following example:
29-
30-
```bash
31-
sed -ie 's/^.*api_token =.*$/ # token pulled from $CLOUDFLARE_API_TOKEN/' cloudflare.tf
17+
Remove credentials from your Terraform files before committing to version control. The Cloudflare provider v5 reads authentication from environment variables automatically.
18+
Update your `main.tf` file to remove the hardcoded API token:
19+
20+
```hcl
21+
terraform {
22+
required_providers {
23+
cloudflare = {
24+
source = "cloudflare/cloudflare"
25+
version = "~> 5"
26+
}
27+
}
28+
}
3229
33-
head -n13 cloudflare.tf | tail -n3
3430
provider "cloudflare" {
35-
# token pulled from $CLOUDFLARE_API_TOKEN
31+
# API token will be read from CLOUDFLARE_API_TOKEN environment variable
3632
}
3733
38-
export CLOUDFLARE_API_TOKEN=your-api-token
39-
```
34+
variable "zone_id" {
35+
description = "Cloudflare Zone ID"
36+
type = string
37+
sensitive = true
38+
}
4039
41-
You must still include the empty provider definition in the file, so that Terraform knows to install the Cloudflare plugin. For more information about advanced options you can use to customize the Cloudflare provider, refer to [Provider customization](/terraform/advanced-topics/provider-customization/).
40+
variable "account_id" {
41+
description = "Cloudflare Account ID"
42+
type = string
43+
sensitive = true
44+
}
4245
43-
After running the commands above, ensure that you can still authenticate to Cloudflare by running `terraform plan`. Terraform will pull the current state which requires a valid email and API token.
46+
variable "domain" {
47+
description = "Domain name"
48+
type = string
49+
default = "example.com"
50+
}
4451
45-
```sh
46-
terraform plan
52+
resource "cloudflare_dns_record" "www" {
53+
zone_id = var.zone_id
54+
name = "www"
55+
content = "203.0.113.10"
56+
type = "A"
57+
ttl = 1
58+
proxied = true
59+
comment = "Domain verification record"
60+
}
4761
```
62+
:::note
63+
You must still include the empty provider definition in the file, so that Terraform knows to install the Cloudflare plugin. For more information about advanced options you can use to customize the Cloudflare provider, refer to [Provider customization](/terraform/advanced-topics/provider-customization/).
64+
:::
4865

49-
```sh output
50-
cloudflare_record.www: Refreshing state... [id=c38d3102767284e7ca14d5dad3ab8b69]
51-
52-
------------------------------------------------------------------------
53-
54-
No changes. Infrastructure is up-to-date.
55-
56-
This means that Terraform did not detect any differences between your
57-
configuration and real physical resources that exist. As a result, no
58-
actions need to be performed.
66+
Update your `terraform.tfvars` file:
67+
```hcl
68+
zone_id = "your-zone-id-here"
69+
account_id = "your-account-id-here"
70+
domain = "your-domain.com"
5971
```
6072

61-
## 2. Store configuration in GitHub
62-
63-
After removing the credentials, initialize a Git repository with your Cloudflare configuration and then push it to GitHub.
64-
65-
First, create the GitHub repository to store the configuration. You can do this via the GitHub user interface or with an API call.
66-
73+
Ensure your API token is set as an environment variable:
6774
```sh
68-
export GITHUB_USER=your-github-user
69-
export GITHUB_TOKEN=your-github-token
70-
71-
export GITHUB_URL=$(curl -H "Authorization: token $GITHUB_TOKEN" -d '{"name": "cf-config", "private": true}' "https://api.github.com/user/repos" 2> /dev/null | jq -r .ssh_url)
72-
73-
echo $GITHUB_URL
74-
75-
[email protected]:$GITHUB_USER/cf-config.git
75+
export CLOUDFLARE_API_TOKEN="your-api-token-here"
7676
```
7777

78-
Next, initialize a Git repository and make the first commit.
79-
80-
:::note[Note]
81-
82-
You might need to [add your SSH key to your GitHub account](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account).
83-
84-
:::
78+
Verify authentication works:
8579

8680
```sh
87-
git init
81+
terraform plan
8882
```
89-
83+
You may see changes detected as Terraform compares your new variable-based configuration with the existing resources. This is normal when migrating from hardcoded values to variables:
9084
```sh output
91-
Initialized empty Git repository in /Users/username/cf-config/.git/
92-
```
93-
94-
```sh
95-
git remote add origin $GITHUB_URL
96-
git add cloudflare.tf
97-
98-
git commit -m "Step 2 - Initial commit with webserver definition."
99-
```
85+
# cloudflare_dns_record.www will be updated in-place
86+
~ resource "cloudflare_dns_record" "www" {
87+
~ name = "www.your-domain.com" -> "www"
88+
~ zone_id = (sensitive value)
89+
# (other attributes may show changes)
90+
}
10091

101-
```sh output
102-
[master (root-commit) 5acea17] Step 2 - Initial commit with webserver definition.
103-
1 file changed, 16 insertions(+)
104-
create mode 100644 cloudflare.tf
92+
Plan: 0 to add, 1 to change, 0 to destroy.
10593
```
10694

107-
Notice that the `.terraform` directory and `terraform.tfstate` file were not committed. The `.terraform` directory was not committed because the repository may be used on a different architecture, and the plugins contained in the directory are built for the system on which `terraform init` was run. The `terraform.tfstate` file was not committed because it may eventually contain sensitive strings, and it is not a good way to keep state in sync, as explained in HashiCorp's documentation on [Remote State](https://developer.hashicorp.com/terraform/language/state/remote).
108-
109-
To prevent Git from notifying you about the two files, add them to a new `.gitignore` file, commit it, and push everything to GitHub.
110-
111-
```bash
112-
cat > .gitignore <<'EOF'
95+
## 2. Store configuration in GitHub
96+
Create a `.gitignore` file with these contents:
97+
```text
11398
.terraform/
114-
terraform.tfstate*
115-
EOF
116-
117-
git add .gitignore
118-
119-
git commit -m "Step 2 - Ignore terraform plugin directory and state file."
120-
```
121-
122-
```sh output
123-
[master 494c6d6] Step 2 - Ignore terraform plugin directory and state file.
124-
1 file changed, 2 insertions(+)
125-
create mode 100644 .gitignore
99+
*.tfstate*
100+
.terraform.lock.hcl
101+
terraform.tfvars
126102
```
127-
103+
Initialize Git and commit your configuration:
128104
```sh
129-
git push
105+
git init
106+
git add main.tf .gitignore
107+
git commit -m "Initial Terraform v5 configuration"
130108
```
131-
132-
```sh output
133-
Counting objects: 6, done.
134-
Delta compression using up to 8 threads.
135-
Compressing objects: 100% (4/4), done.
136-
Writing objects: 100% (6/6), 762 bytes | 0 bytes/s, done.
137-
Total 6 (delta 0), reused 0 (delta 0)
138-
To [email protected]:$GITHUB_USER/cf-config.git
139-
* [new branch] master -> master
109+
Create a GitHub repository (via web interface or GitHub CLI) and push:
110+
```sh
111+
git branch -M main
112+
git remote add origin https://github.com/YOUR_USERNAME/cf-config.git
113+
git push -u origin main
140114
```
115+
Your Terraform configuration is now version controlled and ready for team collaboration. The sensitive data (API tokens, zone IDs) remains secure and separate from your code.

0 commit comments

Comments
 (0)