我尝试了一下 Terraform
请参考
参考了前佛先生和大瀧先生的投稿,
试着使用HashiCorp的新一代编排工具Terraform在AWS上进行了简易教程。
我們來試一試
- Terraformのダウンロード・インストール
$ mkdir terraform
$ cd terraform
$ wget -O 0.1.0_linux_amd64.zip https://dl.bintray.com/mitchellh/terraform/0.1.0_linux_amd64.zip
$ unzip ./0.1.0_linux_amd64.zip
Archive: ./0.1.0_linux_amd64.zip
inflating: terraform
inflating: terraform-provider-aws
inflating: terraform-provider-consul
inflating: terraform-provider-digitalocean
inflating: terraform-provider-dnsimple
inflating: terraform-provider-heroku
inflating: terraform-provisioner-file
inflating: terraform-provisioner-local-exec
inflating: terraform-provisioner-remote-exec
- バージョン確認
$ ./terraform --version
Terraform v0.1.0
- 設定ファイルの配置
$ vi aws.tf
提供者 “aws” {
访问密钥 = “AKIxxxxxxxxxxxx”
密钥 = “xxxxxxxxxxxxxxxxxxxxxxxxxxx”
区域 = “ap-northeast-1”
}资源 “aws_instance” “test1” {
镜像 = “ami-29dc9228”
实例类型 = “t2.micro”
子网 ID = “subnet-xxxxxxxx”
}
似乎只需要指定subnet_id,就可以在VPC内创建实例。不需要指定vpc-id这一点挺方便的。请注意,密钥和子网ID需使用您自己的。
- planで計画
$ ./terraform plan
Refreshing Terraform state prior to plan...
aws_instance.example: Refreshing state... (ID: i-6a3f076c)
The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.
Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.
+ aws_instance.test1
ami: "" => "ami-29dc9228"
availability_zone: "" => "<computed>"
instance_type: "" => "t2.micro"
key_name: "" => "<computed>"
private_dns: "" => "<computed>"
private_ip: "" => "<computed>"
public_dns: "" => "<computed>"
public_ip: "" => "<computed>"
security_groups: "" => "<computed>"
subnet_id: "" => "subnet-xxxxxxxx"
- applyでインスタンス起動
$ ./terraform apply
aws_instance.example: Refreshing state... (ID: i-6a3f076c)
aws_instance.test1: Creating...
ami: "" => "ami-29dc9228"
instance_type: "" => "t2.micro"
subnet_id: "" => "subnet-xxxxxxxx"
aws_instance.example: Destruction complete
aws_instance.test1: Creation complete
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.
State path: terraform.tfstate
我們來嘗試修改設定吧。
$ vi aws.tf
尝试以 t2.small 的实例类型进行执行
提供者 “aws” {
访问密钥 = “AKIxxxxxxxxxxxx”
密钥 = “xxxxxxxxxxxxxxxxxxxxxxxxxxx”
区域 = “ap-northeast-1”
}资源 “aws_instance” “test1” {
AMI = “ami-29dc9228”
实例类型 = “t2.small”
子网ID = “subnet-xxxxxxxx”
}
- terraform plan
$ ./terraform plan
Refreshing Terraform state prior to plan...
aws_instance.test1: Refreshing state... (ID: i-04370f02)
The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.
Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.
-/+ aws_instance.test1
availability_zone: "ap-northeast-1b" => "<computed>"
instance_type: "t2.micro" => "t2.small" (forces new resource)
key_name: "" => "<computed>"
private_dns: "ip-10-0-1-78.ap-northeast-1.compute.internal" => "<computed>"
private_ip: "10.0.1.78" => "<computed>"
public_dns: "" => "<computed>"
public_ip: "" => "<computed>"
security_groups: "" => "<computed>"
subnet_id: "subnet-xxxxxxxx" => "<computed>"
- terraform apply
./terraform apply
aws_instance.test1: Refreshing state... (ID: i-04370f02)
aws_instance.test1: Destroying...
aws_instance.test1: Destruction complete
aws_instance.test1: Modifying...
instance_type: "t2.micro" => "t2.small"
aws_instance.test1: Error: Error launching source instance: The parameter groupName cannot be used with the parameter subnet (InvalidParameterCombination)
Error applying plan:
1 error(s) occurred:
* Error launching source instance: The parameter groupName cannot be used with the parameter subnet (InvalidParameterCombination)
Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
出现了一些错误。。。
由于没有办法,所以重新执行。
$ ./terraform apply
aws_instance.test1: Creating...
ami: "" => "ami-29dc9228"
instance_type: "" => "t2.small"
subnet_id: "" => "subnet-xxxxxxxx"
aws_instance.test1: Creation complete
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.
State path: terraform.tfstate
关于更改实例大小,有一种是删除实例并重新创建(这样做可能希望进行修改),另外一种是进行两次实例大小的更改,但是每次更改必定会出错。
在我试用的范围内,这个功能非常方便,可以轻松地进行休闲使用,所以我想考虑如何使用以及运营方面的问题。