This topic describes how to use Terraform to create Auto Scaling resources, such as scaling groups, scaling configurations, and scaling rules.
Prerequisites
Before you use Terraform, make sure that the following requirements are met:
- An AccessKey pair and an Alibaba Cloud account are created. You can view your AccessKey pair on the AccessKey Management page in the Alibaba Cloud Management Console. For more information about how to create an AccessKey pair, see Create an AccessKey pair.
- Terraform is installed and configured. For more information about how to install Terraform, see Install and configure Terraform in the local PC and Use Terraform in Cloud Shell.
Background information
- Terraform is an open source infrastructure-as-code tool that you can use to preview, configure, and manage cloud infrastructure and resources in a secure and efficient manner. Terraform allows you to write code to create, delete, query, and modify resources. For more information, see Terraform overview.
- Terraform provides commands that are used to manage resources, such as terraform apply and terraform show, commands that are used to manage the status of resources, and other common commands.
For more information, see Common commands for resource management.
- terraform apply: creates and modifies resources. To ensure the security of resources, manual interaction is required when the command is being run. You must manually confirm whether to continue the command.
- terraform show: displays all managed resources and their property values in the Terraform state.
Procedure
The following section describes how to use Terraform to create a scaling group, a scaling configuration, and a scaling rule. If you want to use Terraform to manage other types of Auto Scaling resources, you can repeat the following steps.
Examples
The following sample code provides an example on how to use Terraform to create a scaling group, a scaling configuration, and a scaling rule:
resource "alicloud_vpc" "vpc" {
name = "tf_test_vpc"
cidr_block = "172.16.0.0/12"
}
resource "alicloud_vswitch" "vsw" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = "172.16.0.0/21"
availability_zone = "cn-hangzhou-b"
}
resource "alicloud_security_group" "security" {
name = "tf_test_security"
vpc_id = alicloud_vpc.vpc.id
}
resource "alicloud_security_group_rule" "allow_all_tcp" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = "1/65535"
priority = 1
security_group_id = alicloud_security_group.security.id
cidr_ip = "0.0.0.0/0"
}
resource "alicloud_ess_scaling_group" "group" {
scaling_group_name="tf_test_scalinggroup"
min_size=0
max_size=100
vswitch_ids=[alicloud_vswitch.vsw.id]
}
resource "alicloud_ess_scaling_configuration" "configuration" {
scaling_group_id = alicloud_ess_scaling_group.group.id
instance_type = "ecs.hfc7.xlarge"
image_id = "aliyun_2_1903_x64_20G_alibase_20210120.vhd"
security_group_id = alicloud_security_group.security.id
scaling_configuration_name = "tf_test_scalingconfiguration"
system_disk_category = "cloud_essd"
spot_strategy = "SpotWithPriceLimit"
force_delete = true
}
resource "alicloud_ess_scaling_rule" "rule" {
scaling_group_id = alicloud_ess_scaling_group.group.id
adjustment_type = "QuantityChangeInCapacity"
adjustment_value = 1
}