-
Notifications
You must be signed in to change notification settings - Fork 155
/
terraform-cheat-sheet_temp.html
400 lines (393 loc) · 13.2 KB
/
terraform-cheat-sheet_temp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="./examples/cheat-sheet/template/assets/style.css" rel="stylesheet">
</head>
<body>
<header>
<h1><img width="50" height="50" src="examples/cheat-sheet/template/assets/logo.png"/> Terraform CLI Cheat Sheet</h1>
</header>
<section class="content">
<div class="sect1">
<h2 id="_about_terraform_cli">About Terraform CLI</h2>
<div class="sectionbody">
<p class="">Terraform, a tool created by <a href="https://www.hashicorp.com/">Hashicorp</a> in 2014,
written in Go, aims to build, change and version control your infrastructure.
This tool have a powerfull and very intuitive Command Line Interface.</p>
</div>
</div>
<div class="sect1">
<h2 id="_installation">Installation</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_install_through_curl">Install through curl</h3>
<div class="literalblock">
<div class="content">
<pre>$ curl -O https://releases.hashicorp.com/terraform/
1.4.6/terraform_1.4.6_darwin_amd64.zip
$ sudo terraform_1.4.6_darwin_amd64.zip
-d /usr/local/bin/
$ rm terraform_1.4.6_darwin_amd64.zip</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_or_install_through_tfenv_a_terraform_version_manager">OR install through tfenv: a Terraform version manager</h3>
<p class="">First of all, download the tfenv binary and put it in your PATH.</p>
<div class="literalblock">
<div class="content">
<pre>$ git clone https://github.com/tfutils/tfenv.git
--depth=1 ~/.tfenv
$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"'
>> ~/.bash_profile</pre>
</div>
</div>
<p class="">Then, you can install and use desired version of terraform:</p>
<div class="literalblock">
<div class="content">
<pre>$ tfenv install 1.4.6
$ tfenv use 1.4.6</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_usage">Usage</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_show_version">Show version</h3>
<div class="literalblock">
<div class="content">
<pre>$ terraform -v
Terraform v1.4.6</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_init_terraform">Init Terraform</h3>
<p class=""><code>$ terraform init</code></p>
<p class="">It’s the first command you need to execute. Unless, terraform plan, apply,
destroy and import will not work. The command terraform init will install :</p>
<div class="ulist">
<ul>
<li>
<p>terraform modules</p>
</li>
<li>
<p>eventually a backend</p>
</li>
<li>
<p>and provider(s) plugins</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="_init_terraform_and_don_t_ask_any_input">Init Terraform and don’t ask any input</h3>
<p class=""><code>$ terraform init -input=false</code></p>
</div>
<div class="sect2">
<h3 id=""> </h3>
</div>
<div class="sect2">
<h3 id="_2"> </h3>
</div>
<div class="sect2">
<h3 id="_change_backend_configuration_during_the_init">Change backend configuration during the init</h3>
<p class=""><code>$ terraform init -backend-config=cfg/s3.dev.tf -reconfigure</code></p>
<p class=""><code>-reconfigure</code> is used in order to tell terraform to not copy the existing state to the new remote state location.</p>
</div>
<div class="sect2">
<h3 id="_get">Get</h3>
<p class="">This command is useful when you have defined some modules. Modules are vendored
so when you edit them, you need to get again modules content.</p>
<p class=""><code>$ terraform get -update=true</code></p>
<p class="">When you use modules, the first thing you’ll have to do is to do a terraform
get. This pulls modules into the .terraform directory. Once you do that, unless
you do another <code>terraform get -update=true</code>, you’ve essentially vendored those
modules.</p>
</div>
<div class="sect2">
<h3 id="_plan">Plan</h3>
<p class="">The plan step check configuration to execute and write a plan to apply to target
infrastructure provider.</p>
<p class=""><code>$ terraform plan -out plan.out</code></p>
<p class="">It’s an important feature of Terraform that allows a user to see which actions
Terraform will perform prior to making any changes, increasing confidence that a
change will have the desired effect once applied.</p>
<p class="">When you execute terraform plan command, terraform will scan all *.tf files in
your directory and create the plan.</p>
</div>
<div class="sect2">
<h3 id="_apply">Apply</h3>
<p class="">Now you have the desired state so you can execute the plan.</p>
<p class=""><code>$ terraform apply plan.out</code></p>
<p class=""><strong>Good to know:</strong> Since terraform v0.11+, in an interactive mode (non
CI/CD/autonomous pipeline), you can just execute <code>terraform apply</code> command which
will print out which actions TF will perform.</p>
<p class="">By generating the plan and applying it in the same command, Terraform can
guarantee that the execution plan won’t change, without needing to write it to
disk. This reduces the risk of potentially-sensitive data being left behind, or
accidentally checked into version control.</p>
<p class=""><code>$ terraform apply</code></p>
<div class="sect3">
<h4 id="_apply_and_auto_approve">Apply and auto approve</h4>
<p class=""><code>$ terraform apply -auto-approve</code></p>
</div>
<div class="sect3">
<h4 id="_3"> </h4>
</div>
<div class="sect3">
<h4 id="_apply_and_define_new_variables_value">Apply and define new variables value</h4>
<div class="literalblock">
<div class="content">
<pre>$ terraform apply -auto-approve
-var tags-repository_url=${GIT_URL}</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_apply_only_one_module">Apply only one module</h4>
<div class="literalblock">
<div class="content">
<pre>$ terraform apply -target=module.s3</pre>
</div>
</div>
<p class="">This <em>-target</em> option works with <em>terraform plan</em> too.</p>
</div>
</div>
<div class="sect2">
<h3 id="_destroy">Destroy</h3>
<p class=""><code>$ terraform destroy</code></p>
<p class="">Delete all the resources!</p>
<p class="">A deletion plan can be created before:</p>
<p class=""><code>$ terraform plan –destroy</code></p>
<p class=""><code>-target</code> option allow to destroy only one resource, for example a S3 bucket :</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform destroy -target aws_s3_bucket.my_bucket</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_debug">Debug</h3>
<p class="">The Terraform console command is useful for testing interpolations before using them in configurations. Terraform console will read configured state even if it is remote.</p>
<div class="literalblock">
<div class="content">
<pre>$ echo "aws_iam_user.notif.arn" | terraform console
arn:aws:iam::123456789:user/notif</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_logs_level">Logs level</h3>
<p class="">Set the log to DEBUG level and save the log in an output external file.</p>
<div class="literalblock">
<div class="content">
<pre>$ TF_LOG_PATH=mylogfile.txt TF_LOG=debug
terraform apply</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_graph">Graph</h3>
<p class=""><code>$ terraform graph | dot –Tpng > graph.png</code></p>
<p class="">Visual dependency graph of terraform resources.</p>
</div>
<div class="sect2">
<h3 id="_validate">Validate</h3>
<p class="">Validate command is used to validate/check the syntax of the Terraform files. A syntax check is done on all the terraform files in the directory, and will display an error if any of the files doesn’t validate. The syntax check does not cover every syntax common issues.</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform validate</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_providers">Providers</h3>
<p class="">You can use a lot of providers/plugins in your terraform definition resources, so it can be useful to have a tree of providers used by modules in your project.</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform providers
.
├── provider.aws ~> 1.24.0
├── module.my_module
│ ├── provider.aws (inherited)
│ ├── provider.null
│ └── provider.template
└── module.elastic
└── provider.aws (inherited)</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_state">State</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_show_and_output_the_state_human_readable_way">Show and output the state (human readable way)</h3>
<div class="literalblock">
<div class="content">
<pre>$ terraform show</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_refresh">Refresh</h3>
<p class="">Compare the current real remote information and put it in the state.</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform refresh</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_pull_remote_state_in_a_local_copy">Pull remote state in a local copy</h3>
<div class="literalblock">
<div class="content">
<pre>$ terraform state pull > terraform.tfstate</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_push_state_in_remote_backend_storage">Push state in remote backend storage</h3>
<div class="literalblock">
<div class="content">
<pre>$ terraform state push</pre>
</div>
</div>
<p class="">This command is usefull if for example you riginally use a local tf state and then you define a backend storage, in S3 or Consul…​</p>
</div>
<div class="sect2">
<h3 id="_how_to_tell_to_terraform_you_moved_a_ressource_in_a_module">How to tell to Terraform you moved a ressource in a module?</h3>
<p class="">If you moved an existing resource in a module, you need to update the state:</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform state mv aws_iam_role.role1 module.mymodule</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_how_to_import_existing_resource_in_terraform">How to import existing resource in Terraform?</h3>
<p class="">If you have an existing resource in your infrastructure provider, you can import
it in your Terraform state:</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform import aws_iam_policy.elastic_post
arn:aws:iam::123456789:policy/elastic_post</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_workspaces">Workspaces</h2>
<div class="sectionbody">
<p class="">To manage multiple distinct sets of infrastructure resources/environments.</p>
<p class="">Instead of create a directory for each environment to manage, we need to just
create needed workspace and use them:</p>
<div class="sect2">
<h3 id="_create_workspace">Create workspace</h3>
<p class="">This command create a new workspace and then select it</p>
<p class=""><code>$ terraform workspace new dev</code></p>
</div>
<div class="sect2">
<h3 id="_select_a_workspace">Select a workspace</h3>
<p class=""><code>$ terraform workspace select dev</code></p>
</div>
<div class="sect2">
<h3 id="_list_workspaces">List workspaces</h3>
<div class="literalblock">
<div class="content">
<pre>$ terraform workspace list
default
* dev
prod</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_show_current_workspace">Show current workspace</h3>
<div class="literalblock">
<div class="content">
<pre>$ terraform workspace show
dev</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_tools">Tools</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_jq">jq</h3>
<p class="">jq is a lightweight command-line JSON processor. Combined with terraform output it can be powerful.</p>
<div class="sect3">
<h4 id="_installation_2">Installation</h4>
<p class="">For Linux:</p>
<p class=""><code>$ sudo apt-get install jq</code></p>
<p class="">or</p>
<p class=""><code>$ yum install jq</code></p>
<p class="">For OS X:</p>
<p class=""><code>$ brew install jq</code></p>
</div>
<div class="sect3">
<h4 id="_usage_2">Usage</h4>
<p class="">For example, we defind outputs in a module and when we execute <em>terraform apply</em> outputs are displayed:</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform apply
...
Apply complete! Resources: 0 added, 0 changed,
0 destroyed.
Outputs:
elastic_endpoint = vpc-toto-12fgfd4d5f4ds5fngetwe4.
eu-central-1.es.amazonaws.com</pre>
</div>
</div>
<p class="">We can extract the value that we want in order to use it in a script for example. With jq it’s easy:</p>
<div class="literalblock">
<div class="content">
<pre>$ terraform output -json
{
"elastic_endpoint": {
"sensitive": false,
"type": "string",
"value": "vpc-toto-12fgfd4d5f4ds5fngetwe4.
eu-central-1.es.amazonaws.com"
}
}
$ terraform output -json | jq '.elastic_endpoint.value'
"vpc-toto-12fgfd4d5f4ds5fngetwe4.eu-central-1.
es.amazonaws.com"</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_gcloud_bulk_export_in_terraform_format">gcloud bulk-export in terraform format</h3>
<p class="">Export natively Google Cloud resources in Terraform</p>
<div class="sect3">
<h4 id="_usage_3">Usage</h4>
<p class=""><code>$ gcloud beta resource-config bulk-export --resource-format=terraform</code></p>
<p class="">Resources types supported:</p>
<p class=""><code>$ gcloud beta resource-config list-resources</code></p>
</div>
</div>
</div>
</div>
<div class="sect1 authors">
<h3>Authors :</h3>
<div class="author">
<div class="author-avatar"><img src="https://pbs.twimg.com/profile_images/1589660843012128776/lb5thctk_400x400.jpg"/></div>
<div class="author-name"><a href="https://twitter.com/aurelievache">@aurelievache</a></div>
<div class="author-bio">DevRel at OVHcloud</div>
</div>
<div class="author-bio">v1.0.4</div>
</div>
</section>
</body>