Step-by-Step Guide: Deploying an ECS Instance on Alibaba Cloud with Terraform
Deploying an ECS Instance in Alibaba Cloud using Terraform
Introduction
Terraform simplifies the process of managing cloud infrastructure. Here’s a detailed guide to deploying an Elastic Compute Service (ECS) instance on Alibaba Cloud using Terraform.
Terraform Configuration
1. Provider Configuration
provider "alicloud" {
region = "cn-hangzhou"
}
Explanation:
This block configures the Terraform provider for Alibaba Cloud and specifies the region (cn-hangzhou
).
2. VPC (Virtual Private Cloud)
resource "alicloud_vpc" "default" {
name = "terraform-vpc"
cidr_block = "172.16.0.0/12"
}
Explanation:
A VPC is created with the name terraform-vpc
and a CIDR block of 172.16.0.0/12
, providing a private network for your cloud resources.
3. VSwitch
resource "alicloud_vswitch" "default" {
name = "terraform-vswitch"
vpc_id = alicloud_vpc.default.id
cidr_block = "172.16.0.0/21"
zone_id = "cn-hangzhou-e"
}
Explanation:
A VSwitch, named terraform-vswitch
, is created within the VPC. The cidr_block
specifies the range of IP addresses, and zone_id
determines the availability zone.
4. Security Group
resource "alicloud_security_group" "default" {
name = "terraform-sg"
vpc_id = alicloud_vpc.default.id
}
Explanation:
A security group named terraform-sg
is created to manage inbound and outbound traffic for resources in the VPC.
5. Security Group Rule
resource "alicloud_security_group_rule" "allow_ssh" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "internet"
policy = "accept"
port_range = "22/22"
priority = 1
security_group_id = alicloud_security_group.default.id
cidr_ip = "0.0.0.0/0"
}
Explanation:
This rule allows SSH access (port 22) from any IP address (0.0.0.0/0
). The ingress
type specifies inbound traffic.
6. ECS Instance
resource "alicloud_instance" "default" {
instance_name = "terraform-ecs"
instance_type = "ecs.t5-lc2m1.nano"
security_groups = [alicloud_security_group.default.id]
vswitch_id = alicloud_vswitch.default.id
image_id = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
internet_max_bandwidth_out = 5
system_disk_category = "cloud_efficiency"
instance_charge_type = "PostPaid"
}
Explanation:
This block creates an ECS instance with the name terraform-ecs
. It uses a specific instance type (ecs.t5-lc2m1.nano
), an Ubuntu image, and is attached to the previously created VSwitch and security group. The instance has a maximum outbound internet bandwidth of 5 Mbps and uses a cost-efficient system disk. The billing method is set to PostPaid
.
Conclusion
This Terraform configuration provides a detailed setup for deploying an ECS instance on Alibaba Cloud. Adjust the parameters according to your specific requirements for a more customized deployment.
Related Article: https://clouderlabs.com/deploying-ecs-instance-in-alibaba-cloud/