Skip to content

Commit 9b36d9f

Browse files
authored
Merge pull request #1 from xiaozhu36/master
add new modules
2 parents 87bb41e + 87ab175 commit 9b36d9f

File tree

5 files changed

+405
-0
lines changed

5 files changed

+405
-0
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# used for testing
2+
*.tfvars
3+
4+
# Compiled files
5+
*.tfstate
6+
*.tfstate.backup
7+
8+
# Module directory
9+
.terraform/
10+
11+
# terraform log
12+
*.log
13+
14+
# auto-generated key pair file
15+
*.pem
16+
17+
# tools files
18+
.DS_Store
19+
.idea
20+
21+
# others
22+
*.bak
23+
*.bk
24+

README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
Alicloud ECS Instance Terraform Module In VPC
2+
terraform-alicloud-ecs-instance
3+
=====================================================================
4+
5+
A terraform module to provide ECS instances in Alicloud VPC.
6+
7+
- It assumes you have one VPC and VSwitch and you want to put the new instances to the VPC. If not, you can launch a new one by module [terraform-alicloud-vpc](https://github.com/alibaba/terraform-alicloud-vpc)
8+
- It assumes you have several security groups in the VPC and you want to join the new instances into them. If not, you can launch one or more groups by module [terraform-alicloud-security-group](https://github.com/alibaba/terraform-alicloud-security-group)
9+
- If you have no idea some parametes, such as instance type, availability zone and image id,
10+
the module will provide default values according to some input parameters, such as `image_name_regex`, `cpu_core_count`, `memory_size` and so on.
11+
12+
13+
Module Input Variables
14+
----------------------
15+
16+
The module aim to create one or more instances and disks in the VPC. Its input variables contains VSwitch, Security Group, ECS Disks and ECS Instances.
17+
18+
#### Common Input vairables
19+
20+
- `alicloud_access_key` - The Alicloud Access Key ID to launch resources
21+
- `alicloud_secret_key` - The Alicloud Access Secret Key to launch resources
22+
- `region` - The region to launch resources
23+
- `availability_zone` - The availability zone ID to launch ECS Instances and ECS Disks - default to a zone ID retrieved by zones' data source
24+
- `number_format` - The number format used to mark multiple resources - default to "%02d"
25+
26+
`Note`: If you specify the `vswitch_id`, the `availability_zone` would be ignore when launching ECS instances.
27+
#### Instance Types Data Source Input Variables
28+
29+
- `image_name_regex` - The ECS image's name regex used to fetch latest specified system images - default to "^ubuntu_14.*_64" and it would return a latest 64bit Ubuntu Image
30+
31+
### Instance typs Variables
32+
33+
- `cpu_core_count` - CPU core count used to fetch instance types - default to 1
34+
- `memory_size` - Memory size used to fetch instance types - default to 2
35+
36+
#### VSwitch Input Variables
37+
38+
- `vswitch_id` - VSwitch ID to launch new ECS instances
39+
40+
#### Security Groups Input Variables
41+
42+
- `group_ids` - List of Security Group IDs to launch new ECS instances
43+
44+
#### ECS Disk Input Variables
45+
46+
- `number_of_disks` - The number disks you want to launch - default to 0
47+
- `disk_name` - ECS disk name to mark data disk(s) - default to "TF_ECS_Disk"
48+
- `disk_category` - ECS disk category to launch data disk(s) - choices to ["cloud_ssd", "cloud_efficiency"] - default to "cloud_efficiency"
49+
- `disk_size` - ECS disk size to launch data disk(s) - default to 40
50+
- `disk_tags` - A map for setting ECS disk tags - default to
51+
52+
disk_tags = {
53+
created_by = "Terraform"
54+
created_from = "module-tf-alicloud-ecs-instance"
55+
}
56+
57+
#### ECS Instance Input Variables
58+
59+
- `number_of_instances` - The number of instances you want to launch - default to 1
60+
- `image_id` - The image id to use - default to an Ubuntu-64bit image ID retrieved by images' data source
61+
- `instance_type` - The ECS instance type, e.g. ecs.n4.small, - default to a 1Core 2GB instance type retrieved by instance_types' data source
62+
- `instance_name` - ECS instance name to mark instance(s) - default to "TF_ECS_Instance"
63+
- `host_name` - ECS instance host name to configure instance(s) - default to "TF_ECS_Host_Name"
64+
- `system_category` - ECS disk category to launch system disk - choices to ["cloud_ssd", "cloud_efficiency"] - default to "cloud_efficiency"
65+
- `system_size` - ECS disk size to launch system disk - default to 40
66+
- `allocate_public_ip` - Whether to allocate public for instance(s) - default to true
67+
- `internet_charge_type` - The internet charge type for setting instance network - choices["PayByTraffic", "PayByBandwidth"] - default to "PayByTraffic"
68+
- `internet_max_bandwidth_out` - The max out bandwidth for setting instance network - default to 10
69+
- `instance_charge_type` - The instance charge type - choices to ["PrePaid", "PostPaid"] - default to "PostPaid"
70+
- `period` - The instance charge period when instance charge type is 'PrePaid' - default to 1
71+
- `key_name` - The instance key pair name for SSH keys
72+
- `password` - The instance password
73+
- `instance_tags` - A map for setting ECS Instance tags - default to
74+
75+
instance_tags = {
76+
created_by = "Terraform"
77+
created_from = "module-tf-alicloud-ecs-instance"
78+
}
79+
80+
81+
Usage
82+
-----
83+
You can use this in your terraform template with the following steps.
84+
85+
1. Adding a module resource to your template, e.g. main.tf
86+
87+
module "tf-instances" {
88+
source = "github.com/terraform-community-modules/terraform-alicloud-ecs-instance"
89+
90+
alicloud_access_key = "${var.alicloud_access_key}"
91+
alicloud_secret_key = "${var.alicloud_secret_key}"
92+
region = "${var.region}"
93+
94+
vswitch_id = "${var.vswitch_id}"
95+
group_ids = "${var.group_ids}"
96+
97+
disk_category = "cloud_ssd"
98+
disk_name = "my_module_disk"
99+
disk_size = "50"
100+
number_of_disks = 2
101+
102+
instance_name = "my_module_instances"
103+
host_name = "my_host"
104+
internet_charge_type = "PayByTraffic"
105+
number_of_instances = "2"
106+
107+
key_name = "${var.key_name}"
108+
109+
}
110+
111+
2. Setting values for the following variables, either through terraform.tfvars or environment variables or -var arguments on the CLI
112+
113+
- alicloud_access_key
114+
- alicloud_secret_key
115+
- region
116+
- key_name
117+
- vswitch_id
118+
- group_ids
119+
120+
Module Output Variables
121+
-----------------------
122+
123+
- instance_ids - List of new instance ids
124+
- disk_ids - List of new data disk ids
125+
126+
Authors
127+
-------
128+
Created and maintained by He Guimin(@xiaozhu36, [email protected])

main.tf

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Provider specific configs
2+
provider "alicloud" {
3+
access_key = "${var.alicloud_access_key}"
4+
secret_key = "${var.alicloud_secret_key}"
5+
region = "${var.region}"
6+
}
7+
8+
// Images data source for image_id
9+
data "alicloud_images" "default" {
10+
most_recent = true
11+
owners = "system"
12+
name_regex = "${var.image_name_regex}"
13+
}
14+
15+
// Instance_types data source for instance_type
16+
data "alicloud_instance_types" "default" {
17+
cpu_core_count = "${var.cpu_core_count}"
18+
memory_size = "${var.memory_size}"
19+
}
20+
21+
// Zones data source for availability_zone
22+
data "alicloud_zones" "default" {
23+
available_disk_category = "${var.disk_category}"
24+
available_instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}"
25+
}
26+
27+
// ECS Instance Resource for Module
28+
resource "alicloud_instance" "instances" {
29+
count = "${var.number_of_instances}"
30+
31+
image_id = "${var.image_id == "" ? data.alicloud_images.default.images.0.id : var.image_id }"
32+
availability_zone = "${var.vswitch_id != "" ? "" : var.availability_zone == "" ? data.alicloud_zones.default.zones.0.id : var.availability_zone}"
33+
instance_type = "${var.instance_type == "" ? data.alicloud_instance_types.default.instance_types.0.id : var.instance_type}"
34+
security_groups = ["${var.group_ids}"]
35+
36+
instance_name = "${var.number_of_instances < 2 ? var.instance_name : format("%s-%s", var.instance_name, format(var.number_format, count.index+1))}"
37+
host_name = "${var.number_of_instances < 2 ? var.host_name : format("%s-%s", var.host_name, format(var.number_format, count.index+1))}"
38+
39+
internet_charge_type = "${var.internet_charge_type}"
40+
internet_max_bandwidth_out = "${var.internet_max_bandwidth_out}"
41+
42+
allocate_public_ip = "${var.allocate_public_ip}"
43+
44+
instance_charge_type = "${var.instance_charge_type}"
45+
system_disk_category = "${var.system_category}"
46+
system_disk_size = "${var.system_size}"
47+
48+
password = "${var.password}"
49+
50+
vswitch_id = "${var.vswitch_id}"
51+
52+
period = "${var.period}"
53+
54+
tags {
55+
created_by = "${lookup(var.instance_tags, "created_by")}"
56+
created_from = "${lookup(var.instance_tags, "created_from")}"
57+
}
58+
}
59+
60+
// ECS Disk Resource for Module
61+
resource "alicloud_disk" "disks" {
62+
count = "${var.number_of_disks}"
63+
64+
availability_zone = "${var.availability_zone == "" ? data.alicloud_zones.default.zones.0.id : var.availability_zone}"
65+
name = "${var.number_of_disks < 2 ? var.disk_name : format("%s-%s", var.disk_name, format(var.number_format, count.index+1))}"
66+
category = "${var.disk_category}"
67+
size = "${var.disk_size}"
68+
69+
tags {
70+
created_by = "${lookup(var.disk_tags, "created_by")}"
71+
created_from = "${lookup(var.disk_tags, "created_from")}"
72+
}
73+
}
74+
75+
// Attach ECS disks to instances for Module
76+
resource "alicloud_disk_attachment" "disk_attach" {
77+
count = "${(var.number_of_instances > 0 && var.number_of_disks > 0) ? var.number_of_disks : 0}"
78+
disk_id = "${element(alicloud_disk.disks.*.id, count.index)}"
79+
instance_id = "${element(alicloud_instance.instances.*.id, count.index%var.number_of_instances)}"
80+
}
81+
82+
// Attach key pair to instances for Module
83+
resource "alicloud_key_pair_attchment" "default" {
84+
count = "${var.number_of_instances > 0 && var.key_name != "" ? 1 : 0}"
85+
86+
key_name = "${var.key_name}"
87+
instance_ids = ["${alicloud_instance.instances.*.id}"]
88+
}

outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Output the IDs of the ECS instances created
2+
output "instance_ids" {
3+
value = "${join(",", alicloud_instance.instances.*.id)}"
4+
}
5+
6+
// Output the IDs of the ECS disks created
7+
output "disk_ids" {
8+
value = "${join(",", alicloud_disk.disks.*.id)}"
9+
}

0 commit comments

Comments
 (0)