首页 > 其他分享 >terraform安装与命令详解 zz

terraform安装与命令详解 zz

时间:2023-07-18 11:44:57浏览次数:35  
标签:vpc instance terraform 详解 zz root id alicloud

terraform安装与命令详解

by wanzi 2021-02-25 约 3703 字 - 预计阅读 8 分钟  devops | 阅读 92

安装Terraform

Mac系统安装

1
2
brew tap hashicorp/tap
brew install hashicorp/tap/terraform

Linux系统安装

  1. ubuntu安装
1
2
3
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
  1. centos系统
1
2
3
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform

验证安装

 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
# terraform -v
Terraform v0.14.3

Your version of Terraform is out of date! The latest version
is 0.14.7. You can update by downloading from https://www.terraform.io/downloads.html
# terraform
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure

All other commands:
  console       Try Terraform expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote Terraform modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a Terraform resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current Terraform version
  workspace     Workspace management

Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.

terraform命令之资源管理

资源初始化

对于一个terraform资源项目,我这里创建了3个基本文件,分别为:main.tf(入口文件),variables.tf(变量信息),versions.tf(版本信息)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# ls 
main.tf     variables.tf      versions.tf
# terraform init

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of aliyun/alicloud from the dependency lock file
- Using aliyun/alicloud v1.115.1 from the shared cache directory

Terraform has been successfully initialized!

格式化terraform文件

fmt默认会回格式化处理当前目录下.tf文件,并格式为标准的tf格式。

 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
