当前位置: > 其它学习 > Terraform >

Terraform - 基础

时间:2019-01-29 16:50来源:未知 作者:IT

这里我们基于前面学习的AWS进行学习。

Terraform 是一个 IT 基础架构自动化编排工具,IaaS, 基础架构即代码。具体的说就是可以用代码来管理维护IT资源。并且在真正运行之前可以看到执行计划。由于状态保存到文件中,因此能够离线方式查看资源情况。

可以去官网查看更多详细信息:Terraform

Install

下载安装包Here,解压压缩包,为了更好的应用,把它添加环境变量PATH。

打开命令行,验证能用:

1
2
3
4
5
C:\Users\yongfei.hu>terraform --version
Terraform v0.11.7


C:\Users\yongfei.hu>

 

HashiCorp Configuration Language (HCL)

Terraform用HCL来编写,并保存在后缀有.tf的文件。

provider

在main.tf文件中

1
2
3
provider "aws" {
region = "us-east-1"
}

 

  • 用AWS做为Provider。
  • 部署你的infrastructure到US-EAST-1 Region。

resources

对每一种Provider,都有很多不同的Resources进行管理。

1
2
3
resource "PROVIDER_TYPE" "NAME" {
[CONFIG ...]
}

 

  • PROVIDER is the name of a provider
  • TYPE is the type of resources to create in that provider
  • NAME is an identifier you can use throughout the Terraform code to refer to this resource
  • CONFIG consists of one or more configuration parameters that are specific to that resource
    E.g.,
    1
    2
    3
    4
    
    resource "aws_instance" "example" {
    ami = "ami-40d28157"
    instance_type = "t2.micro"
    }
    

Run

命令行进入到main.tf的目录执行 Terraform Command。

terrafrom plan

1
2
3
4
5
6
> terraform plan
Refreshing Terraform state in-memory prior to plan...
(...)
+ aws_instance.example
ami: "ami-40dddded"
(...)

The plan command lets you see what Terraform will do before actually making
any changes。

  • resources with a plus sign (+) are going to be created
  • resources with a minus sign (–) are going to be deleted
  • resources with a tilde sign (~) are going to be modified.

    terraform apply

    这才是真正执行命令,去AWS上进行相应的操作。

    terraform graph

    To show the dependency graph


(责任编辑:IT)
------分隔线----------------------------