10分钟内掌握Terraform
Terraform 只需要一个选项
属于基础设施定义工具类别的工具,在云端生成和操作资源,使其成为定义文件的状态。
具有可以声明性地定义基础架构的特点,可以将注意力集中在配置定义上,而不必编写构建步骤。
※可以声明地定义是指只需编写”创建一个t2.micro实例”,而无需关注创建过程即可完成创建。
如果只是简单地进行脚本化,仍需要按照步骤编写构建手续。
由HashiCorp公司开发的Vagrant是一种工具,因此使用感与Vagrant类似。
太长不看
只需将定义内容写入.tf文件中,然后运行$ terraform apply命令,即可自动按照定义内容构建基础设施。
安装
只需要按照以下步骤,从链接中下载每个操作系统的预编译二进制文件,并设置其路径即可。
https://www.terraform.io/intro/getting-started/install.html
在Mac情况下
您可以使用 Homebrew 进行安装。
$ brew update
$ brew install terraform
在AWS上的使用方法
预先准备
创建IAM用户
请向管理员申请创建一个 IAM 用户,并获取用于使用 AWS CLI 的访问密钥。
请访问以下链接:https://console.aws.amazon.com/iam/home?#/home
需要获取对所需使用的AWS服务(如EC2、S3等)的操作权限。
在本地设置AWS CLI。
请参考以下链接的内容,设置您的 AWS 命令行工具:
http://docs.aws.amazon.com/zh_cn/streams/latest/dev/kinesis-tutorial-cli-installation.html
如果您已经通过$ aws configure命令设置了身份验证信息,那么系统会自动引用这些AWS身份验证信息。
注意:AWS的身份验证信息将保存在~/.aws/credentials文件中。
创建EC2实例
创建.tf文件 .tf
设置 AWS 的认证信息。
請創建variables.tf文件並按以下方式記述所需操作的區域。
provider "aws" {
version = "~> 2.0"
region = "ap-northeast-1"
}
定义EC2的规格
resource "aws_instance" "sandbox" {
count = 2
ami = "ami-785c491f" # Ubuntu 16.04 LTS official ami
instance_type = "t2.micro"
tags = {
Name = "${format("sandbox-%02d", count.index + 1)}"
}
}
从terraform v0.12版本开始,一些语法发生了变化,现在在tags之前需要加上”=”,而在v0.11版本中是不需要的。详细信息请参考:https://www.terraform.io/upgrade-guides/0-12.html#attributes-vs-blocks
这句话代表了以下内容。
-
- 2つインスタンスを生成
-
- AMI: ami-785c491f (Ubuntu 16.04 LST)
-
- t2.micro
- Name に “sandbox-xx”
2. 检查定义内容
在存放variable.tf和ec2.tf的目录中执行以下操作。
只有在第一次执行时才执行init进行初始化。
$ terraform init
执行计划以进行定义内容的检查
$ terraform plan
2. 将执行计划的内容输出
确认定义的内容是否按照预期被反映出来
〜
+ aws_instance.sandbox.0
ami: "ami-785c491f"
〜
tags.Name: "sandbox-01"
〜
+ aws_instance.sandbox.1
ami: "ami-785c491f"
〜
tags.Name: "sandbox-02"
〜
Plan: 2 to add, 0 to change, 0 to destroy.
3. 使用定义来创建实例
在同一个目录下执行以下操作。
$ terraform apply
2. 确认输出正常
可以看到连续输出,确认已添加了2个对象
〜
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
〜
通过运行”terraform show”命令来查看。
将显示由terraform创建的对象的内容。
$ terraform show
aws_instance.sandbox.0:
id = i-02bbfece7cbf56027
〜
aws_instance.sandbox.1:
id = i-0aad82a9b81951965
〜
确认EC2控制台
可以确认已经创建了两个实例:sandbox-01和sandbox-02

4. 修改定义内容
修改当前实例的配置值
将实例名称更改为modified sandbox-xx。
〜
Name = "${format("modified sandbox-%02d", count.index + 1)}"
〜
请检查更改的内容。
$ terraform plan
〜
~ aws_instance.sandbox.0
tags.Name: "sandbox-01" => "modified sandbox-01"
〜
您可以看到标签的差异显示出来。
应用更改
$ terraform apply
〜
Apply complete! Resources: 0 added, 2 changed, 0 destroyed.
〜
使用terraform show进行确认
$ terraform show
aws_instance.sandbox.0:
〜
tags.Name = modified sandbox-01
〜
aws_instance.sandbox.1:
〜
tags.Name = modified sandbox-02
〜
标签的更改已经生效了。

更改实例的数量
将 count 更改为 1。
〜
count = 1
〜
2. 检查变更内容
$ terraform plan
〜
- aws_instance.sandbox.1
Plan: 0 to add, 0 to change, 1 to destroy.
我知道sandbox.1将被删除。
3. 应用更改
$ terraform apply
〜
Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
输出已删除沙箱1的日志。

删除由terraform定义的所有资源。
1. 运行 terraform destroy 后会出现确认提示信息,输入”yes”以开始删除操作。
$ terraform destroy
Do you really want to destroy?
Terraform will delete all your managed infrastructure.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
aws_instance.sandbox: Refreshing state... (ID: i-02bbfece7cbf56027)
aws_instance.sandbox: Destroying... (ID: i-02bbfece7cbf56027)
aws_instance.sandbox: Still destroying... (ID: i-02bbfece7cbf56027, 10s elapsed)
aws_instance.sandbox: Still destroying... (ID: i-02bbfece7cbf56027, 20s elapsed)
aws_instance.sandbox: Still destroying... (ID: i-02bbfece7cbf56027, 30s elapsed)
aws_instance.sandbox: Still destroying... (ID: i-02bbfece7cbf56027, 40s elapsed)
aws_instance.sandbox: Still destroying... (ID: i-02bbfece7cbf56027, 50s elapsed)
aws_instance.sandbox: Destruction complete
Destroy complete! Resources: 1 destroyed.
用terraform show命令进行确认
$ terraform show
你可以确认在管理之下没有任何对象存在。

我可以看到所有东西都被删除了。