# terraform fmt 
main.tf
variables.tf
versions.tf
# terraform fmt -diff  #格式化处理
main.tf
--- old/main.tf
+++ new/main.tf
@@ -1,7 +1,7 @@
 provider "alicloud" {
   region     = var.region
   access_key = var.alicloud_access_key
-  secret_key =  var.alicloud_secret_key
+  secret_key = var.alicloud_secret_key
 }

 resource "alicloud_vpc" "vpc" {
@@ -12,7 +12,7 @@
 resource "alicloud_vswitch" "vsw" {
   vpc_id            = alicloud_vpc.vpc.id
   cidr_block        = "10.100.0.0/24"
-  availability_zone =  var.availability_zone
+  availability_zone = var.availability_zone
 }

 resource "alicloud_security_group" "default" {
variables.tf
--- old/variables.tf
+++ new/variables.tf
@@ -4,7 +4,7 @@
 }

 variable "alicloud_secret_key" {
-  default                     = "4Z4gbl3d9TGz9jWobv9MPwInvyH2Kf"
+  default     = "4Z4gbl3d9TGz9jWobv9MPwInvyH2Kf"
   description = "The Alicloud Access Secret Key to launch resources.  Support to environment 'ALICLOUD_SECRET_KEY'."
 }

创建资源计划

terraform plan 会检查一组更改的执行计划是否符合您的期望,而不会更改实际资源或状态。

 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
# terraform  plan

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # alicloud_instance.wanzi_test will be created
  + resource "alicloud_instance" "wanzi_test" {
      + availability_zone             = "cn-hangzhou-i"
      + credit_specification          = (known after apply)
      + deletion_protection           = false
      + dry_run                       = false
      + host_name                     = (known after apply)
      + id                            = (known after apply)
      + image_id                      = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
      + instance_charge_type          = "PostPaid"
      + instance_name                 = "wanzi_tf001"
      + instance_type                 = "ecs.s6-c1m2.small"
      + internet_max_bandwidth_in     = (known after apply)
      + internet_max_bandwidth_out    = 0
      + key_name                      = (known after apply)
      + password                      = (sensitive value)
      + private_ip                    = (known after apply)
      + public_ip                     = (known after apply)
      + role_name                     = (known after apply)
      + security_groups               = (known after apply)
      + spot_strategy                 = "NoSpot"
      + status                        = "Running"
      + subnet_id                     = (known after apply)
      + system_disk_category          = "cloud_efficiency"
      + system_disk_performance_level = (known after apply)
      + system_disk_size              = 40
      + volume_tags                   = (known after apply)
      + vswitch_id                    = (known after apply)
    }

创建云资源

terraform apply 会自动生成一个资源创建计划,并批准执行该计划,同时在当前目录下会生成tfstate文件

 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
# terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # alicloud_instance.wanzi_test will be created
  + resource "alicloud_instance" "wanzi_test" {
      + availability_zone             = "cn-hangzhou-i"
      + credit_specification          = (known after apply)
      + deletion_protection           = false
      + dry_run                       = false
      + host_name                     = (known after apply)
      + id                            = (known after apply)
      + image_id                      = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
      + instance_charge_type          = "PostPaid"
      + instance_name                 = "wanzi_tf001"
      + instance_type                 = "ecs.s6-c1m2.small"
      + internet_max_bandwidth_in     = (known after apply)
      + internet_max_bandwidth_out    = 0
      + key_name                      = (known after apply)
      + password                      = (sensitive value)
      + private_ip                    = (known after apply)
      + public_ip                     = (known after apply)
      + role_name                     = (known after apply)
      + security_groups               = (known after apply)
      + spot_strategy                 = "NoSpot"
      + status                        = "Running"
      + subnet_id                     = (known after apply)
      + system_disk_category          = "cloud_efficiency"
      + system_disk_performance_level = (known after apply)
      + system_disk_size              = 40
      + volume_tags                   = (known after apply)
      + vswitch_id                    = (known after apply)
    }

  # alicloud_security_group.default will be created
  + resource "alicloud_security_group" "default" {
      + id                  = (known after apply)
      + inner_access        = (known after apply)
      + inner_access_policy = (known after apply)
      + name                = "default"
      + security_group_type = "normal"
      + vpc_id              = (known after apply)
    }
......
......
Plan: 5 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

alicloud_vpc.vpc: Creating...
alicloud_vpc.vpc: Creation complete after 9s [id=vpc-bp1kulcyygsi727aay4hd]
alicloud_security_group.default: Creating...
alicloud_vswitch.vsw: Creating...
alicloud_security_group.default: Creation complete after 1s [id=sg-bp11s5pka9pxtj6pn4xq]
alicloud_security_group_rule.allow_all_tcp: Creating...
alicloud_security_group_rule.allow_all_tcp: Creation complete after 1s [id=sg-bp11s5pka9pxtj6pn4xq:ingress:tcp:1/65535:intranet:0.0.0.0/0

标签:vpc,instance,terraform,详解,zz,root,id,alicloud
From: https://www.cnblogs.com/zafu/p/17562469.html

相关文章

  • Proj. CMI Paper Reading: Distributed System Fuzzing
    Abstract背景:当前分布式系统分析一般都是黑盒工具,难以探索程序状态工具:MALLORY任务:greyboxfuzzingtestingdistributedsystem方法:timeline-driventesting,timelineabstraction步骤:动态构建描述系统行为的Lamporttimelines将这些timelines抽象化为happens-beforesu......
  • 大语言模型的预训练[1]:基本概念原理、神经网络的语言模型、Transformer模型原理详解
    大语言模型的预训练[1]:基本概念原理、神经网络的语言模型、Transformer模型原理详解、Bert模型原理介绍1.大语言模型的预训练1.LLM预训练的基本概念预训练属于迁移学习的范畴。现有的神经网络在进行训练时,一般基于反向传播(BackPropagation,BP)算法,先对网络中的参数进行随机初始......
  • 线性基详解
    线性基详解线性基主要用来解决异或问题线性基的性质原序列的任何一个数都可以由线性基中的若干个元素异或得到线性基中的任何元素互相异或,不可异或出0我们考虑到异或的性质:交换律:如果a1^a2^a3=a4那么a2^a1^a3=a4很容易证明a1^a2=a3则a1^a3......
  • Java方法详解
    Java方法详解方法的定义Java方法是语句的集合,它们在一起执行一个功能方法是解决一类问题的步骤的有序结合方法包含于类或对象中方法在程序中被创建,在其他地方被引用publicclassDemo01{//main方法publicstaticvoidmain(String[]args){intsum......
  • Linux磁盘专题-linux文件系统详解
    这可是我几年前的杰作笔记呀.....当初手写计算都会,现在忘光光....物理硬盘Block的概念和作用硬盘底层一次IO就是读、写一次扇区,一个扇区默认是512Byte。读写大量文件如果以扇区为单位会很慢、性能不好,所以出现了逻辑块的概念(logicblock),也就是硬盘Block。逻辑块Block是......
  • 详解prettier使用以及与主流IDE的配合
    很多前端小伙伴在日常使用prettier的时候都或多或少有一点疑惑,prettier在每一个IDE中究竟是怎样工作起来的,为什么配置有时候生效,有时又毫无效果。为了让我们的前端小伙伴更加熟悉这块,本文将对prettier在主流IDE中的使用过程一探究竟。prettier是什么在介绍prettier如何集成到IDE......
  • 带你掌握利用Terraform不同数据源扩展应用场景
    本文分享自华为云社区《利用Terraform不同数据源扩展应用场景》,作者:kaliarch。一背景在生产环境中使用Terraform进行基础设施编排,通常又一些信息是通过其他外部系统传入,该场数据源为一个接口,需要Terraform具备调用远程接口能力,获取数据进行目标资源编排,处理各种云厂商提供的p......
  • 详解C#开发Android应用程序的流程
    Android系统一下子铺天盖地而来,让人目不暇接。兴奋的同时也让部分开发人员犯难了!要知道从熟知的Wince、Mobile开发语言C#跨越到RFID-Android的Java。可不是一朝一夕就能完成的。就好比你的乾坤大挪移已经第七层了,却忽然要你从易筋经从头练起,真是愁煞人也!难道微软的开发环境和谷歌......
  • 【后端面经-Java】JVM内存分区详解
    @目录1.JVM内存分区简介2.JVM栈3.JVM堆4.JVM方法区5.JVM内存分配实例面试模拟参考资料1.JVM内存分区简介JVM内存分区如图所示:主要有如下几个区域:栈(Stack)堆(Heap)方法区(MethodArea)程序计数器(PC)本地方法栈(NativeMethodStack)其中,程序计数器用于存储线程当前执行的......
  • 人工智能自然语言处理:N-gram和TF-IDF模型详解
    人工智能自然语言处理:N-gram和TF-IDF模型详解1.N-gram模型N-Gram是一种基于统计语言模型的算法。它的基本思想是将文本里面的内容按照字节进行大小为N的滑动窗口操作,形成了长度是N的字节片段序列。每一个字节片段称为gram,对所有gram的出现频度进行统计,并且按照事先设......