forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aliyun#56 from xiaozhu36/example-docs
add some examples and improve docs
- Loading branch information
Showing
39 changed files
with
2,081 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
### ECS Example | ||
|
||
The example launches a keypair and use it to launch ECS instances attaching keypair. Besides, the example also launches other resources, such as vpc, vswitch, disk and so on. | ||
The count parameter in variables.tf can let you create specify number ECS instances. | ||
|
||
### Get up and running | ||
|
||
* Planning phase | ||
|
||
terraform plan | ||
|
||
* Apply phase | ||
|
||
terraform apply | ||
|
||
* Destroy | ||
|
||
terraform destroy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
data "alicloud_instance_types" "instance_type" { | ||
instance_type_family = "ecs.n4" | ||
cpu_core_count = "1" | ||
memory_size = "2" | ||
} | ||
resource "alicloud_vpc" "main" { | ||
name = "vpc-${var.short_name}" | ||
cidr_block = "10.1.0.0/21" | ||
} | ||
|
||
resource "alicloud_vswitch" "main" { | ||
vpc_id = "${alicloud_vpc.main.id}" | ||
cidr_block = "10.1.1.0/24" | ||
availability_zone = "${var.availability_zones}" | ||
depends_on = [ | ||
"alicloud_vpc.main"] | ||
} | ||
|
||
resource "alicloud_security_group" "group" { | ||
name = "group-${var.short_name}" | ||
description = "New security group" | ||
vpc_id = "${alicloud_vpc.main.id}" | ||
} | ||
|
||
resource "alicloud_security_group_rule" "allow_http_80" { | ||
type = "ingress" | ||
ip_protocol = "tcp" | ||
nic_type = "${var.nic_type}" | ||
policy = "accept" | ||
port_range = "80/80" | ||
priority = 1 | ||
security_group_id = "${alicloud_security_group.group.id}" | ||
cidr_ip = "0.0.0.0/0" | ||
} | ||
|
||
|
||
resource "alicloud_security_group_rule" "allow_https_22" { | ||
type = "ingress" | ||
ip_protocol = "tcp" | ||
nic_type = "${var.nic_type}" | ||
policy = "accept" | ||
port_range = "22/22" | ||
priority = 1 | ||
security_group_id = "${alicloud_security_group.group.id}" | ||
cidr_ip = "0.0.0.0/0" | ||
} | ||
|
||
resource "alicloud_key_pair" "key_pair" { | ||
key_name = "${var.key_name}" | ||
key_file = "${var.private_key_file}" | ||
} | ||
|
||
resource "alicloud_instance" "instance" { | ||
instance_name = "${var.short_name}-${var.role}-${format(var.count_format, count.index+1)}" | ||
host_name = "${var.short_name}-${var.role}-${format(var.count_format, count.index+1)}" | ||
image_id = "${var.image_id}" | ||
instance_type = "${data.alicloud_instance_types.instance_type.instance_types.0.id}" | ||
count = "${var.count}" | ||
availability_zone = "${var.availability_zones}" | ||
security_groups = ["${alicloud_security_group.group.*.id}"] | ||
|
||
internet_charge_type = "${var.internet_charge_type}" | ||
internet_max_bandwidth_out = "${var.internet_max_bandwidth_out}" | ||
|
||
password = "${var.ecs_password}" | ||
key_name = "${alicloud_key_pair.key_pair.id}" | ||
|
||
allocate_public_ip = "${var.allocate_public_ip}" | ||
|
||
instance_charge_type = "PostPaid" | ||
system_disk_category = "${var.disk_category}" | ||
|
||
vswitch_id = "${alicloud_vswitch.main.id}" | ||
|
||
tags { | ||
role = "${var.role}" | ||
dc = "${var.datacenter}" | ||
} | ||
|
||
} | ||
|
||
resource "alicloud_disk" "disk" { | ||
availability_zone = "${alicloud_instance.instance.0.availability_zone}" | ||
category = "${var.disk_category}" | ||
size = "${var.disk_size}" | ||
count = "${var.disk_count}" | ||
} | ||
|
||
resource "alicloud_disk_attachment" "instance-attachment" { | ||
count = "${var.disk_count}" | ||
disk_id = "${element(alicloud_disk.disk.*.id, count.index)}" | ||
instance_id = "${element(alicloud_instance.instance.*.id, count.index%var.count)}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
output "hostname_list" { | ||
value = "${join(",", alicloud_instance.instance.*.instance_name)}" | ||
} | ||
|
||
output "ecs_ids" { | ||
value = "${join(",", alicloud_instance.instance.*.id)}" | ||
} | ||
|
||
output "ecs_public_ip_list" { | ||
value = "${join(",", alicloud_instance.instance.*.public_ip)}" | ||
} | ||
|
||
output "key_pair" { | ||
value = "${jsonencode(alicloud_key_pair.key_pair.id)}" | ||
} | ||
|
||
output "tags" { | ||
value = "${jsonencode(alicloud_instance.instance.tags)}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
variable "count" { | ||
default = "2" | ||
} | ||
variable "count_format" { | ||
default = "%02d" | ||
} | ||
variable "image_id" { | ||
default = "ubuntu_140405_64_40G_cloudinit_20161115.vhd" | ||
} | ||
|
||
variable "availability_zones" { | ||
default = "cn-beijing-a" | ||
} | ||
|
||
variable "role" { | ||
default = "work" | ||
} | ||
variable "datacenter" { | ||
default = "beijing" | ||
} | ||
variable "short_name" { | ||
default = "hi" | ||
} | ||
variable "ecs_type" { | ||
default = "ecs.n4.small" | ||
} | ||
variable "ecs_password" { | ||
default = "Test12345" | ||
} | ||
variable "allocate_public_ip" { | ||
default = true | ||
} | ||
variable "internet_charge_type" { | ||
default = "PayByTraffic" | ||
} | ||
variable "internet_max_bandwidth_out" { | ||
default = 10 | ||
} | ||
|
||
variable "disk_category" { | ||
default = "cloud_efficiency" | ||
} | ||
variable "disk_size" { | ||
default = "40" | ||
} | ||
|
||
variable "disk_count" { | ||
default = "4" | ||
} | ||
|
||
variable "nic_type" { | ||
default = "intranet" | ||
} | ||
|
||
variable "private_key_file" { | ||
default = "alicloud_ssh_key.pem" | ||
} | ||
|
||
variable "key_name" { | ||
default = "key-pair-from-terraform" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
Alicloud ECS Instance Launched In VPC Examples | ||
============================================================== | ||
|
||
A terraform module to provide ECS instances in Alicloud. These instances will be launched in the VPC network. | ||
|
||
- The module contains one VPC, one VSwitch, one Security Group, several Security Group Rules, several Disks and several Instances. | ||
- If VPC, VSwitch or Security Group is not specified, the module will launch a new one using its own parameters. | ||
- If you have no idea some parametes, such as instance type, availability zone and image id, the module will provide default values by these data source. | ||
|
||
|
||
Module Input Variables | ||
---------------------- | ||
|
||
The module aim to create one or more instances and disks in the VPC. Its input variables contains VPC, VSwitch, Security Group, Security Group Rules, ECS Disks and ECS Instances. | ||
|
||
#### Common Imput vairables | ||
|
||
- `alicloud_access_key` - The Alicloud Access Key ID to launch resources | ||
- `alicloud_secret_key` - The Alicloud Access Secret Key to launch resources | ||
- `region` - The region to launch resources | ||
- `zone_id` - The availability zone ID to launch VSwitch, ECS Instances and ECS Disks - default to a zone ID retrieved by zones' data source | ||
- `number_format` - The number format used to mark multiple resources - default to "%02d" | ||
|
||
#### VPC Input variables | ||
|
||
- `vpc_id` - VPC ID to launch a new VSwitch and Security Group | ||
- `vpc_name` - VPC name to mark a new VPC when `vpc_id` is not specified - default to "TF-VPC" | ||
- `vpc_cidr` - VPC CIDR block to launch a new VPC when `vpc_id` is not specified - default to "172.16.0.0/12" | ||
|
||
#### VSwitch Input variables | ||
|
||
- `vswitch_id` - VSwitch ID to launch new ECS instances | ||
- `vswitch_name` - VSwitch name to mark a new VSwitch when `vswitch_id` is not specified - default to "TF_VSwitch" | ||
- `vswitch_cidr` - VSwitch CIDR block to launch a new VSwitch when `vswitch_id` is not specified. It has a default value '172.16.0.0/16' according `vpc_cidr's` default value. | ||
|
||
`NOTE`: One of the `vswitch_id` and `vswitch_cidr` is required. | ||
|
||
#### Security Group Input variables | ||
|
||
- `sg_id` - Security Group ID to configure rules and launch new ECS instances | ||
- `sg_name` - Security Group name to mark a new Security Group when `sg_id` is not specified - default to "TF_Security_Group" | ||
- `ip_protocols` - List of IP protocols to configure Security Group rules - item choices: ["tcp", "udp", "icmp", "gre", "all"] | ||
- `rule_directions` - List of directions to configure Security Group rules - item choices: ["ingress", "egress"] - default to ["ingress"] | ||
- `policies` - List of policies to configure Security Group rules - item choices: ["accept", "drop"] - default to ["accept"] | ||
- `port_ranges` - List of port ranges to configure Security Group rules - default to ["-1/-1"] | ||
- `priorities` - List of priorities to configure Security Group rules - item choices: [1-100] - default to [1] | ||
- `cidr_ips` - List of CIDR IPs to configure Security Group rules - default to ["0.0.0.0/0"] | ||
|
||
`NOTE`: | ||
1. The number of Security Group rules depends on the size of `ip_protocols` | ||
2. All of the Security Group rules' network type are `intranet` | ||
|
||
#### ECS Disk Input variables | ||
|
||
- `number_of_disks` - The number disks you want to launch - default to 0 | ||
- `disk_name` - ECS disk name to mark data disk(s) - default to "TF_ECS_Disk" | ||
- `disk_category` - ECS disk category to launch data disk(s) - choices to ["cloud_ssd", "cloud_efficiency"] - default to "cloud_efficiency" | ||
- `disk_size` - ECS disk size to launch data disk(s) - default to 40 | ||
- `disk_tags` - A map for setting ECS disk tags - default to | ||
|
||
disk_tags = { | ||
created_by = "Terraform" | ||
created_from = "module-tf-alicloud-ecs-instance" | ||
} | ||
|
||
#### ECS Instance Input variables | ||
|
||
- `number_of_instances` - The number of instances you want to launch - default to 1 | ||
- `image_id` - The image id to use - default to an Ubuntu-64bit image ID retrieved by images' data source | ||
- `instance_type` - The ECS instance type, e.g. ecs.n4.small, - default to a 1Core 2GB instance type retrieved by instance_types' data source | ||
- `instance_name` - ECS instance name to mark instance(s) - default to "TF_ECS_Instance" | ||
- `host_name` - ECS instance host name to configure instance(s) - default to "TF_ECS_Host_Name" | ||
- `system_category` - ECS disk category to launch system disk - choices to ["cloud_ssd", "cloud_efficiency"] - default to "cloud_efficiency" | ||
- `system_size` - ECS disk size to launch system disk - default to 40 | ||
- `allocate_public_ip` - Whether to allocate public for instance(s) - default to true | ||
- `internet_charge_type` - The internet charge type for setting instance network - choices["PayByTraffic", "PayByBandwidth"] - default to "PayByTraffic" | ||
- `internet_max_bandwidth_out` - The max out bandwidth for setting instance network - default to 10 | ||
- `instance_charge_type` - The instance charge type - choices to ["PrePaid", "PostPaid"] - default to "PostPaid" | ||
- `period` - The instance charge period when instance charge type is 'PrePaid' - default to 1 | ||
- `key_name` - The instance key pair name for SSH keys | ||
- `password` - The instance password | ||
- `instance_tags` - A map for setting ECS Instance tags - default to | ||
|
||
instance_tags = { | ||
created_by = "Terraform" | ||
created_from = "module-tf-alicloud-ecs-instance" | ||
} | ||
|
||
|
||
Usage | ||
----- | ||
You can input and specify some parameters in the variables.tf, and then execute the following commands to create and manage them: | ||
|
||
* Planning phase | ||
|
||
terraform plan | ||
|
||
* Apply phase | ||
|
||
terraform apply | ||
|
||
|
||
* Destroy | ||
|
||
terraform destroy | ||
|
||
Module Output Variables | ||
----------------------- | ||
|
||
- instance_ids - List of new instance ids | ||
- disk_ids - List of new data disk ids | ||
- vpc_id - A new VPC ID | ||
- vswitch_id - A new VSwitch ID | ||
- security_group_id - A new Security Group ID | ||
|
||
Authors | ||
------- | ||
Created and maintained by He Guimin(heguimin36@163.com) | ||
|
||
License | ||
------- | ||
Apache 2 Licensed. See LICENSE for full details. |
Oops, something went wrong.