本文為您提供常見的Terraform模板樣本,包括建立ECS執行個體、RDS執行個體和SLB執行個體。
建立ECS執行個體
以下模板將建立Virtual Private Cloud、交換器vSwitch、安全性群組SecurityGroup,以及ECS執行個體。
說明
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
variable "region"{
default = "cn-beijing"
}
provider "alicloud"{
region = var.region
}
variable "instance_type" {
type = string
default = "ecs.e-c1m1.large"
}
data "alicloud_zones" "default" {
available_instance_type = var.instance_type
available_resource_creation = "VSwitch"
available_disk_category = "cloud_essd"
}
variable "vpc_cidr_block" {
default = "172.16.0.0/16"
}
variable "vsw_cidr_block" {
default = "172.16.0.0/24"
}
resource "random_integer" "default" {
min = 10000
max = 99999
}
resource "alicloud_vpc" "vpc" {
vpc_name = "vpc-test_${random_integer.default.result}"
cidr_block = var.vpc_cidr_block
}
resource "alicloud_security_group" "group" {
name = "test_${random_integer.default.result}"
vpc_id = alicloud_vpc.vpc.id
}
resource "alicloud_vswitch" "vswitch" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = var.vsw_cidr_block
zone_id = data.alicloud_zones.default.zones[0].id
vswitch_name = "vswitch-test-${random_integer.default.result}"
}
resource "alicloud_instance" "instance" {
availability_zone = data.alicloud_zones.default.zones[0].id
security_groups = alicloud_security_group.group.*.id
instance_type = var.instance_type
system_disk_category = "cloud_essd"
system_disk_name = "test_foo_system_disk_${random_integer.default.result}"
system_disk_description = "test_foo_system_disk_description"
image_id = "aliyun_2_1903_x64_20G_alibase_20240628.vhd"
instance_name = "test_ecs_${random_integer.default.result}"
vswitch_id = alicloud_vswitch.vswitch.id
internet_max_bandwidth_out = 10
password = "Terraform@Example"
}
建立RDS執行個體
以下模板將建立Virtual Private Cloud、交換器vSwitch,以及RDS執行個體。
說明
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
variable "region" {
default = "cn-beijing"
}
provider "alicloud" {
region = var.region
}
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
available_disk_category = "cloud_essd"
}
variable "vpc_cidr_block" {
default = "172.16.0.0/16"
}
variable "vsw_cidr_block" {
default = "172.16.0.0/24"
}
resource "random_integer" "default" {
min = 10000
max = 99999
}
resource "alicloud_vpc" "vpc" {
vpc_name = "vpc-test_${random_integer.default.result}"
cidr_block = var.vpc_cidr_block
}
resource "alicloud_vswitch" "vswitch" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = var.vsw_cidr_block
zone_id = data.alicloud_zones.default.zones[0].id
vswitch_name = "vswitch-test-${random_integer.default.result}"
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "5.6"
instance_type = "rds.mysql.s2.large"
instance_storage = "30"
instance_charge_type = "Postpaid"
instance_name = "my_db_${random_integer.default.result}"
vswitch_id = alicloud_vswitch.vswitch.id
monitoring_period = "60"
}
建立SLB執行個體
以下模板將建立Virtual Private Cloud、交換器vSwitch,以及SLB執行個體。
說明
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
variable "region" {
default = "cn-beijing"
}
provider "alicloud" {
region = var.region
}
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
available_disk_category = "cloud_essd"
}
variable "vpc_cidr_block" {
default = "172.16.0.0/16"
}
variable "vsw_cidr_block" {
default = "172.16.0.0/24"
}
resource "random_integer" "default" {
min = 10000
max = 99999
}
resource "alicloud_vpc" "vpc" {
vpc_name = "vpc-test_${random_integer.default.result}"
cidr_block = var.vpc_cidr_block
}
resource "alicloud_vswitch" "vswitch" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = var.vsw_cidr_block
zone_id = data.alicloud_zones.default.zones[0].id
vswitch_name = "vswitch-test-${random_integer.default.result}"
}
resource "alicloud_slb_load_balancer" "default" {
load_balancer_name = "my_slb_${random_integer.default.result}"
load_balancer_spec = "slb.s2.small"
vswitch_id = alicloud_vswitch.vswitch.id
}