-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5.11.import.html
1091 lines (455 loc) · 34.1 KB
/
5.11.import.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>import · 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="5.12.init.html" />
<link rel="prev" href="5.10.graph.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 " 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 active" 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="." >import</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="import">import</h1>
<p>terraform import命令用来将已经存在的资源对象导入Terraform。</p>
<p>我们并不总是那么幸运,能够在项目一开始就使用Terraform来构建和管理我们的基础设施;有时我们有一组已经运行着的基础设施资源,然后我们为它们编写了相应的Terraform代码,我们进行了测试,确认了这组代码描述的基础设施与当前正在使用的基础设施是等价的;但是我们仍然无法直接使用这套代码来管理现有的基础设施,因为我们缺乏了相应的状态文件。这时我们需要使用terraform import将资源对象“导入”到Terraform状态文件中去。</p>
<h2 id="用法">用法</h2>
<p>terraform import [options] ADDRESS ID</p>
<p>terraform import会根据资源ID找到相应资源,并将其信息导入到状态文件中ADDRESS对应的资源上。ADDRESS必须符合我们在资源地址中描述的合法资源地址格式,这样terraform import不但可以把资源导入到根模块中,也可以导入到子模块中。</p>
<p>ID取决于被导入的资源对象的类型。举例来说,AWS主机的ID格式类似于i-abcd1234,而AWS Route53 Zone的ID类似于Z12ABC4UGMOZ2N,请参考相关Provider文档来获取有关ID的详细信息。如果不确信的话,可以随便尝试任意ID。如果ID不合法,你会收到一个报错。</p>
<p>需要尤其注意的是,Terraform设想的是每一个资源对象都仅对应一个独一无二的实际基础设施对象,通常来说如果我们完全使用Terraform创建并管理基础设施时这一点不是问题;但如果你是通过导入的方式把基础设施对象导入到Terraform里,要绝对避免将同一个对象导入到两个以及更多不同的地址上,这会导致Terraform产生不可预测的行为。</p>
<p>该命令有以下参数可以使用:</p>
<ul>
<li>-backup=path:生成状态备份文件的地址,默认情况下是-state-out路径加上".backup"后缀名。设置为"-"可以关闭备份(不推荐)</li>
<li>-config=path:包含含有导入目标的Terraform代码的文件夹路径。默认为当前工作目录</li>
<li>-input=true:是否允许提示输入Provider配置信息</li>
<li>-lock=true:如果Backend支持,是否锁定状态文件</li>
<li>-lock-timeout=0s:重试获取状态锁的间隔</li>
<li>-no-color:如果指定,则不会输出彩色信息</li>
<li>-parallelism=n:限制Terraform遍历图的最大并行度,默认值为10(又是考点)</li>
<li>-state=path:要读取的状态文件的地址。默认为配置的Backend存储地址,或是"terraform.tfstate"文件</li>
<li>-state-out=path:指定修改后的状态文件的保存路径,默认情况下覆盖源状态文件。使用该参数可以生成一个新的状态文件,避免破坏现有状态文件</li>
<li>-var 'foo=bar':通过命令行设置输入变量值,类似apply命令中的介绍</li>
<li>-var-file=foo:类似apply命令中的介绍</li>
</ul>
<h2 id="provider配置">Provider配置</h2>
<p>Terraform会尝试读取要导入的资源对应的Provider的配置信息。如果找不到相关Provider的配置,那么Terraform会提示你输入相关的访问凭据。你也可以通过环境变量来配置访问凭据。</p>
<p>Terraform在读取Provider配置时唯一的限制是不能依赖于"非输入变量"的输入。举例来说,Provider配置不能依赖于数据源的输出。</p>
<p>举一个例子,如果你想要导入AWS资源而你有这样的一份代码文件,那么Terraform会使用这两个输入变量来配置AWS Provier:</p>
<pre class="language-"><code class="lang-hcl"><span class="token keyword">variable<span class="token type variable"> "access_key" </span></span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">variable<span class="token type variable"> "secret_key" </span></span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">provider<span class="token type variable"> "aws" </span></span><span class="token punctuation">{</span>
<span class="token property">access_key</span> <span class="token punctuation">=</span> var.access_key
<span class="token property">secret_key</span> <span class="token punctuation">=</span> var.secret_key
<span class="token punctuation">}</span>
</code></pre>
<h2 id="例子">例子</h2>
<pre class="language-"><code class="lang-bash">$ terraform <span class="token function">import</span> aws_instance.foo i-abcd1234
</code></pre>
<pre class="language-"><code class="lang-bash">$ terraform <span class="token function">import</span> module.foo.aws_instance.bar i-abcd1234
</code></pre>
<pre class="language-"><code class="lang-bash">$ terraform <span class="token function">import</span> <span class="token string">'aws_instance.baz[0]'</span> i-abcd1234
</code></pre>
<pre class="language-"><code class="lang-bash">$ terraform <span class="token function">import</span> <span class="token string">'aws_instance.baz["example"]'</span> i-abcd1234