terraform基本使用

安装terraform

下载terraform最新版本:

wget https://releases.hashicorp.com/terraform/0.11.5/terraform_0.11.5_linux_amd64.zip

terraform是一个二进制文件,将其放入环境变量目录中即可:

unzip terraform_0.11.5_linux_amd64.zip
cp terraform /usr/local/bin

想验证安装成功?执行terraform命令即可:

[root@ip-172-31-42-166 data]# terraform
Usage: terraform [--version] [--help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

命令执行成功,安装正确。

设置aws凭证

terraform支持多种云平台,我们这里使用aws为例演示,terraform会远程调用aws的API进行工作,我们通过环境变量方式设置其凭证:

export AWS_ACCESS_KEY_ID=(your access key id)
export AWS_SECRET_ACCESS_KEY=(your secret access key)

把相应信息替换为你自己的即可。

注意:咱们这样设置的环境变量是临时生效的,你切换了shell,或者重启电脑就得重新设置了,当然也可以使用其它的方式来做凭证:

你把认证信息写在$ HOME / .aws / credentials文件中也是可以的,细节请自己查阅aws官方文档(Command Line Interface)。

下面我们就可以做些什么了,比如启台服务器玩玩。

启动一台服务器

terraform的主要操作就是写配置文件,它的配置文件以 .tf结尾,并且有它自己的专用语言来书写(HCL),并不难。

配置文件写多了难免乱,我们应该有合适的文本编辑器,很多主流的工具支持它的专用语言语法,你比如说sublime。

好了我们来书写第一个.tf文件吧:

mkdir terraform_workspace
cd terraform_workspace/
touch main.tf

向main.tf中写入文件:

provider "aws" {
  region = "us-west-2"
}

上面代码意思就是我们使用的是aws云服务,然后要在us-west-2区进行操作。

再来一段:

resource "aws_instance" "example" {
  ami           = "ami-0031f978"
  instance_type = "t2.micro"
}

上面这段意思就是用什么类型的实例,基于什么镜像。

如果熟悉aws,上面这些术语就没毛问题了。

行了,代码写完了,咱进工作区执行个命令试试好使不:

cd terraform_workspace/
terraform init
terraform plan

[root@ip-172-31-42-166 terraform_workspace]# terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

------------------------------------------------------------------------

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:

  + aws_instance.example
      id:                           <computed>
      ami:                          "ami-0031f978"
      associate_public_ip_address:  <computed>
      availability_zone:            <computed>
      ebs_block_device.#:           <computed>
      ephemeral_block_device.#:     <computed>
      get_password_data:            "false"
      instance_state:               <computed>
      instance_type:                "t2.micro"
      ipv6_address_count:           <computed>
      ipv6_addresses.#:             <computed>
      key_name:                     <computed>
      network_interface.#:          <computed>
      network_interface_id:         <computed>
      password_data:                <computed>
      placement_group:              <computed>
      primary_network_interface_id: <computed>
      private_dns:                  <computed>
      private_ip:                   <computed>
      public_dns:                   <computed>
      public_ip:                    <computed>
      root_block_device.#:          <computed>
      security_groups.#:            <computed>
      source_dest_check:            "true"
      subnet_id:                    <computed>
      tenancy:                      <computed>
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

terraform plan的意思就是提前检查一下你的配置文件要干啥,但是并不真正去执行。

执行terraform apply就可以真正去执行了,执行以后上控制台看看,主机已经启动了。

如果我们想更改刚才已经建立的实例配置怎么办?直接修改配置文件,重新执行即可,terraform是默认有记忆功能的奥。

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0031f978"
  instance_type = "t2.micro"
  tags {
    Name = "terraform-example"
  }
}

我们给配置文件里加个tags选项,再执行terraform apply。

看,给我们的机器加了一个名字

terraform基本使用

先这样吧,后续更新更深入的。

上一篇:BITED数学建模七日谈之五:怎样问数学模型问题


下一篇:值得推荐的android开源框架