-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3.6.资源.html
1447 lines (781 loc) · 102 KB
/
3.6.资源.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>资源 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism-tomorrow.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="3.7.数据源.html" />
<link rel="prev" href="3.5.局部值.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="1.Terraform初步体验.html">
<a href="1.Terraform初步体验.html">
Terraform初步体验
</a>
</li>
<li class="chapter " data-level="1.3" >
<span>
Terraform基础概念
</span>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="2.1.Terraform基础概念——Provider.html">
<a href="2.1.Terraform基础概念——Provider.html">
Provider
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="2.2.Terraform基础概念——状态管理.html">
<a href="2.2.Terraform基础概念——状态管理.html">
状态管理
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="3.Terraform代码的书写.html">
<a href="3.Terraform代码的书写.html">
Terraform代码的书写
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="3.1.类型.html">
<a href="3.1.类型.html">
类型
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="3.2.配置语法.html">
<a href="3.2.配置语法.html">
配置语法
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="3.3.输入变量.html">
<a href="3.3.输入变量.html">
输入变量
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="3.4.输出值.html">
<a href="3.4.输出值.html">
输出值
</a>
</li>
<li class="chapter " data-level="1.4.5" data-path="3.5.局部值.html">
<a href="3.5.局部值.html">
局部值
</a>
</li>
<li class="chapter active" data-level="1.4.6" data-path="3.6.资源.html">
<a href="3.6.资源.html">
资源
</a>
</li>
<li class="chapter " data-level="1.4.7" data-path="3.7.数据源.html">
<a href="3.7.数据源.html">
数据源
</a>
</li>
<li class="chapter " data-level="1.4.8" data-path="3.8.表达式.html">
<a href="3.8.表达式.html">
表达式
</a>
</li>
<li class="chapter " data-level="1.4.9" data-path="3.9.重载文件.html">
<a href="3.9.重载文件.html">
重载文件
</a>
</li>
<li class="chapter " data-level="1.4.10" data-path="3.10.代码风格规范.html">
<a href="3.10.代码风格规范.html">
代码风格规范
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="4.Terraform模块.html">
<a href="4.Terraform模块.html">
Terraform模块
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="4.1.创建模块.html">
<a href="4.1.创建模块.html">
创建模块
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="4.2.使用模块.html">
<a href="4.2.使用模块.html">
使用模块
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="5.Terraform命令行.html">
<a href="5.Terraform命令行.html">
Terraform命令行
</a>
<ul class="articles">
<li class="chapter " data-level="1.6.1" data-path="5.1.命令行配置文件.html">
<a href="5.1.命令行配置文件.html">
命令行配置文件
</a>
</li>
<li class="chapter " data-level="1.6.2" data-path="5.2.环境变量.html">
<a href="5.2.环境变量.html">
环境变量
</a>
</li>
<li class="chapter " data-level="1.6.3" data-path="5.3.资源地址.html">
<a href="5.3.资源地址.html">
资源地址
</a>
</li>
<li class="chapter " data-level="1.6.4" data-path="5.4.apply.html">
<a href="5.4.apply.html">
apply
</a>
</li>
<li class="chapter " data-level="1.6.5" data-path="5.5.console.html">
<a href="5.5.console.html">
console
</a>
</li>
<li class="chapter " data-level="1.6.6" data-path="5.6.destroy.html">
<a href="5.6.destroy.html">
destroy
</a>
</li>
<li class="chapter " data-level="1.6.7" data-path="5.7.fmt.html">
<a href="5.7.fmt.html">
fmt
</a>
</li>
<li class="chapter " data-level="1.6.8" data-path="5.8.force-unlock.html">
<a href="5.8.force-unlock.html">
force-unlock
</a>
</li>
<li class="chapter " data-level="1.6.9" data-path="5.9.get.html">
<a href="5.9.get.html">
get
</a>
</li>
<li class="chapter " data-level="1.6.10" data-path="5.10.graph.html">
<a href="5.10.graph.html">
graph
</a>
</li>
<li class="chapter " data-level="1.6.11" data-path="5.11.import.html">
<a href="5.11.import.html">
import
</a>
</li>
<li class="chapter " data-level="1.6.12" data-path="5.12.init.html">
<a href="5.12.init.html">
init
</a>
</li>
<li class="chapter " data-level="1.6.13" data-path="5.13.output.html">
<a href="5.13.output.html">
output
</a>
</li>
<li class="chapter " data-level="1.6.14" data-path="5.14.plan.html">
<a href="5.14.plan.html">
plan
</a>
</li>
<li class="chapter " data-level="1.6.15" data-path="5.15.providers.html">
<a href="5.15.providers.html">
providers
</a>
</li>
<li class="chapter " data-level="1.6.16" data-path="5.16.refresh.html">
<a href="5.16.refresh.html">
refresh
</a>
</li>
<li class="chapter " data-level="1.6.17" data-path="5.17.show.html">
<a href="5.17.show.html">
show
</a>
</li>
<li class="chapter " data-level="1.6.18" data-path="5.18.state.html">
<a href="5.18.state.html">
state
</a>
<ul class="articles">
<li class="chapter " data-level="1.6.18.1" data-path="5.18.1.list.html">
<a href="5.18.1.list.html">
list
</a>
</li>
<li class="chapter " data-level="1.6.18.2" data-path="5.18.2.mv.html">
<a href="5.18.2.mv.html">
mv
</a>
</li>
<li class="chapter " data-level="1.6.18.3" data-path="5.18.3.pull.html">
<a href="5.18.3.pull.html">
pull
</a>
</li>
<li class="chapter " data-level="1.6.18.4" data-path="5.18.4.push.html">
<a href="5.18.4.push.html">
push
</a>
</li>
<li class="chapter " data-level="1.6.18.5" data-path="5.18.5.replace-provider.html">
<a href="5.18.5.replace-provider.html">
replace-provider
</a>
</li>
<li class="chapter " data-level="1.6.18.6" data-path="5.18.6.rm.html">
<a href="5.18.6.rm.html">
rm
</a>
</li>
<li class="chapter " data-level="1.6.18.7" data-path="5.18.7.show.html">
<a href="5.18.7.show.html">
show
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6.19" data-path="5.19.taint.html">
<a href="5.19.taint.html">
taint
</a>
</li>
<li class="chapter " data-level="1.6.20" data-path="5.20.validate.html">
<a href="5.20.validate.html">
validate
</a>
</li>
<li class="chapter " data-level="1.6.21" data-path="5.21.untaint.html">
<a href="5.21.untaint.html">
untaint
</a>
</li>
<li class="chapter " data-level="1.6.22" data-path="5.22.workspace.html">
<a href="5.22.workspace.html">
workspace
</a>
<ul class="articles">
<li class="chapter " data-level="1.6.22.1" data-path="5.22.1.list.html">
<a href="5.22.1.list.html">
list
</a>
</li>
<li class="chapter " data-level="1.6.22.2" data-path="5.22.2.select.html">
<a href="5.22.2.select.html">
select
</a>
</li>
<li class="chapter " data-level="1.6.22.3" data-path="5.22.3.new.html">
<a href="5.22.3.new.html">
new
</a>
</li>
<li class="chapter " data-level="1.6.22.4" data-path="5.22.4.delete.html">
<a href="5.22.4.delete.html">
delete
</a>
</li>
<li class="chapter " data-level="1.6.22.5" data-path="5.22.5.show.html">
<a href="5.22.5.show.html">
show
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="1.7" data-path=".6.Aha.md">
<span>
Aha
</a>
<ul class="articles">
<li class="chapter " data-level="1.7.1" data-path="6.1.有条件创建.html">
<a href="6.1.有条件创建.html">
有条件创建
</a>
</li>
<li class="chapter " data-level="1.7.2" data-path="6.2.依赖反转.html">
<a href="6.2.依赖反转.html">
依赖反转
</a>
</li>
<li class="chapter " data-level="1.7.3" data-path="6.3.多可用区分布.html">
<a href="6.3.多可用区分布.html">
多可用区分布
</a>
</li>
<li class="chapter " data-level="1.7.4" data-path="6.4.provisioner与user_data.html">
<a href="6.4.provisioner与user_data.html">
provisioner与user_data
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.8" data-path="7.后记.html">
<a href="7.后记.html">
后记
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >资源</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="资源">资源</h1>
<p>资源是Terraform最重要的组成部分,而本节亦是本教程最重要的一节。资源通过resource块来定义,一个resource可以定义一个或多个基础设施资源对象,例如VPC、虚拟机,或是DNS记录、Consul的键值对数据等。</p>
<h2 id="资源语法">资源语法</h2>
<p>资源通过resource块定义,我们首先讲解通过resource块定义单个资源对象的场景。</p>
<pre class="language-"><code class="lang-hcl"><span class="token keyword">resource <span class="token type variable">"aws_instance"</span></span> <span class="token string">"web"</span> <span class="token punctuation">{</span>
<span class="token property">ami</span> <span class="token punctuation">=</span> <span class="token string">"ami-a1b2c3d4"</span>
<span class="token property">instance_type</span> <span class="token punctuation">=</span> <span class="token string">"t2.micro"</span>
<span class="token punctuation">}</span>
</code></pre>
<p>紧跟resource关键字的是资源类型,在上面的例子里就是aws_instance。后面是资源的Local Name,例子里就是web。Local Name可以在同一模块内的代码里被用来引用该资源,但类型加Local Name的组合在当前模块内必须是唯一的,不同类型的两个资源Local Name可以相同。随后的花括号内的内容就是块体,创建资源所用到的各种参数的值就在块体内定义。例子中我们定义了虚拟机所使用的镜像id以及虚拟机的尺寸。</p>
<h2 id="资源参数">资源参数</h2>
<p>不同资源定义了不同的可赋值的属性,官方文档将之称为参数(Argument),有些参数是必填的,有些参数是可选的。使用某项资源前可以通过阅读相关文档了解参数列表以及他们的含义、赋值的约束条件。</p>
<p>参数值可以是简单的字面量,也可以是一个复杂的表达式。</p>
<h2 id="资源类型的文档">资源类型的文档</h2>
<p>每一个Terraform Provider都有自己的文档,用以描述它所支持的资源类型种类,以及每种资源类型所支持的属性列表。</p>
<p>大部分公共的Provider都是通过<a href="https://registry.terraform.io/browse/providers" target="_blank">Terraform Registry</a>连带文档一起发布的。当我们在Terraform Registry站点上浏览一个Provider的页面时,我们可以点击"Documentation"链接来浏览相关文档。Provider的文档都是版本化的,我们可以选择特定版本的Provider文档。</p>
<p>需要注意的是,Provider文档曾经是直接托管在terraform.io站点上的,也就是Terraform核心主站的一部分,有些Provider的文档目前依然托管在那里,但目前Terraform Rregistry才是所有公共Provider文档的主站(唯一的例外是用来读取其他Terraform状态数据的内建的terraform provider,它的文档目前不在Terraform Registry上)。</p>
<h2 id="资源的行为">资源的行为</h2>
<p>一个resource块声明了作者想要创建的一个确切的基础设施对象,并且设定了各项属性的值。如果我们正在编写一个新的Terraform代码文件,那么代码所定义的资源仅仅只在代码中存在,并没有与之对应的实际的基础设施资源存在。</p>
<p>对一组Terraform代码执行terraform apply可以创建、更新或者销毁实际的基础设施对象,Terraform会制定并执行变更计划,以使得实际的基础设施符合代码的定义。</p>
<p>每当Terraform按照一个resource块创建了一个新的基础设施对象,这个实际的对象的id会被保存进Terraform状态中,使得将来Terraform可以根据变更计划对它进行更新或是销毁操作。如果一个resource块描述的资源在状态文件中已有记录,那么Terraform会比对记录的状态与代码描述的状态,如果有必要,Terraform会制定变更计划以使得资源状态能够符合代码的描述。</p>
<p>这种行为适用于所有资源而无关其类型。创建、更新、销毁一个资源的细节会根据资源类型而不同,但是这个行为规则却是普适的。</p>
<h2 id="访问资源输出属性">访问资源输出属性</h2>
<p>资源不但可以通过参数传值,成功创建的资源还对外输出一些通过调用API才能获得的只读数据,经常包含了一些我们在实际创建一个资源之前无法获知的数据,比如云主机的id等,官方文档将之称为属性(Attribute)。我们可以在同一模块内的代码中引用资源的属性来创建其他资源或是表达式。在表达式中引用资源属性的语法是\<resource type\="">.\<name\>.\<attribute\>。</attribute\></name\></resource></p>
<p>要获取一个资源类型输出的属性列表,我们可以查阅对应的Provider文档,一般在文档中会专门记录资源的输出属性列表。</p>
<h2 id="资源的依赖关系">资源的依赖关系</h2>
<p>我们在介绍输出值的depends_on的时候已经简单介绍过了依赖关系。一般来说在Terraform代码定义的资源之间不会有特定的依赖关系,Terraform可以并行地对多个无依赖关系的资源执行变更,默认情况下这个并行度是10。</p>
<p>然而,创建某些资源所需要的信息依赖于另一个资源创建后输出的属性,又或者必须在某些资源成功创建后才可以被创建,这时资源之间就存在依赖关系。</p>
<p>大部分资源间的依赖关系可以被Terraform自动处理,Terraform会分析resource块内的表达式,根据表达式的引用链来确定资源之间的引用,进而计算出资源在创建、更新、销毁时的执行顺序。大部分情况下,我们不需要显式指定资源之间的依赖关系。</p>
<p>然而,有时候某些依赖关系是无法从代码中推导出来的。例如,Terraform必须要创建一个访问控制权限资源,以及另一个需要该权限才能成功创建的资源。后者的创建依赖于前者的成功创建,然而这种依赖在代码中没有表现为数据引用关联,这种情况下,我们需要用depends_on来显式声明这种依赖关系。</p>
<h2 id="元参数">元参数</h2>
<p>resource块支持几种元参数声明,这些元参数可以被声明在所有类型的resource块内,它们将会改变资源的行为:</p>
<ul>
<li>depends_on:显式声明依赖关系</li>
<li>count:创建多个资源实例</li>
<li>for_each:迭代集合,为集合中每一个元素创建一个对应的资源实例</li>
<li>provider:指定非默认Provider实例</li>
<li>lifecycle:自定义资源的生命周期行为</li>
<li>provisioner 和 connection:在资源创建后执行一些额外的操作</li>
</ul>
<p>下面我们将逐一讲解他们的用法。</p>
<h3 id="dependson">depends_on</h3>