Terraform 的特点:
Terraform 工具处在什么位置:
local-exec
remote-exec
# 自动化
terraform init -input=false to initialize the working directory.
terraform plan -out=tfplan -input=false to create a plan and save it to the local file tfplan.
terraform apply -input=false tfplan to apply the plan stored in the file tfplan.variable
定义变量,使用 -var
/-var-file
/TF_VAR_name
/UI-Input 给变量赋值output
定义输出module
组织 tf 文件schema.Provider
type describes the provider's properties:schema.Resource
: resource_xxx
一个provider例子的执行流程
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
variable "image_id" {
type = string
}
provider "aws" {alias="west"}
引用:provider = aws.west
variable xxx {type=xx, default=xx, description=xx}
, 引用:var.<NAME>
-var
-var-file=
TF_VAR_xxx
output "xx" { value=xxx, description=xx, sensitive=t/f, depends_on}
, 引用:module.<MODULE NAME>.<OUTPUT NAME>
locals { xx1=yy1, xx2=yy2 }
, 引用 local.xxx
module xxx { source=xx, version=xx, providers=xx, xx1=yy1, xx2=yy2 }
, 其中 source, version, providers 为 meta-arguments 其他为 输入变量module.<MODULE NAME>.<OUTPUT NAME>
data resource
, 声明 data "aws_ami" "example" {}
, data "local_file" "foo" { filename = "${path.module}/foo.bar"}
, data "template_file" xx {}
resource "null_resource" "cluster"
provisioner "file" {
source = "conf/myapp.conf"
destination = "/etc/myapp.conf"
connection {
type = "ssh"
user = "root"
password = "${var.root_password}"
host = "${var.host}"
}
}
resource "aws_instance" "web" {
# ...
provisioner "local-exec" {
command = "echo ${aws_instance.web.private_ip} >> private_ips.txt"
}
}
resource "aws_instance" "web" {
# ...
provisioner "remote-exec" {
inline = [
"puppet apply",
"consul join ${aws_instance.web.private_ip}",
]
}
}
几个主要的操作
terraform.Context
执行,生成一个 terraform.Graph
, 这时候的 graph builder 是一个 PlanGraphBuilder
GraphTransformer
组成,比如 ConfigTransformer
创建配置中的 Resource, LocalTransformer
add local values, OutputTransformer
增加输出terraform.Graph
: walkOperation 为 walkPlan
, walk 操作会有多个 goroutine (vertex两倍数量) 并发执行(考虑依赖关系)原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。