This topic provides examples of commonly used Terraform templates for provisioning Alibaba Cloud resources. It includes three complete, ready-to-use templates that create the following resources:
An Elastic Compute Service (ECS) instance with networking and security configuration
An ApsaraDB RDS instance with a MySQL database engine
A Server Load Balancer (SLB) instance, also known as Classic Load Balancer (CLB), in a VPC
Each template is self-contained: you can copy the code into a .tf file, run it with Terraform, and have working infrastructure in minutes.
Prerequisites
Before you use the templates in this topic, make sure that the following requirements are met:
Terraform is installed. For more information, see Using Terraform locally.
An Alibaba Cloud account is created, and an AccessKey pair is configured for the Alibaba Cloud provider. For more information, see Create an AccessKey pair.
The AccessKey pair is set as environment variables or configured in the provider block. For more information, see Using Terraform locally.
How to use the templates
To deploy any of the templates in this topic, perform the following steps:
Create a working directory and save the template code to a file named
main.tf.Open a terminal, navigate to the directory, and run the following commands:
# Download the required provider plugins terraform init # Preview the resources that Terraform will create terraform plan # Create the resources terraform applyWhen prompted during
terraform apply, typeyesto confirm.
Create an ECS instance
The following template creates a virtual private cloud (VPC), a vSwitch, a security group, and an ECS instance.
The template uses the alicloud_zones data source to automatically select a zone that supports the specified instance type and disk category. A random integer is appended to resource names to ensure uniqueness.
You can run the sample code with a few clicks on the Code tab.
Template code
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"
}Key parameters
The following table describes the key parameters in this template.
| Parameter | Description |
|---|---|
region | The region in which to create the resources. Default: cn-beijing. |
instance_type | The ECS instance type. Default: ecs.e-c1m1.large. |
vpc_cidr_block | The CIDR block of the VPC. Default: 172.16.0.0/16. |
vsw_cidr_block | The CIDR block of the vSwitch. Default: 172.16.0.0/24. |
system_disk_category | The category of the system disk. Set to cloud_essd (Enterprise SSD). |
image_id | The ID of the system image. Set to an Alibaba Cloud Linux image. |
internet_max_bandwidth_out | The maximum outbound public bandwidth, in Mbps. Set to 10. |
password | The logon password of the ECS instance. |
Create an ApsaraDB RDS instance
The following template creates a VPC, a vSwitch, and an ApsaraDB RDS instance that runs MySQL.
The RDS instance uses pay-as-you-go billing (the Postpaid charge type in the Terraform provider).
You can run the sample code with a few clicks on the Code tab.
Template code
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"
}Key parameters
The following table describes the key parameters in this template.
| Parameter | Description |
|---|---|
region | The region in which to create the resources. Default: cn-beijing. |
engine | The database engine. Set to MySQL. |
engine_version | The engine version. Set to 5.6. |
instance_type | The RDS instance type. Set to rds.mysql.s2.large. |
instance_storage | The storage capacity of the instance, in GB. Set to 30. |
instance_charge_type | The billing method. Set to Postpaid (pay-as-you-go). |
monitoring_period | The monitoring data collection interval, in seconds. Set to 60. |
Create an SLB instance
The following template creates a VPC, a vSwitch, and an SLB instance. The SLB instance is created inside the VPC by specifying a vSwitch.
- Server Load Balancer (SLB) is also known as Classic Load Balancer (CLB) in current Alibaba Cloud documentation. The Terraform resource name alicloud_slb_load_balancer retains the legacy SLB naming. - You can run the sample code with a few clicks on the Code tab.
Template code
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
}Key parameters
The following table describes the key parameters in this template.
| Parameter | Description |
|---|---|
region | The region in which to create the resources. Default: cn-beijing. |
load_balancer_name | The name of the SLB instance. |
load_balancer_spec | The specification of the SLB instance. Set to slb.s2.small. |
vswitch_id | The ID of the vSwitch. This places the SLB instance inside the VPC. |
Clean up resources
After you finish testing a template, run the following command to release all resources that Terraform created. This prevents unexpected charges on your account.
terraform destroyWhen prompted, type yes to confirm the resource deletion. Terraform destroys the resources in the correct dependency order.