Create Auto Scaling resources by using Terraform
This topic describes how to use Terraform to create your first Auto Scaling resources, such as scaling groups, scaling configurations, and scaling rules.
You can run the sample code in this topic with a single click. Run Sample Code
Prerequisites
-
An Alibaba Cloud account has full permissions on all its resources, creating a significant security risk if the account credentials are leaked. We recommend that you use a RAM user and create an AccessKey for that user. For more information, see Create a RAM user and Create an AccessKey.
-
RAM authorization helps you manage access permissions for your cloud resources. It supports multi-user collaboration and lets you grant users only the minimum required permissions to prevent security vulnerabilities caused by excessive privileges. For more information, see Manage RAM user permissions.
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:CreateSecurityGroup", "ecs:ModifySecurityGroupPolicy", "ecs:DescribeSecurityGroups", "ecs:ListTagResources", "ecs:DeleteSecurityGroup", "ecs:DescribeSecurityGroupAttribute", "ecs:AuthorizeSecurityGroup", "ecs:RevokeSecurityGroup" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "vpc:DescribeVpcAttribute", "vpc:DescribeRouteTableList", "vpc:DescribeVSwitchAttributes", "vpc:DeleteVpc", "vpc:DeleteVSwitch", "vpc:CreateVpc", "vpc:CreateVSwitch" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "ess:CreateScalingGroup", "ess:DescribeScalingGroups", "ess:ListTagResources", "ess:DeleteScalingGroup", "ess:ModifyScalingGroup", "ess:CreateScalingConfiguration", "ess:DescribeScalingConfigurations", "ess:ModifyScalingConfiguration", "ess:CreateScalingRule", "ess:DescribeScalingRules", "ess:DeleteScalingRule" ], "Resource": "*" } ] } -
Set up a Terraform runtime environment. You can use one of the following methods:
-
Use Terraform in Terraform Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You can use and experiment with Terraform online after you log on, without needing to install it. This free method is ideal for quickly trying and debugging Terraform.
-
Use Terraform to quickly create resources: Terraform is pre-installed in Alibaba Cloud Cloud Shell and configured with your identity credentials. You can run Terraform commands directly in Cloud Shell. This method offers quick, convenient, and low-cost access to Terraform.
-
Install and configure Terraform locally: This method is suitable for scenarios with poor network connectivity or when you need a custom development environment.
-
Resources used
-
alicloud_ess_scaling_group: Creates a scaling group.
-
alicloud_ess_scaling_configuration: Creates a scaling configuration.
-
alicloud_ess_scaling_rule: Creates a scaling rule.
-
alicloud_vpc: Creates a VPC.
-
alicloud_vswitch: Creates a vSwitch.
-
alicloud_security_group: Creates a security group.
-
alicloud_security_group_rule: Creates a security group rule.
Create Auto Scaling resources
In this example, you will create Auto Scaling resources, including a scaling group, a scaling configuration, and a scaling rule.
-
Create a working directory, create a configuration file named main.tf, and then copy the following code into main.tf.
variable "region" { default = "cn-heyuan" } variable "instance_type" { default = "ecs.hfc7.xlarge" } variable "image_id" { default = "aliyun_2_1903_x64_20G_alibase_20210120.vhd" } variable "zone_id" { default = "cn-heyuan-b" } provider "alicloud" { region = var.region } # Create a VPC. resource "alicloud_vpc" "vpc" { vpc_name = "tf-test-vpc-wjt" cidr_block = "172.16.0.0/12" # Specify the private CIDR block for the VPC. } # Create a vSwitch. resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.0.0/21" # Specify the private CIDR block for the vSwitch. zone_id = var.zone_id # Select the zone for the vSwitch. vswitch_name = "tf-test-vswitch-wjt" } # Create a security group. resource "alicloud_security_group" "security" { name = "tf_test_security" description = "New security group" vpc_id = alicloud_vpc.vpc.id } # Add a security group rule that allows traffic from all IP addresses. 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" } # This example creates a scaling group with a maximum of 100 instances. resource "alicloud_ess_scaling_group" "group" { scaling_group_name = "tf_test_scalinggroup" min_size = 0 max_size = 100 vswitch_ids = [alicloud_vswitch.vsw.id] } # This example creates a scaling configuration for ECS instances. resource "alicloud_ess_scaling_configuration" "configuration" { scaling_group_id = alicloud_ess_scaling_group.group.id instance_type = var.instance_type image_id = var.image_id security_group_id = alicloud_security_group.security.id scaling_configuration_name = "tf_test_scalingconfiguration" system_disk_category = "cloud_essd" spot_strategy = "SpotWithPriceLimit" active = true force_delete = true } # Create a scaling rule. resource "alicloud_ess_scaling_rule" "rule" { scaling_group_id = alicloud_ess_scaling_group.group.id adjustment_type = "QuantityChangeInCapacity" adjustment_value = 1 } -
Run the following command to initialize the Terraform runtime environment.
terraform initThe following output indicates that the initialization is successful.
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. -
Create an execution plan and preview the changes.
terraform plan -
Run the following command to create the resources.
terraform applyWhen prompted, type
yesand press Enter. Wait for the command to finish. The following output indicates that the Auto Scaling resources are created.Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes ... alicloud_security_group_rule.allow_all_tcp: Creation complete after 0s [id=sg-f8z1mes******:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1] alicloud_vswitch.vsw: Creation complete after 3s [id=vsw-f8ztgc4******] alicloud_ess_scaling_group.group: Creating... alicloud_ess_scaling_group.group: Creation complete after 2s [id=asg-f8z9mo******] ... Apply complete! Resources: 7 added, 0 changed, 0 destroyed. -
Verify the results
Terraform show
Run the following command to view the details of the resources created by Terraform:
terraform showD:\tf>terraform show # alicloud_ess_scaling_configuration.configuration: resource "alicloud_ess_scaling_configuration" "configuration" { active = true credit_specification = null force_delete = true host_name = null id = "xxx" image_id = "aliyun_2_1903_x64_20G_alibase_20210120.vhd" image_name = null instance_name = "ESS-Instance" instance_type = "ecs.hfc7.xlarge" internet_charge_type = "PayByBandwidth" internet_max_bandwidth_in = 0 internet_max_bandwidth_out = 0 key_name = null override = false password_inherit = false resource_group_id = null role_name = null scaling_configuration_name = "tf_test_scalingconfiguration" scaling_group_id = "xxx" security_group_id = "xxx" spot_strategy = "SpotWithPriceLimit" ... system_disk_auto_snapshot_policy_id = null system_disk_category = "cloud_essd" system_disk_description = null system_disk_encrypted = false system_disk_name = null system_disk_performance_level = null system_disk_size = 0 } # alicloud_ess_scaling_group.group: resource "alicloud_ess_scaling_group" "group" { allocation_strategy = null az_balance = false default_cooldown = 300 desired_capacity = 0 group_deletion_protection = false group_type = "ECS" health_check_type = "ECS" id = "asg-xxx" launch_template_id = null launch_template_version = null max_size = 100 min_size = 0 multi_az_policy = "PRIORITY" on_demand_base_capacity = 0 on_demand_percentage_above_base_capacity = 0 removal_policies = [ "OldestScalingConfiguration", "OldestInstance", ] resource_group_id = "xxx" scaling_group_name = "tf_test_scalinggroup" scaling_policy = "release" spot_allocation_strategy = null spot_instance_remedy = false vswitch_ids = [ "vsw-f8ztgc4ad3e7ft9evy38z", ] }Auto Scaling console
Log on to the Auto Scaling console to view the scaling group that you created.
In the top navigation bar, select China (Heyuan) from the region selector. On the Scaling Groups page, find the scaling group named tf_test_scalinggroup and confirm that its status is Disabled, its type is ECS, and its maximum number of instances is 100.
Clean up resources
When you no longer need the resources created and managed by Terraform, run the following command to destroy them. For more information about terraform destroy, see Common commands.
terraform destroy
Sample code
You can run the sample code in this topic with a single click. Run Sample Code