1. 什么是HCL语言✅
HCL(HashiCorp Configuration Language)是一种专门为构建结构化配置格式而设计的语法和API。 他由HashiCorp公司设计。
- HCL是一个用于创建结构化配置语言的工具包,主要针对DevOps工具、服务器等。
- HCL既有一种原生语法,旨在让人类愉快地阅读和编写,也有一种基于json的变体,更容易让机器生成和解析。
- HCL包含一个表达式语法,允许基本的内联计算,并且在调用应用程序的支持下,可以使用变量和函数来进行更动态的配置语言。
2. Terraform语言✅
Terraform语言的主要目的是声明资源,这些资源表示基础设施对象。所有其他语言特性的存在只是为了使资源的定义更加灵活和方便。
Terraform配置是一个用Terraform语言编写的完整文档,它告诉Terraform如何管理给定的基础设施集合。
一个配置可以由多个文件和目录组成。
2.1 语法
https://developer.hashicorp.com/terraform/language/syntax
配置语法
Terraform
的配置文件都是以.tf
为后缀Terraform
支持两种模式HCL、JSON
yaml
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" "..." {
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
或称
<block type> "<RESOURCE TYPE>" "<LOCAL NAME/LABEL>"{
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
块类型 "块标签1/资源类型" "块标签2/本地名称/本地标签" {
# 块主体
参数 = 表达式
参数 = 表达式
...
}
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" "..." {
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
或称
<block type> "<RESOURCE TYPE>" "<LOCAL NAME/LABEL>"{
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
块类型 "块标签1/资源类型" "块标签2/本地名称/本地标签" {
# 块主体
参数 = 表达式
参数 = 表达式
...
}
block(块)是其他内容的容器,通常表示某种对象的配置,如resource。block具有块类型,可以有零个或多个标签,并且具有包含任意数量的参数和嵌套块的主体。Terraform的大部分功能都由配置文件中的顶级块控制。
官方案例
yaml
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 1.0.4"
}
}
}
variable "aws_region" {}
variable "base_cidr_block" {
description = "A /16 CIDR range definition, such as 10.1.0.0/16, that the VPC will use"
default = "10.1.0.0/16"
}
variable "availability_zones" {
description = "A list of availability zones in which to create subnets"
type = list(string)
}
provider "aws" {
region = var.aws_region
}
resource "aws_vpc" "main" {
# Referencing the base_cidr_block variable allows the network address
# to be changed without modifying the configuration.
cidr_block = var.base_cidr_block
}
resource "aws_subnet" "az" {
# Create one subnet for each given availability zone.
count = length(var.availability_zones)
# For each subnet, use one of the specified availability zones.
availability_zone = var.availability_zones[count.index]
# By referencing the aws_vpc.main object, Terraform knows that the subnet
# must be created only after the VPC is created.
vpc_id = aws_vpc.main.id
# Built-in functions and operators can be used for simple transformations of
# values, such as computing a subnet address. Here we create a /20 prefix for
# each subnet, using consecutive addresses for each availability zone,
# such as 10.1.16.0/20 .
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index+1)
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 1.0.4"
}
}
}
variable "aws_region" {}
variable "base_cidr_block" {
description = "A /16 CIDR range definition, such as 10.1.0.0/16, that the VPC will use"
default = "10.1.0.0/16"
}
variable "availability_zones" {
description = "A list of availability zones in which to create subnets"
type = list(string)
}
provider "aws" {
region = var.aws_region
}
resource "aws_vpc" "main" {
# Referencing the base_cidr_block variable allows the network address
# to be changed without modifying the configuration.
cidr_block = var.base_cidr_block
}
resource "aws_subnet" "az" {
# Create one subnet for each given availability zone.
count = length(var.availability_zones)
# For each subnet, use one of the specified availability zones.
availability_zone = var.availability_zones[count.index]
# By referencing the aws_vpc.main object, Terraform knows that the subnet
# must be created only after the VPC is created.
vpc_id = aws_vpc.main.id
# Built-in functions and operators can be used for simple transformations of
# values, such as computing a subnet address. Here we create a /20 prefix for
# each subnet, using consecutive addresses for each availability zone,
# such as 10.1.16.0/20 .
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index+1)
}