All Products
Search
Document Center

Terraform:Terraform sample templates

Last Updated:Feb 27, 2026

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:

  1. Create a working directory and save the template code to a file named main.tf.

  2. 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 apply
  3. When prompted during terraform apply, type yes to 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.

Note

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.

ParameterDescription
regionThe region in which to create the resources. Default: cn-beijing.
instance_typeThe ECS instance type. Default: ecs.e-c1m1.large.
vpc_cidr_blockThe CIDR block of the VPC. Default: 172.16.0.0/16.
vsw_cidr_blockThe CIDR block of the vSwitch. Default: 172.16.0.0/24.
system_disk_categoryThe category of the system disk. Set to cloud_essd (Enterprise SSD).
image_idThe ID of the system image. Set to an Alibaba Cloud Linux image.
internet_max_bandwidth_outThe maximum outbound public bandwidth, in Mbps. Set to 10.
passwordThe 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).

Note

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.

ParameterDescription
regionThe region in which to create the resources. Default: cn-beijing.
engineThe database engine. Set to MySQL.
engine_versionThe engine version. Set to 5.6.
instance_typeThe RDS instance type. Set to rds.mysql.s2.large.
instance_storageThe storage capacity of the instance, in GB. Set to 30.
instance_charge_typeThe billing method. Set to Postpaid (pay-as-you-go).
monitoring_periodThe 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.

Note

- 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.

ParameterDescription
regionThe region in which to create the resources. Default: cn-beijing.
load_balancer_nameThe name of the SLB instance.
load_balancer_specThe specification of the SLB instance. Set to slb.s2.small.
vswitch_idThe 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 destroy

When prompted, type yes to confirm the resource deletion. Terraform destroys the resources in the correct dependency order.