Skip to content

Commit

Permalink
feat: Add module ECS VM instance (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrTitov authored May 4, 2023
1 parent 8dc4551 commit 7968b1b
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 1 deletion.
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
# Module name
# ECS VM instance

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.4 |
| <a name="requirement_huaweicloud"></a> [huaweicloud](#requirement\_huaweicloud) | ~>1.47 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_huaweicloud"></a> [huaweicloud](#provider\_huaweicloud) | ~>1.47 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [huaweicloud_compute_eip_associate.ecs_associated](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/compute_eip_associate) | resource |
| [huaweicloud_compute_instance.main](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/compute_instance) | resource |
| [huaweicloud_availability_zones.zones](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/data-sources/availability_zones) | data source |
| [huaweicloud_images_image.compute_instance_image](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/data-sources/images_image) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_agency_name"></a> [agency\_name](#input\_agency\_name) | Specifies the IAM agency name which is created on IAM to provide temporary credentials for ECS to access cloud services | `string` | `null` | no |
| <a name="input_availability_zone"></a> [availability\_zone](#input\_availability\_zone) | Specifies the availability zone in which to create the instance | `string` | `null` | no |
| <a name="input_data_disks"></a> [data\_disks](#input\_data\_disks) | Specifies the EVS Volumes and parameters:<br><br> * `type` - Specifies the ECS data disk type, which must be one of available disk types, contains of SSD, GPSSD and SAS;<br> * `size` - Specifies the data disk size, in GB. The value ranges form 10 to 32768;<br> * `snapshot_id` - Specifies the snapshot id;<br> * `kms_key_id` - Specifies the ID of a KMS key, this is used to encrypt the disk. | <pre>list(object({<br> volume_type = optional(string, "SSD")<br> size = optional(number, 40)<br> snapshot_id = optional(string)<br> kms_key_id = optional(string)<br> }))</pre> | `[]` | no |
| <a name="input_data_disks1"></a> [data\_disks1](#input\_data\_disks1) | Specifies the ECS data disks and parameters | <pre>list(object({<br> type = string<br> size = number<br> }))</pre> | `[]` | no |
| <a name="input_description"></a> [description](#input\_description) | Specifies the description of the instance | `string` | `null` | no |
| <a name="input_eip_address"></a> [eip\_address](#input\_eip\_address) | Specifies the EIP address to associate | `string` | `null` | no |
| <a name="input_eip_enabled"></a> [eip\_enabled](#input\_eip\_enabled) | Enabler for EIP | `bool` | `false` | no |
| <a name="input_flavor_name"></a> [flavor\_name](#input\_flavor\_name) | Specifies the name of the desired flavor for the instance | `string` | `"s7n.medium.2"` | no |
| <a name="input_image_id"></a> [image\_id](#input\_image\_id) | Specifies the image ID of the desired image for the instance | `string` | `null` | no |
| <a name="input_instance_image"></a> [instance\_image](#input\_instance\_image) | Requirements for Compute Instance Image | <pre>object({<br> name = optional(string, "Ubuntu 22.04 server 64bit")<br> visibility = optional(string, "public")<br> architecture = optional(string, "x86")<br> most_recent = optional(bool, true)<br> })</pre> | `{}` | no |
| <a name="input_key_pair"></a> [key\_pair](#input\_key\_pair) | Specifies the SSH keypair name used for logging in to the instance | `string` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | Specifies the name of the ECS VM instance | `string` | n/a | yes |
| <a name="input_name_postfix"></a> [name\_postfix](#input\_name\_postfix) | Name Postfix for ECS VM instance | `string` | `null` | no |
| <a name="input_region"></a> [region](#input\_region) | Specifies the region in which to create the resource. If omitted, the provider-level region will be used | `string` | `null` | no |
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | Specifies an array of one or more security group IDs to associate with the instance | `list(string)` | `[]` | no |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | VPC Subnet ID in UUID format | `string` | n/a | yes |
| <a name="input_system_disk_size"></a> [system\_disk\_size](#input\_system\_disk\_size) | Specifies the system disk size in GB | `number` | `40` | no |
| <a name="input_system_disk_type"></a> [system\_disk\_type](#input\_system\_disk\_type) | Specifies the system disk type of the instance:<br><br> * `SAS`: high I/O disk type;<br> * `SSD`: ultra-high I/O disk type;<br> * `GPSSD`: general purpose SSD disk type;<br> * `ESSD`: Extreme SSD type. | `string` | `"GPSSD"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Specifies the key/value pairs to associate with the VPC | `map(string)` | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_ip"></a> [ip](#output\_ip) | Compute Instance Private IP |
<!-- END_TF_DOCS -->
54 changes: 54 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
data "huaweicloud_availability_zones" "zones" {
region = var.region
}

data "huaweicloud_images_image" "compute_instance_image" {
name = var.instance_image.name
visibility = var.instance_image.visibility
architecture = var.instance_image.architecture
most_recent = var.instance_image.most_recent
region = var.region
}

locals {
name = var.name_postfix == null ? var.name : format("%s-%s", var.name, var.name_postfix)
}

resource "huaweicloud_compute_instance" "main" {
name = local.name
description = var.description
region = var.region
image_id = var.image_id == null ? data.huaweicloud_images_image.compute_instance_image.id : var.image_id
flavor_name = var.flavor_name
key_pair = var.key_pair
agency_name = var.agency_name
security_group_ids = var.security_group_ids
availability_zone = var.availability_zone == null ? element(data.huaweicloud_availability_zones.zones.names, 0) : var.availability_zone
system_disk_size = var.system_disk_size
system_disk_type = var.system_disk_type


network {
uuid = var.subnet_id
}

dynamic "data_disks" {
for_each = var.data_disks
content {
type = data_disks.value.type
size = data_disks.value.size
snapshot_id = data_disks.value.snapshot_id
kms_key_id = data_disks.value.kms_key_id
}
}

tags = var.tags
}


resource "huaweicloud_compute_eip_associate" "ecs_associated" {
count = var.eip_enabled ? 1 : 0

public_ip = var.eip_address
instance_id = huaweicloud_compute_instance.main.id
}
4 changes: 4 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "ip" {
description = "Compute Instance Private IP"
value = huaweicloud_compute_instance.main.access_ip_v4
}
148 changes: 148 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
variable "name" {
description = "Specifies the name of the ECS VM instance"
type = string
nullable = false
}

variable "name_postfix" {
description = "Name Postfix for ECS VM instance"
type = string
default = null
}

variable "description" {
description = "Specifies the description of the instance"
type = string
default = null
}

variable "region" {
description = "Specifies the region in which to create the resource. If omitted, the provider-level region will be used"
type = string
default = null
}

variable "key_pair" {
description = "Specifies the SSH keypair name used for logging in to the instance"
type = string
nullable = false
}

variable "flavor_name" {
description = "Specifies the name of the desired flavor for the instance"
type = string
default = "s7n.medium.2"
}

variable "image_id" {
description = "Specifies the image ID of the desired image for the instance"
type = string
default = null
}

variable "instance_image" {
description = "Requirements for Compute Instance Image"
type = object({
name = optional(string, "Ubuntu 22.04 server 64bit")
visibility = optional(string, "public")
architecture = optional(string, "x86")
most_recent = optional(bool, true)
})
default = {}
}

variable "subnet_id" {
description = "VPC Subnet ID in UUID format"
type = string
nullable = false
}

variable "availability_zone" {
description = "Specifies the availability zone in which to create the instance"
type = string
default = null
}

variable "system_disk_type" {
description = <<DES
Specifies the system disk type of the instance:
* `SAS`: high I/O disk type;
* `SSD`: ultra-high I/O disk type;
* `GPSSD`: general purpose SSD disk type;
* `ESSD`: Extreme SSD type.
DES
type = string
default = "GPSSD"
validation {
condition = contains(["SAS", "SSD", "GPSSD", "ESSD"], var.system_disk_type)
error_message = "Possible values are: 'SAS', 'SSD', 'GPSSD' and 'ESSD'."
}
}

variable "system_disk_size" {
description = "Specifies the system disk size in GB"
type = number
default = 40
validation {
condition = 1 <= var.system_disk_size && var.system_disk_size <= 1024
error_message = "Value range: 1 to 1024."
}
}

variable "data_disks1" {
description = "Specifies the ECS data disks and parameters"
type = list(object({
type = string
size = number
}))
default = []
}

variable "data_disks" {
description = <<DES
Specifies the EVS Volumes and parameters:
* `type` - Specifies the ECS data disk type, which must be one of available disk types, contains of SSD, GPSSD and SAS;
* `size` - Specifies the data disk size, in GB. The value ranges form 10 to 32768;
* `snapshot_id` - Specifies the snapshot id;
* `kms_key_id` - Specifies the ID of a KMS key, this is used to encrypt the disk.
DES
type = list(object({
volume_type = optional(string, "SSD")
size = optional(number, 40)
snapshot_id = optional(string)
kms_key_id = optional(string)
}))
default = []
}

variable "security_group_ids" {
description = "Specifies an array of one or more security group IDs to associate with the instance"
type = list(string)
default = []
}

variable "agency_name" {
description = "Specifies the IAM agency name which is created on IAM to provide temporary credentials for ECS to access cloud services"
type = string
default = null
}

variable "eip_enabled" {
description = "Enabler for EIP"
type = bool
default = false
}

variable "eip_address" {
description = "Specifies the EIP address to associate"
type = string
default = null
}

variable "tags" {
description = "Specifies the key/value pairs to associate with the VPC"
type = map(string)
default = {}
}
7 changes: 7 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
terraform {
required_version = "~> 1.4"

required_providers {
huaweicloud = {
source = "huaweicloud/huaweicloud"
version = "~>1.47"
}
}
}

0 comments on commit 7968b1b

Please sign in to comment.