-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2.1.Terraform基础概念——Provider.html
1188 lines (546 loc) · 53.8 KB
/
2.1.Terraform基础概念——Provider.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>Provider · 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="2.2.Terraform基础概念——状态管理.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 active" 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 " 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="." >Provider</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="terraform基础概念——provider">Terraform基础概念——Provider</h1>
<p>Terraform被设计成一个多云基础设施编排工具,不像CloudFormation那样绑定AWS平台,Terraform可以同时编排各种云平台或是其他基础设施的资源。Terraform实现多云编排的方法就是Provider插件机制。</p>
<p><img src="https://gitee.com/lonegunman/introduction-to-terraform-pic/raw/master/2020-11-16/1605496195438-image.png" alt=""></p>
<p>Terraform使用的是HashiCorp自研的go-plugin库(<a href="https://github.com/hashicorp/go-plugin" target="_blank">https://github.com/hashicorp/go-plugin</a>),本质上各个Provider插件都是独立的进程,与Terraform进程之间通过rpc进行调用。Terraform引擎首先读取并分析用户编写的Terraform代码,形成一个由data与resource组成的图(Graph),再通过rpc调用这些data与resource所对应的Provider插件;Provider插件的编写者根据Terraform所制定的插件框架来定义各种data和resource,并实现相应的CRUD方法;在实现这些CRUD方法时,可以调用目标平台提供的SDK,或是直接通过调用Http(s) API来操作目标平台。</p>
<h2 id="下载provider">下载Provider</h2>
<p>我们在第一章的小例子中,写完代码后在apply之前,首先我们执行了一次terraform init。terraform init会分析代码中所使用到的Provider,并尝试下载Provider插件到本地。如果我们观察执行完第一章例子的文件夹,我们会发现有一个.terraform文件夹,我们所使用的UCloud Provider插件就被下载安装在里面。</p>
<pre class="language-"><code class="lang-bash">.terraform
└── plugins
├── registry.terraform.io
│ └── ucloud
│ └── ucloud
│ └── <span class="token number">1.22</span>.0
│ └── darwin_amd64 -<span class="token operator">></span> /Users/byers/.terraform.d/plugin-cache/registry.terraform.io/ucloud/ucloud/1.22.0/darwin_amd64
└── selections.json
</code></pre>
<p>有的时候下载某些Provider会非常缓慢,或是在开发环境中存在许多的Terraform项目,每个项目都保有自己独立的插件文件夹非常浪费磁盘,这时我们可以使用插件缓存。</p>
<p>有两种方式可以启用插件缓存,一种是配置TF_PLUGIN_CACHE_DIR这个环境变量:</p>
<pre class="language-"><code class="lang-bash"><span class="token builtin class-name">export</span> <span class="token assign-left variable">TF_PLUGIN_CACHE_DIR</span><span class="token operator">=</span><span class="token string">"<span class="token environment constant">$HOME</span>/.terraform.d/plugin-cache"</span>
</code></pre>
<p>第二种方法是使用CLI配置文件。Windows下是在相关用户的%APPDATA%目录下创建名为"terraform.rc"的文件,Macos和Linux用户则是在用户的home下创建名为".terraformrc"的文件。在文件中配置如下:</p>
<pre class="language-"><code class="lang-config">plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
</code></pre>
<p>当启用插件缓存之后,每当执行terraform init命令时,Terraform引擎会首先检查期望使用的插件在缓存文件夹中是否已经存在,如果存在,那么就会将缓存的插件拷贝到当前工作目录下的.terraform文件夹内。。如果插件不存在,那么Terraform仍然会像之前那样下载插件,并首先保存在插件文件夹中,随后再从插件文件夹拷贝到当前工作目录下的.terraform文件夹内。为了尽量避免同一份插件被保存多次,只要操作系统提供支持,Terraform就会使用符号连接而不是实际从插件缓存目录拷贝到工作目录。</p>
<p>Terrafom引擎永远不会主动删除缓存文件夹中的插件,缓存文件夹的尺寸可能会随着时间而增长到非常大,这时需要手工清理。</p>
<h2 id="搜索provider">搜索Provider</h2>
<p>想要了解有哪些被官方接纳的Provider,有两种方法,第一种是访问<a href="https://www.terraform.io/docs/providers/index.html" target="_blank">https://www.terraform.io/docs/providers/index.html</a>,列出了主流的Provider:</p>
<p><img src="https://gitee.com/lonegunman/introduction-to-terraform-pic/raw/master/2020-11-16/1605498841554-image.png" alt=""></p>
<p><img src="https://gitee.com/lonegunman/introduction-to-terraform-pic/raw/master/2020-11-19/1605799271127-image.png" alt=""></p>
<p>第二种方式就是前往<a href="https://registry.terraform.io/browse/providers" target="_blank">registry.terraform.io</a>进行搜索:</p>
<p><img src="https://gitee.com/lonegunman/introduction-to-terraform-pic/raw/master/2020-11-19/1605799350775-image.png" alt=""></p>
<p>目前推荐在registry搜索Provider,因为大量由社区开发的Provider都被注册在了那里。</p>
<p><img src="https://gitee.com/lonegunman/introduction-to-terraform-pic/raw/master/2020-11-16/1605498907942-image.png" alt=""></p>
<p><img src="https://gitee.com/lonegunman/introduction-to-terraform-pic/raw/master/2020-11-16/1605498945181-image.png" alt=""></p>
<p><img src="https://gitee.com/lonegunman/introduction-to-terraform-pic/raw/master/2020-11-16/1605499003904-image.png" alt=""></p>
<p>一般来说,相关Provider如何声明,以及相关data、resource的使用说明,都可以在registry上查阅到相关文档。</p>
<p>registry.terraform.io不但可以查询Provider,也可以用来发布Provider;并且它也可以用来查询和发布模块(Module),不过模块将是我们后续篇章讨论的话题。</p>
<h2 id="provider的声明">Provider的声明</h2>
<p>一组Terraform代码要被执行,相关的Provider必须在代码中被声明。不少的Provider在声明时需要传入一些关键信息才能被使用,例如我们在第一章的例子中,必须给出访问密钥以及期望执行的UCloud区域信息。</p>
<pre class="language-"><code class="lang-hcl"><span class="token keyword">terraform</span> <span class="token punctuation">{</span>
<span class="token keyword">required_providers</span> <span class="token punctuation">{</span>
<span class="token property">ucloud</span> <span class="token punctuation">=</span> <span class="token punctuation">{</span>
<span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">"ucloud/ucloud"</span>
<span class="token property">version</span> <span class="token punctuation">=</span> <span class="token string">">=1.24.1"</span>
<span class="token punctuation">}</span>