Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support customization metric auto scaling #40

Merged
merged 53 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
5f8983e
chore: format code
xshot9011 Feb 16, 2023
d1f80bf
chore: format code
xshot9011 Feb 16, 2023
3e5bdc9
chore: just want to snapshot
xshot9011 Feb 17, 2023
68965ea
chore: remove comment
xshot9011 Feb 17, 2023
f69d52a
chore: remove previous version
xshot9011 Feb 17, 2023
30b064c
chore: first darft
xshot9011 Feb 17, 2023
d71dd90
chore: make it first run; before restrucutre it
xshot9011 Feb 17, 2023
b18e537
chore: make it first run
xshot9011 Feb 17, 2023
5b3f835
chore: remove comment part
xshot9011 Feb 21, 2023
9241e63
chore: remove comment part
xshot9011 Feb 21, 2023
8afaf60
feat: support select container to attached lb
xshot9011 Feb 21, 2023
a5a1030
chore: remove unused resource
xshot9011 Feb 21, 2023
28d24c4
chore: remove raise valid
xshot9011 Feb 21, 2023
8c68f40
fix: error in target group
xshot9011 Feb 21, 2023
ee94be5
fix: error in target group
xshot9011 Feb 21, 2023
4c3a604
chore: update listener rule
xshot9011 Feb 21, 2023
6209400
feat: configuration mem, cpu as task level
xshot9011 Feb 21, 2023
f7ad157
feat: secret base on container inside
xshot9011 Feb 21, 2023
69b3bfe
feat: secret separate by container
xshot9011 Feb 21, 2023
46382c9
fix: secrets typo
xshot9011 Feb 21, 2023
a8c4183
chore: remove validation
xshot9011 Feb 21, 2023
5d056f3
chore: update new way to put secret and env
xshot9011 Feb 21, 2023
2a2c757
chore: remove unused var
xshot9011 Feb 21, 2023
f488403
chore: update output
xshot9011 Feb 21, 2023
40151b5
chore: update env and sec
xshot9011 Feb 21, 2023
cfd49ab
chore: update env and sec
xshot9011 Feb 21, 2023
15f8d66
chore: update env and sec
xshot9011 Feb 21, 2023
75eb55f
chore: update env and sec
xshot9011 Feb 21, 2023
517e7c1
chore: update env and sec
xshot9011 Feb 21, 2023
626ef4a
chore: fix raise condition
xshot9011 Feb 21, 2023
7e7fc9e
chore: remove comment
xshot9011 Feb 21, 2023
4b994a7
chore: remove comment
xshot9011 Feb 21, 2023
36ce13e
fix: code smell and example usage
xshot9011 Feb 27, 2023
bf5e136
fix: security issue log group with kms (#39)
xshot9011 May 12, 2023
61e8199
fix: ref to non created resource
xshot9011 Jun 2, 2023
ba34b13
chore: test host_headers
xshot9011 Jun 2, 2023
1e31e6a
chore: test host_headers
xshot9011 Jun 2, 2023
1643814
test: host header
lycbrian Jun 8, 2023
547d1bd
chore: un limite provider version
xshot9011 Jun 8, 2023
cc51a24
Merge branch 'feat/support-multiple-sidecard-container' of github.com…
xshot9011 Jun 8, 2023
17ebc86
chore: linter
xshot9011 Aug 10, 2023
a3d7d2b
chore: lint code
xshot9011 Aug 11, 2023
f5e0084
chore: update CHANGELOG
xshot9011 Aug 11, 2023
97975b0
chore: mask the date
xshot9011 Aug 11, 2023
9cde567
chore: first draft before changing all things
xshot9011 Oct 9, 2023
b8c97a5
feat: support customization metric
xshot9011 Oct 10, 2023
f1b9c17
chore: update CHANGELOG
xshot9011 Oct 11, 2023
8074ca8
chore: merge
xshot9011 Oct 11, 2023
ab01d81
fix: merge conflict
xshot9011 Oct 20, 2023
0a896f2
chore: update CHANGELOG
xshot9011 Oct 20, 2023
cef53b7
chore: update README
xshot9011 Oct 20, 2023
026544e
chore: update README for example
xshot9011 Oct 20, 2023
4565c19
chore: update examples
xshot9011 Oct 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
# Change Log

## [v1.3.0] - 2023-10-20

### Added

- Support customization metric autoscaling
- Local to make condition create resources: `local.is_created_aws_appautoscaling_target` `local.is_target_tracking_scaling` `local.is_contain_predefined_metric`
- Related resources: `aws_appautoscaling_policy.target_tracking_scaling_policies`, `aws_appautoscaling_policy.step_scaling_policies`, `module.step_alarm`
- Variables: `step_scaling_configuration`, `target_tracking_configuration`

### Changed

- Update resource tagging
- local tags append with module's name created this resource
- Resources: `aws_secretsmanager_secret.this`
- Conditional to create resources: `aws_appautoscaling_target.this`, `aws_appautoscaling_policy.target_tracking_scaling_policies`, `aws_appautoscaling_policy.step_scaling_policies`

### Removed

- Variables: `scaling_configuration`

## [v1.2.0] - 2023-10-11

### Added

- Support custom and built-in module KMS for cloudwatch log group
- resources: `data.aws_iam_policy_document.cloudwatch_log_group_kms_policy`, `module.cloudwatch_log_group_kms`
- variables: `is_create_default_kms`, `cloudwatch_log_group_kms_key_arn`
- Resources: `data.aws_iam_policy_document.cloudwatch_log_group_kms_policy`, `module.cloudwatch_log_group_kms`
- Variables: `is_create_default_kms`, `cloudwatch_log_group_kms_key_arn`
- Validation condition `local.raise_multiple_container_attach_to_alb`
- New method to create task definition with support multiple container `local.container_task_definitions`
- variables: `container`
- Variables: `container`
- Support for 1 secretManager: N secret
- resources: `aws_secretsmanager_secret.this`, `aws_secretsmanager_secret_version.this`, `aws_iam_role_policy.task_execution_role_access_secret`
- Resources: `aws_secretsmanager_secret.this`, `aws_secretsmanager_secret_version.this`, `aws_iam_role_policy.task_execution_role_access_secret`

### Changed

Expand All @@ -22,7 +42,7 @@
- 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`
- Remove all previous method to construct the task definition for ECS
- Remove all secrets usage 1 key : 1 secret; use 1 secret in JSON form
- resources: `aws_secretsmanager_secret.service_secrets`, `aws_secretsmanager_secret_version.service_secrets`, `aws_iam_role_policy.task_execution_secrets`
- Resources: `aws_secretsmanager_secret.service_secrets`, `aws_secretsmanager_secret_version.service_secrets`, `aws_iam_role_policy.task_execution_secrets`
- Remove unused variables `is_attach_service_with_lb`, `service_info`, `apm_sidecar_ecr_url`, `apm_config`. `unix_max_connection`, `entry_point` and `command`

## [v1.1.12] - 2023-01-23
Expand Down
373 changes: 319 additions & 54 deletions README.md

Large diffs are not rendered by default.

33 changes: 1 addition & 32 deletions examples/ecs_ec2_capacity_provider/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

No requirements.

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_service_api"></a> [service\_api](#module\_service\_api) | ../.. | n/a |

## Resources

No resources.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <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 |
| <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 |
| <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 |
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | VPC ID to deploy | `string` | n/a | yes |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
# NO BODY TEST THIS FEATURE FOR LONG TIME
100 changes: 22 additions & 78 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -1,97 +1,41 @@
### Target Tracking Policies

```terraform
scaling_configuration = {
policy_type = "TargetTrackingScaling"
capacity = {
min_capacity = 1
max_capacity = 10
}
scaling_behaviors = {
cpu_average = {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
target_value = 60
scale_in_cooldown = 180
scale_out_cooldown = 60
}
memory_average = {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
target_value = 60
scale_in_cooldown = 180
scale_out_cooldown = 60
}
}
}
```

### Simple Policies

```terraform
scaling_configuration = {
policy_type = "StepScaling"
capacity = {
min_capacity = 1
max_capacity = 10
}
scaling_behaviors = {
cpu_up_average = {
metric_name = "CPUUtilization"
statistic = "Average"
comparison_operator = ">="
threshold = "65"
period = "60"
evaluation_periods = "1"
cooldown = 60
scaling_adjustment = 1
}
cpu_down_average = {
metric_name = "CPUUtilization"
statistic = "Average"
comparison_operator = "<"
threshold = "50"
period = "60"
evaluation_periods = "10"
cooldown = 180
scaling_adjustment = -1
}
}
}
```

<!-- BEGIN_TF_DOCS -->
## Requirements

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

## Providers

| Name | Version |
|---------------------------------------------------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.50.0 |
| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.67.0 |

## Modules

| Name | Source | Version |
|-------------------------------------------------------------------------------------|-------------------------------|---------|
| <a name="module_fargate_cluster"></a> [fargate\_cluster](#module\_fargate\_cluster) | oozou/ecs-fargate-cluster/aws | 1.0.7 |
| <a name="module_service_api"></a> [service\_api](#module\_service\_api) | ../.. | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | oozou/vpc/aws | 1.2.4 |
| Name | Source | Version |
|------|--------|---------|
| <a name="module_api_service"></a> [api\_service](#module\_api\_service) | ../.. | n/a |
| <a name="module_fargate_cluster"></a> [fargate\_cluster](#module\_fargate\_cluster) | oozou/ecs-fargate-cluster/aws | 1.0.7 |
| <a name="module_payment_service"></a> [payment\_service](#module\_payment\_service) | ../.. | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | oozou/vpc/aws | 1.2.4 |

## Resources

| Name | Type |
|----------------------------------------------------------------------------------------------------------------------------|-------------|
| Name | Type |
|------|------|
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|-----------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|---------------|---------|:--------:|
| <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 |
| <a name="input_environment"></a> [environment](#input\_environment) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | [Required] Name of Platfrom or application | `string` | n/a | yes |
| <a name="input_prefix"></a> [prefix](#input\_prefix) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <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 |
| <a name="input_environment"></a> [environment](#input\_environment) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | [Required] Name of Platfrom or application | `string` | n/a | yes |
| <a name="input_prefix"></a> [prefix](#input\_prefix) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |

## Outputs

Expand Down
99 changes: 12 additions & 87 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module "fargate_cluster" {
/* -------------------------------------------------------------------------- */
/* Service */
/* -------------------------------------------------------------------------- */
module "api_service" {
module "payment_service" {
source = "../.."

prefix = var.prefix
Expand Down Expand Up @@ -159,93 +159,18 @@ module "api_service" {
}
}

tags = var.custom_tags
}

module "payment_service" {
source = "../.."

prefix = var.prefix
environment = var.environment
name = format("%s-api-service", var.name)

# ECS service
task_cpu = 1024
task_memory = 2048
ecs_cluster_name = module.fargate_cluster.ecs_cluster_name
service_discovery_namespace = module.fargate_cluster.service_discovery_namespace
is_enable_execute_command = true
application_subnet_ids = module.vpc.private_subnet_ids
security_groups = [
module.fargate_cluster.ecs_task_security_group_id
]
additional_ecs_task_role_policy_arns = [
"arn:aws:iam::aws:policy/AmazonSSMFullAccess"
]

# ALB
alb_listener_arn = module.fargate_cluster.alb_listener_http_arn
alb_host_header = null
alb_paths = ["/*"]
alb_priority = "100"
vpc_id = module.vpc.vpc_id
health_check = {
interval = 20,
path = "",
timeout = 10,
healthy_threshold = 3,
unhealthy_threshold = 3,
matcher = "200,201,204"
}

is_create_cloudwatch_log_group = true

container = {
main_container = {
name = format("%s-api-service", local.name)
image = "nginx"
cpu = 128
memory = 256
is_attach_to_lb = true
port_mappings = [
{
# If a container has multiple ports, index 0 will be used for target group
host_port = 80
container_port = 80
protocol = "tcp"
}
]
entry_point = []
command = []
target_tracking_configuration = {
policy_type = "TargetTrackingScaling"
name = "cpu-average"
capacity = {
min_capacity = 1
max_capacity = 10
}
side_container = {
name = format("%s-nginx", local.name)
image = "tutum/dnsutils"
cpu = 128
memory = 256
port_mappings = [
{
host_port = 443
container_port = 443
protocol = "tcp"
},
]
}
}
environment_variables = {
main_container = {
THIS_IS_ENV = "ENV1",
THIS_IS_ENVV = "ENVV",
}
side_container = {
XXXX = "XXXX",
XXXXX = "XXXXX",
}
}
secret_variables = {
main_container = {
THIS_IS_SECRET = "1xxxxx",
THIS_IS_SECRETT = "2xxxxx",
scaling_behaviors = {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
target_value = 60
scale_in_cooldown = 180
scale_out_cooldown = 60
}
}

Expand Down
25 changes: 18 additions & 7 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ locals {
| 1 | 1 | use custom kms */
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

comparison_operators = {
">=" = "GreaterThanOrEqualToThreshold",
">" = "GreaterThanThreshold",
"<" = "LessThanThreshold",
"<=" = "LessThanOrEqualToThreshold",
}

tags = merge(
{
"Environment" = var.environment,
"Terraform" = "true"
"Module" = "terraform-aws-ecs-fargate-service"
},
var.tags
)
Expand Down Expand Up @@ -130,3 +124,20 @@ locals {
}
]
}

/* -------------------------------------------------------------------------- */
/* Auto Scaling */
/* -------------------------------------------------------------------------- */
locals {
is_created_aws_appautoscaling_target = var.target_tracking_configuration != {} || var.step_scaling_configuration != {}

is_target_tracking_scaling = var.target_tracking_configuration == {} ? false : true
is_contain_predefined_metric = local.is_target_tracking_scaling ? try(var.target_tracking_configuration["scaling_behaviors"]["predefined_metric_type"], null) != null : false

comparison_operators = {
">=" = "GreaterThanOrEqualToThreshold",
">" = "GreaterThanThreshold",
"<" = "LessThanThreshold",
"<=" = "LessThanOrEqualToThreshold",
}
}
Loading
Loading