java系统找不到指定文件怎么解决
258
2022-09-28
Terraform 常用语法(一)
Provider
Terraform 通过 provider 管理基础设施,使用 provider 与云供应商 API进行交互;每个 Provider 都包含相关的资源和数据源;Provider
配置 Provider
关键字 provider ,一般是主体部分的配置参数。
配置多个 Provider
Resource
资源来自 Provider,是 Terraform 中最重要的元素,每个 resource 块描述一个或多个基础对象,例如网络、计算实例、或提供的更高级的组件,比如 mysql 实例,DNS 解析记录等。资源名称必须以字母或下划线开头,并且只能包含字母、数字、 下划线和破折号、
alicloud_docs
定义一台 ECS 实例:包括如下选项
~# cat alicloud_instance.tfresource "alicloud_instance" "myecs" { # 可用区 availability_zone = "cn-beijing-b" # 安全组 security_groups = alicloud_security_group.group.*.id # 实例规格 instance_type = "ecs.t5-lc2m1.nano" # 系统盘类型、名称、描述 system_disk_category = "cloud_efficiency" system_disk_name = "test_foo_system_disk_name" system_disk_description = "test_foo_system_disk_description" # 使用镜像 image_id = "centos_7_5_x64_20G_alibase_20211130.vhd" # 实例名称 instance_name = "test_foo" # Vswitch ID vswitch_id = alicloud_vswitch.vsw.id # 带宽 internet_max_bandwidth_out = 1 # 付款方式 instance_charge_type = "PaybyTraffic" # 账号密码 password = "Test1234567"}
可用区安全组实例规格系统盘类型、名称、描述镜像实例名称带宽交换机付费类型账号密码
Resource DNS
alicloud_dns_record_docs
定义一条 DNS 解析记录
~# cat alicloud_dns.tf resource "alicloud_dns_record" "record" { name = "whale.com" host_record = "test" type = "A" value = alicloud_instance.myecs.public_ip}
DataSource
使用 output 块进行展示引用
data "alicloud_images" "images_centos" { owners = "system" name_regex = "^centos_7_9" architecture = "x86_64" status = "Available" os_type = "linux" output_file = "./outputs.json"}output "first_image_id" { value = data.alicloud_images.images_centos.images}
# resource 引用image_id = data.alicloud_images.images_centos.images[0].id
Variable
变量允许自定义 Terraform 模块,而无需更改模块自己的代码。这可以实现跨不同的 Terraform 配置共享模块,使模块可组合和可重用。
在variables.tf 中定义变量;在同一个模块的所有变量中必须是唯一的;可以从环境变量或者文本文件中读取;Terraform 默认读取terraform.tfvars
variable "region" { type = string description = "region name" default = "cn-beijing" sensitive = true}
default 变量的默认值type 变量的类型describtion 变量的描述信息validation 定义变量的验证规则sensitive 限制变量在 UI 中显示nullable 变量是否可为空
variable 参数类型
anystringnumberboollistsetmapobjecttuple
map DNS
# variables.tfvariable "dns_record" { type = map(string) description = "custom dns record"}
# terraform.tfvarsdns_record = { "dev" = "dev.whale.com", "stag" = "stag.whale.com", "prod" = "prod.whale.com"}
# variables.tfvariable "env_list" { type = list(string) description = "env list"}output "env_list" { value = var.env_list}# terraform.tfvarsenv_list = [ "dev", "stag", "prod" ]
# variables.tfvariable "ecs_info" { type = object({ecs_image = string, ecs_name = string})}output "ecs_info" { value = var.ecs_info}# terraform.tfvarsecs_info = { ecs_image = "centos_7_5_64_20G_alibase_20G_20211130.vhd", ecs_name = "mydemoecs"}
环境变量terrform.tfvars |terrform.tfvars.json*.auto.tfvars |*.auto.tfvars.json命令行 -var | -var-file
# -varterraform plan -var="region=cn-hangzhou"terraform plan -var='env_list=["dev", "stag"]' -var="region=cn-hangzhou"# -var-fileterraform plan -var-file="dev.tfvars"##ENV export TF_VAR_region='cn-beijing'export TF_VAR_env_list='env_list=["dev", "stag"]'
locals-局部变量
局部、本地变量;局部值有助于避免在配置中多次重复相同的值或表达式;
# 定义局部变量locals { ecs_name = "mydemoecs" owner = "whale"}# 引用局部变量local.ecs_namelocal.owner
Output
output 可以打印已定义的变量,并且可以公开信息以供其他 Terraform 配置使用。输出值类似于编程语言中的返回值。可选参数:
description 变量的描述信息sensitive 限制变量在 UI中显示, true 不显示,false 显示,默认是 flasedepends_on 依赖关系
# output.tfoutput "dev_dns_name" { value = alicloud_dns_record.record.host_record}# 运行 terraform plan
module.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~