Resource是Teraform中最重要的元素,通过关键字"resource"进行声明。
每个云服务都有一个或多个资源与之关联,例如volcengine_ecs_instance表示云服务器,volcengine_eip_address表示公网IP,使用volcengine_eip_associate可以为云服务器分配公网IP。示例如下:
resource "volcengine_ecs_instance" "foo" { ... } resource "volcengine_eip_address" "foo" { ... } resource "volcengine_eip_associate" "foo" { allocation_id = volcengine_eip_address.foo.id instance_id = volcengine_ecs_instance.foo.id instance_type = "EcsInstance" }
引用资源的属性时,格式为:<资源类型>.<名称>.<属性>。例如引用名为"foo"的volcengine_ecs_instance资源时,示例如下:
# 实例ID > volcengine_ecs_instance.foo.id i-mizl7m1kqccg5smt1bdp**** # 实例的第一个网卡的安全组 > volcengine_ecs_instance.foo.network_interfaces[0].security_group_ids ["sg-13f5gejti0pvk3n6nu503****"] # 实例第一个网卡的IP地址 > volcengine_ecs_instance.foo.network_interfaces[0].primary_ip_address 172.XX.XX.20 # 实例所有网卡的IP地址 > volcengine_ecs_instance.foo.network_interfaces[*].primary_ip_address ["172.XX.XX.20", "172.XX.XX.20"]