Skip to content

Commit 040f05d

Browse files
xshot9011lycbrian
andauthored
feat: support customization metric auto scaling (#40)
* feat: support select container to attached lb * fix: error in target group * feat: configuration mem, cpu as task level * feat: secret base on container inside * feat: secret separate by container * fix: code smell and example usage * fix: security issue log group with kms (#39) * feat: add defualt kms with additional override kms * fix: typo * fix: count depends on undetermined resource * fix: fix provider version to 4.65 dua to bug of 4.66 * fix: ref to non created resource * test: host header * feat: support customization metric --------- Co-authored-by: Brian - oozou <98243528+lycbrian@users.noreply.github.com>
1 parent d41b12b commit 040f05d

File tree

8 files changed

+500
-315
lines changed

8 files changed

+500
-315
lines changed

CHANGELOG.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
11
# Change Log
22

3+
## [v1.3.0] - 2023-10-20
4+
5+
### Added
6+
7+
- Support customization metric autoscaling
8+
- Local to make condition create resources: `local.is_created_aws_appautoscaling_target` `local.is_target_tracking_scaling` `local.is_contain_predefined_metric`
9+
- Related resources: `aws_appautoscaling_policy.target_tracking_scaling_policies`, `aws_appautoscaling_policy.step_scaling_policies`, `module.step_alarm`
10+
- Variables: `step_scaling_configuration`, `target_tracking_configuration`
11+
12+
### Changed
13+
14+
- Update resource tagging
15+
- local tags append with module's name created this resource
16+
- Resources: `aws_secretsmanager_secret.this`
17+
- Conditional to create resources: `aws_appautoscaling_target.this`, `aws_appautoscaling_policy.target_tracking_scaling_policies`, `aws_appautoscaling_policy.step_scaling_policies`
18+
19+
### Removed
20+
21+
- Variables: `scaling_configuration`
22+
323
## [v1.2.0] - 2023-10-11
424

525
### Added
626

727
- Support custom and built-in module KMS for cloudwatch log group
8-
- resources: `data.aws_iam_policy_document.cloudwatch_log_group_kms_policy`, `module.cloudwatch_log_group_kms`
9-
- variables: `is_create_default_kms`, `cloudwatch_log_group_kms_key_arn`
28+
- Resources: `data.aws_iam_policy_document.cloudwatch_log_group_kms_policy`, `module.cloudwatch_log_group_kms`
29+
- Variables: `is_create_default_kms`, `cloudwatch_log_group_kms_key_arn`
1030
- Validation condition `local.raise_multiple_container_attach_to_alb`
1131
- New method to create task definition with support multiple container `local.container_task_definitions`
12-
- variables: `container`
32+
- Variables: `container`
1333
- Support for 1 secretManager: N secret
14-
- resources: `aws_secretsmanager_secret.this`, `aws_secretsmanager_secret_version.this`, `aws_iam_role_policy.task_execution_role_access_secret`
34+
- Resources: `aws_secretsmanager_secret.this`, `aws_secretsmanager_secret_version.this`, `aws_iam_role_policy.task_execution_role_access_secret`
1535

1636
### Changed
1737

@@ -22,7 +42,7 @@
2242
- Non-used module level validation `local.raise_vpc_id_empty`, `local.raise_service_port_empty`, `local.raise_health_check_empty` and `local.raise_alb_listener_arn_empty`
2343
- Remove all previous method to construct the task definition for ECS
2444
- Remove all secrets usage 1 key : 1 secret; use 1 secret in JSON form
25-
- resources: `aws_secretsmanager_secret.service_secrets`, `aws_secretsmanager_secret_version.service_secrets`, `aws_iam_role_policy.task_execution_secrets`
45+
- Resources: `aws_secretsmanager_secret.service_secrets`, `aws_secretsmanager_secret_version.service_secrets`, `aws_iam_role_policy.task_execution_secrets`
2646
- Remove unused variables `is_attach_service_with_lb`, `service_info`, `apm_sidecar_ecr_url`, `apm_config`. `unix_max_connection`, `entry_point` and `command`
2747

2848
## [v1.1.12] - 2023-01-23

README.md

Lines changed: 319 additions & 54 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1 @@
1-
<!-- BEGIN_TF_DOCS -->
2-
## Requirements
3-
4-
No requirements.
5-
6-
## Providers
7-
8-
No providers.
9-
10-
## Modules
11-
12-
| Name | Source | Version |
13-
|------|--------|---------|
14-
| <a name="module_service_api"></a> [service\_api](#module\_service\_api) | ../.. | n/a |
15-
16-
## Resources
17-
18-
No resources.
19-
20-
## Inputs
21-
22-
| Name | Description | Type | Default | Required |
23-
|------|-------------|------|---------|:--------:|
24-
| <a name="input_generics_info"></a> [generics\_info](#input\_generics\_info) | Generic infomation | <pre>object({<br> region = string<br> prefix = string<br> environment = string<br> name = string<br> custom_tags = map(any)<br> })</pre> | n/a | yes |
25-
| <a name="input_service_info"></a> [service\_info](#input\_service\_info) | is\_attach\_service\_with\_lb >> Attach the container to the public ALB? (true/false)<br> service\_alb\_host\_header >> Mention host header for api endpoint<br> service\_info >> The configuration of service<br> health\_check >> Health Check Config for the service | <pre>map(object({<br> is_attach_service_with_lb = bool<br> service_alb_host_header = string<br> alb_paths = list(string)<br> alb_priority = string<br> service_info = object({<br> cpu_allocation = number<br> mem_allocation = number<br> containers_num = number<br> port = number<br> image = string<br> })<br> health_check = object({<br> interval = number<br> path = string<br> timeout = number<br> healthy_threshold = number<br> unhealthy_threshold = number<br> matcher = string<br> })<br> }))</pre> | n/a | yes |
26-
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | n/a | yes |
27-
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | VPC ID to deploy | `string` | n/a | yes |
28-
29-
## Outputs
30-
31-
No outputs.
32-
<!-- END_TF_DOCS -->
1+
# NO BODY TEST THIS FEATURE FOR LONG TIME

examples/simple/README.md

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,41 @@
1-
### Target Tracking Policies
2-
3-
```terraform
4-
scaling_configuration = {
5-
policy_type = "TargetTrackingScaling"
6-
capacity = {
7-
min_capacity = 1
8-
max_capacity = 10
9-
}
10-
scaling_behaviors = {
11-
cpu_average = {
12-
predefined_metric_type = "ECSServiceAverageCPUUtilization"
13-
target_value = 60
14-
scale_in_cooldown = 180
15-
scale_out_cooldown = 60
16-
}
17-
memory_average = {
18-
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
19-
target_value = 60
20-
scale_in_cooldown = 180
21-
scale_out_cooldown = 60
22-
}
23-
}
24-
}
25-
```
26-
27-
### Simple Policies
28-
29-
```terraform
30-
scaling_configuration = {
31-
policy_type = "StepScaling"
32-
capacity = {
33-
min_capacity = 1
34-
max_capacity = 10
35-
}
36-
scaling_behaviors = {
37-
cpu_up_average = {
38-
metric_name = "CPUUtilization"
39-
statistic = "Average"
40-
comparison_operator = ">="
41-
threshold = "65"
42-
period = "60"
43-
evaluation_periods = "1"
44-
cooldown = 60
45-
scaling_adjustment = 1
46-
}
47-
cpu_down_average = {
48-
metric_name = "CPUUtilization"
49-
statistic = "Average"
50-
comparison_operator = "<"
51-
threshold = "50"
52-
period = "60"
53-
evaluation_periods = "10"
54-
cooldown = 180
55-
scaling_adjustment = -1
56-
}
57-
}
58-
}
59-
```
60-
611
<!-- BEGIN_TF_DOCS -->
622
## Requirements
633

64-
No requirements.
4+
| Name | Version |
5+
|------|---------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0, < 5.0.0 |
8+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.3.0 |
659

6610
## Providers
6711

68-
| Name | Version |
69-
|---------------------------------------------------|---------|
70-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.50.0 |
12+
| Name | Version |
13+
|------|---------|
14+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.67.0 |
7115

7216
## Modules
7317

74-
| Name | Source | Version |
75-
|-------------------------------------------------------------------------------------|-------------------------------|---------|
76-
| <a name="module_fargate_cluster"></a> [fargate\_cluster](#module\_fargate\_cluster) | oozou/ecs-fargate-cluster/aws | 1.0.7 |
77-
| <a name="module_service_api"></a> [service\_api](#module\_service\_api) | ../.. | n/a |
78-
| <a name="module_vpc"></a> [vpc](#module\_vpc) | oozou/vpc/aws | 1.2.4 |
18+
| Name | Source | Version |
19+
|------|--------|---------|
20+
| <a name="module_api_service"></a> [api\_service](#module\_api\_service) | ../.. | n/a |
21+
| <a name="module_fargate_cluster"></a> [fargate\_cluster](#module\_fargate\_cluster) | oozou/ecs-fargate-cluster/aws | 1.0.7 |
22+
| <a name="module_payment_service"></a> [payment\_service](#module\_payment\_service) | ../.. | n/a |
23+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | oozou/vpc/aws | 1.2.4 |
7924

8025
## Resources
8126

82-
| Name | Type |
83-
|----------------------------------------------------------------------------------------------------------------------------|-------------|
27+
| Name | Type |
28+
|------|------|
8429
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
85-
| [aws_region.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
8630

8731
## Inputs
8832

89-
| Name | Description | Type | Default | Required |
90-
|-----------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|---------------|---------|:--------:|
91-
| <a name="input_custom_tags"></a> [custom\_tags](#input\_custom\_tags) | Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys. | `map(string)` | `{}` | no |
92-
| <a name="input_environment"></a> [environment](#input\_environment) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
93-
| <a name="input_name"></a> [name](#input\_name) | [Required] Name of Platfrom or application | `string` | n/a | yes |
94-
| <a name="input_prefix"></a> [prefix](#input\_prefix) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
33+
| Name | Description | Type | Default | Required |
34+
|------|-------------|------|---------|:--------:|
35+
| <a name="input_custom_tags"></a> [custom\_tags](#input\_custom\_tags) | Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys. | `map(string)` | `{}` | no |
36+
| <a name="input_environment"></a> [environment](#input\_environment) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
37+
| <a name="input_name"></a> [name](#input\_name) | [Required] Name of Platfrom or application | `string` | n/a | yes |
38+
| <a name="input_prefix"></a> [prefix](#input\_prefix) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
9539

9640
## Outputs
9741

examples/simple/main.tf

Lines changed: 12 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module "fargate_cluster" {
7272
/* -------------------------------------------------------------------------- */
7373
/* Service */
7474
/* -------------------------------------------------------------------------- */
75-
module "api_service" {
75+
module "payment_service" {
7676
source = "../.."
7777

7878
prefix = var.prefix
@@ -159,93 +159,18 @@ module "api_service" {
159159
}
160160
}
161161

162-
tags = var.custom_tags
163-
}
164-
165-
module "payment_service" {
166-
source = "../.."
167-
168-
prefix = var.prefix
169-
environment = var.environment
170-
name = format("%s-api-service", var.name)
171-
172-
# ECS service
173-
task_cpu = 1024
174-
task_memory = 2048
175-
ecs_cluster_name = module.fargate_cluster.ecs_cluster_name
176-
service_discovery_namespace = module.fargate_cluster.service_discovery_namespace
177-
is_enable_execute_command = true
178-
application_subnet_ids = module.vpc.private_subnet_ids
179-
security_groups = [
180-
module.fargate_cluster.ecs_task_security_group_id
181-
]
182-
additional_ecs_task_role_policy_arns = [
183-
"arn:aws:iam::aws:policy/AmazonSSMFullAccess"
184-
]
185-
186-
# ALB
187-
alb_listener_arn = module.fargate_cluster.alb_listener_http_arn
188-
alb_host_header = null
189-
alb_paths = ["/*"]
190-
alb_priority = "100"
191-
vpc_id = module.vpc.vpc_id
192-
health_check = {
193-
interval = 20,
194-
path = "",
195-
timeout = 10,
196-
healthy_threshold = 3,
197-
unhealthy_threshold = 3,
198-
matcher = "200,201,204"
199-
}
200-
201-
is_create_cloudwatch_log_group = true
202-
203-
container = {
204-
main_container = {
205-
name = format("%s-api-service", local.name)
206-
image = "nginx"
207-
cpu = 128
208-
memory = 256
209-
is_attach_to_lb = true
210-
port_mappings = [
211-
{
212-
# If a container has multiple ports, index 0 will be used for target group
213-
host_port = 80
214-
container_port = 80
215-
protocol = "tcp"
216-
}
217-
]
218-
entry_point = []
219-
command = []
162+
target_tracking_configuration = {
163+
policy_type = "TargetTrackingScaling"
164+
name = "cpu-average"
165+
capacity = {
166+
min_capacity = 1
167+
max_capacity = 10
220168
}
221-
side_container = {
222-
name = format("%s-nginx", local.name)
223-
image = "tutum/dnsutils"
224-
cpu = 128
225-
memory = 256
226-
port_mappings = [
227-
{
228-
host_port = 443
229-
container_port = 443
230-
protocol = "tcp"
231-
},
232-
]
233-
}
234-
}
235-
environment_variables = {
236-
main_container = {
237-
THIS_IS_ENV = "ENV1",
238-
THIS_IS_ENVV = "ENVV",
239-
}
240-
side_container = {
241-
XXXX = "XXXX",
242-
XXXXX = "XXXXX",
243-
}
244-
}
245-
secret_variables = {
246-
main_container = {
247-
THIS_IS_SECRET = "1xxxxx",
248-
THIS_IS_SECRETT = "2xxxxx",
169+
scaling_behaviors = {
170+
predefined_metric_type = "ECSServiceAverageCPUUtilization"
171+
target_value = 60
172+
scale_in_cooldown = 180
173+
scale_out_cooldown = 60
249174
}
250175
}
251176

locals.tf

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,11 @@ locals {
4040
| 1 | 1 | use custom kms */
4141
cloudwatch_log_group_kms_key_arn = var.is_create_cloudwatch_log_group ? var.cloudwatch_log_group_kms_key_arn != null ? var.cloudwatch_log_group_kms_key_arn : var.is_create_default_kms ? module.cloudwatch_log_group_kms[0].key_arn : null : null
4242

43-
comparison_operators = {
44-
">=" = "GreaterThanOrEqualToThreshold",
45-
">" = "GreaterThanThreshold",
46-
"<" = "LessThanThreshold",
47-
"<=" = "LessThanOrEqualToThreshold",
48-
}
49-
5043
tags = merge(
5144
{
5245
"Environment" = var.environment,
5346
"Terraform" = "true"
47+
"Module" = "terraform-aws-ecs-fargate-service"
5448
},
5549
var.tags
5650
)
@@ -130,3 +124,20 @@ locals {
130124
}
131125
]
132126
}
127+
128+
/* -------------------------------------------------------------------------- */
129+
/* Auto Scaling */
130+
/* -------------------------------------------------------------------------- */
131+
locals {
132+
is_created_aws_appautoscaling_target = var.target_tracking_configuration != {} || var.step_scaling_configuration != {}
133+
134+
is_target_tracking_scaling = var.target_tracking_configuration == {} ? false : true
135+
is_contain_predefined_metric = local.is_target_tracking_scaling ? try(var.target_tracking_configuration["scaling_behaviors"]["predefined_metric_type"], null) != null : false
136+
137+
comparison_operators = {
138+
">=" = "GreaterThanOrEqualToThreshold",
139+
">" = "GreaterThanThreshold",
140+
"<" = "LessThanThreshold",
141+
"<=" = "LessThanOrEqualToThreshold",
142+
}
143+
}

0 commit comments

Comments
 (0)