Skip to content

Commit

Permalink
[add]: add as module
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Sultanov committed Nov 1, 2023
1 parent 94826ba commit d25dfa5
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 0 deletions.
65 changes: 65 additions & 0 deletions terraform-cloud-autoscaling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Cloud.ru Auto Scaling Terraform Module

Terraform moudle which create Auto Scaling resource on Huawei Cloud.

These types of resources are supported:

* [Auto Scaling Group](https://registry.terraform.io/providers/sbercloud-terraform/sbercloud/latest/docs/resources/as_group)
* [Auto Scaling Configuration](https://registry.terraform.io/providers/sbercloud-terraform/sbercloud/latest/docs/resources/as_configuration)

## Usage

```hcl
module "example" {
source = "https://github.com/sbercloud-terraform/terraform-cloud-modules/tree/main/terraform-cloud-autoscaling"
// Autoscaling Configuration
configuration_name = "testASConfig"
instance_id = "2c31a1d8-80db-49bc-a3b1-8db55bdc0e60"
key_name = "KeyPair"
// Autoscaling Group
group_name = "testASGroup"
availability_zones = ["eu-west-0a", "eu-west-0b"]
network_id = "55534eaa-533a-419d-9b40-ec427ea7195a"
security_group = "e28c7982-ecf0-4498-852d-9683cfc364f2"
vpc_id = "8eed4fc7-e5e5-44a2-b5f2-23b3e5d46235"
}
```

## Conditional creation

This moudle can create both Auto Scaling group and Auto Scaling configuration, it is
possible to use existing scaling configuration only if you specify `as_config_id` parameter.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| as_config_id | Specifying existing autoscaling configuration ID | string | `""` | no |
| configuration_name | The name for the autoscaling configuration | string | `""` | no |
| instance_id | The ID of the ECS instance | string | `""` | no |
| key_name | The KeyPair of the ECS instance | string | `""` | no |
| group_name | The name of the autoscaling group | string | `-` | yes |
| availability_zones | List of availability zones in which to create the instances in the autoscaling group | list | `[]` | no |
| desire_instance_number | The expected number of ECS instances | string | `"0"` | no |
| min_instance_number | The minimum number of ECS instances | string | `"0"` | no |
| max_instance_number | The maximum number of ECS instances | string | `"1"` | no |
| cool_down_time | The cooling duration(in seconds) | string | `"300"` | no |
| lb_listener_id | The ELB listener IDs, supporting up to three listeners, which are separated using a comma(,) | string | `""` | no |
| network_id | The subnet Network ID | string | `-` | yes |
| security_group | The security group ID to associate with the autoscaling group | string | `-` | yes |
| vpc_id | The VPC ID | string | `-` | yes |
| health_periodic_audit_method | The health check method for instances in the AS group. Valid options are ELB_AUDIT and NOVA_AUDIT | string | `"NOVA_AUDIT"` | no |
| health_periodic_audit_time | The health check period for instances. Valid options are 5 minutes, 15 minutes, 60 minutes, and 180 minutes | string | `"5"` | no |
| instance_terminate_policy | The instance removal policy. Valid options are OLD_CONFIG_OLD_INSTNACE, OLD_CONFIG_NEW_INSTANCE, OLD_INSTANCE, and NEW_INSTANCE | string | `"OLD_CONFIG_OLD_INSTANCE"` | no |
| delete_publicip | Whether to delete the elastic IP address bound to the instances of the autoscaling group | string | `"false"` | no |


## Outputs

| Name | Description |
|------|-------------|
| this_as_group_id | The id of the autoscaling group |
| this_as_configuration_id | The id of the autoscaling configuration |

24 changes: 24 additions & 0 deletions terraform-cloud-autoscaling/examples/asg_basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Autoscaling basic example

Configuration in this directory creates Autoscaling group and Autoscaling configuration.

## Usage
To run this example you need first replace the configuration like instance_id, security_group,etc, with your own resource and execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Outputs

| Name | Description |
|------|-------------|
| this_as_group_id | The id of the autoscaling group |
| this_as_configuration_id | The id of the autoscaling configuration |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
30 changes: 30 additions & 0 deletions terraform-cloud-autoscaling/examples/asg_basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
terraform {
required_providers {
sbercloud = {
source = "sbercloud-terraform/sbercloud"
}
}
}

provider "sbercloud" {
region = "ru-moscow-1"
account_name = ""
access_key = ""
secret_key = ""
}
data "sbercloud_networking_secgroup" "default" {
name = "default"
}

module "example" {
source ="../.."

configuration_name = "testASConfig"
instance_id = "instance_id"
key_name = "KeyPair"
group_name = "testASGroup"
availability_zones = ["ru-moscow-1a", "ru-moscow-1b"]
network_id = "subnet_id"
security_group = "${data.sbercloud_networking_secgroup.default.id}"
vpc_id = "vpc_id"
}
9 changes: 9 additions & 0 deletions terraform-cloud-autoscaling/examples/asg_basic/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "this_as_configuration_id" {
description = "The id of the autoscaling configuration"
value = "${module.example.this_as_configuration_id}"
}

output "this_as_group_id" {
description = "The id of the autoscaling group"
value = "${module.example.this_as_group_id}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Autoscaling with existing scaling configuration example

Configuration in this directory creates Autoscaling group and Autoscaling configuration.

## Usage
To run this example you need first replace the configuration like as_config_id, security_group,etc, with your own resource and execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Outputs

| Name | Description |
|------|-------------|
| this_as_group_id | The id of the autoscaling group |
| this_as_configuration_id | The id of the autoscaling configuration |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
terraform {
required_providers {
sbercloud = {
source = "sbercloud-terraform/sbercloud"
}
}
}

provider "sbercloud" {
region = "ru-moscow-1"
account_name = ""
access_key = ""
secret_key = ""
}
data "sbercloud_networking_secgroup" "default" {
name = "default"
}

module "example" {
source ="../.."

as_config_id = "config_id"
group_name = "testASGroup"
availability_zones = ["ru-moscow-1a", "ru-moscow-1b"]
network_id = "network_id"
security_group = "${data.sbercloud_networking_secgroup.default.id}"
vpc_id = "vpc_id"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "this_as_configuration_id" {
description = "The id of the autoscaling configuration"
value = "${module.example.this_as_configuration_id}"
}

output "this_as_group_id" {
description = "The id of the autoscaling group"
value = "${module.example.this_as_group_id}"
}
50 changes: 50 additions & 0 deletions terraform-cloud-autoscaling/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
terraform {
required_providers {
sbercloud = {
source = "mycloud.com/myorg/sbercloud"
}
}
}

provider "sbercloud" {
region = "ru-moscow-1"
account_name = ""
access_key = ""
secret_key = ""
}
# Create AS Configuration
resource "sbercloud_as_configuration" "this" {
count = "${var.as_config_id=="" ? 1 : 0}"
scaling_configuration_name = "${var.configuration_name}"


instance_config {
instance_id = "${var.instance_id}"
key_name = "${var.key_name}"
}
}

# Create AS Group
resource "sbercloud_as_group" "this" {
scaling_group_name = "${var.group_name}"
scaling_configuration_id = "${var.as_config_id=="" ? join("",sbercloud_as_configuration.this.*.id) : var.as_config_id}"
available_zones = "${var.availability_zones}"
desire_instance_number = "${var.desire_instance_number}"
min_instance_number = "${var.min_instance_number}"
max_instance_number = "${var.max_instance_number}"
cool_down_time = "${var.cool_down_time}"
lb_listener_id = "${var.lb_listener_id}"

networks {
id = "${var.network_id}"
}
security_groups {
id = "${var.security_group}"
}

vpc_id = "${var.vpc_id}"
health_periodic_audit_method = "${var.health_periodic_audit_method}"
health_periodic_audit_time = "${var.health_periodic_audit_time}"
instance_terminate_policy = "${var.instance_terminate_policy}"
delete_publicip = "${var.delete_publicip}"
}
9 changes: 9 additions & 0 deletions terraform-cloud-autoscaling/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "this_as_configuration_id" {
description = "The id of the autoscaling configuration"
value = "${var.as_config_id=="" ? join("",sbercloud_as_configuration.this.*.id) : var.as_config_id}"
}

output "this_as_group_id" {
description = "The id of the autoscaling group"
value = "${sbercloud_as_group.this.id}"
}
86 changes: 86 additions & 0 deletions terraform-cloud-autoscaling/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
variable "as_config_id" {
description = "Specifying existing autoscaling configuration ID"
default = ""
}

variable "configuration_name" {
description = "The name of the autoscaling configuration"
default = "test"
}

variable "instance_id" {
description = "The ID of the ECS instance"
default = ""
}

variable "key_name" {
description = "The KeyPair of the ECS instance"
default = ""
}

variable "group_name" {
description = "The name of the autoscaling group"
}

variable "availability_zones" {
description = "The list of availability zones in which to create the instances in the autoscaling group"
type = list(string)
default = ["ru-moscow-1a"]
}

variable "desire_instance_number" {
description = "The expected number of ECS instances"
default = "0"
}

variable "min_instance_number" {
description = "The minimum number of ECS instances"
default = "0"
}

variable "max_instance_number" {
description = "The maximum number of ECS instances"
default = "1"
}

variable "cool_down_time" {
description = "The cooling duration(in seconds)"
default = "300"
}

variable "lb_listener_id" {
description = "The ELB listener IDs, supporting up to three listeners, which are separated using a comma(,)"
default = ""
}

variable "network_id" {
description = "The subnet Network ID"
}

variable "security_group" {
description = "The security group ID to associate with the autoscaling group"
}

variable "vpc_id" {
description = "The VPC ID"
}

variable "health_periodic_audit_method" {
description = "The health check method for instances in the AS group. Valid options are ELB_AUDIT and NOVA_AUDIT"
default = "NOVA_AUDIT"
}

variable "health_periodic_audit_time" {
description = "The health check period for instances. Valid options are 5 minutes, 15 minutes, 60 minutes, and 180 minutes"
default = "5"
}

variable "instance_terminate_policy" {
description = "The instance removal policy. Valid options are OLD_CONFIG_OLD_INSTNACE, OLD_CONFIG_NEW_INSTANCE, OLD_INSTANCE, and NEW_INSTANCE"
default = "OLD_CONFIG_OLD_INSTANCE"
}

variable "delete_publicip" {
description = "Whether to delete the elastic IP address bound to the instances of the autoscaling group"
default = "false"
}

0 comments on commit d25dfa5

Please sign in to comment.