Skip to content

Commit 402001a

Browse files
committed
Creating new version of service. Moving load balancers to other module
1 parent a4fdf9f commit 402001a

File tree

6 files changed

+315
-409
lines changed

6 files changed

+315
-409
lines changed

README.md

Lines changed: 6 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -15,119 +15,20 @@ Check valid versions on:
1515
* Github Releases: <https://github.com/cn-terraform/terraform-aws-ecs-fargate-service/releases>
1616
* Terraform Module Registry: <https://registry.terraform.io/modules/cn-terraform/ecs-fargate-service/aws>
1717

18-
module "ecs-fargate-service" {
19-
source = "cn-terraform/ecs-fargate-service/aws"
20-
version = "1.0.10"
21-
name_preffix = var.name_preffix
22-
profile = var.profile
23-
region = var.region
24-
vpc_id = module.networking.vpc_id
25-
task_definition_arn = module.td.aws_ecs_task_definition_td_arn
26-
container_port = module.td.container_port
27-
ecs_cluster_name = module.ecs-cluster.aws_ecs_cluster_cluster_name
28-
ecs_cluster_arn = module.ecs-cluster.aws_ecs_cluster_cluster_arn
29-
private_subnets = module.networking.private_subnets_ids
30-
public_subnets = module.networking.public_subnets_ids
31-
}
32-
33-
Check the section "Other modules that you may need to use this module" for details about modules mentioned in the usage example.
34-
35-
## Input values
36-
37-
* name_preffix: Name preffix for resources on AWS.
38-
* profile: AWS API key credentials to use.
39-
* region: AWS Region the infrastructure is hosted in.
40-
* vpc_id: ID of the VPC.
41-
* task_definition_arn: (Required) The full ARN of the task definition that you want to run in your service.
42-
* ecs_cluster_name = Name of the ECS cluster.
43-
* ecs_cluster_arn: ARN of an ECS cluster.
44-
* subnets: The subnets associated with the task or service.
45-
* container_name: Name of the running container.
46-
* container_port: Port on which the container is listening.
47-
* desired_count: (Optional) The number of instances of the task definition to place and keep running. Defaults to 1.
48-
* platform_version: (Optional) The platform version on which to run your service. Defaults to LATEST. More information about Fargate platform versions can be found in the AWS ECS User Guide.
49-
* deployment_maximum_percent: (Optional) The upper limit (as a percentage of the service's desiredCount) of the number of running tasks that can be running in a service during a deployment.
50-
* deployment_minimum_healthy_percent: (Optional) The lower limit (as a percentage of the service's desiredCount) of the number of running tasks that must remain running and healthy in a service during a deployment.
51-
* enable_ecs_managed_tags: (Optional) Specifies whether to enable Amazon ECS managed tags for the tasks within the service.
52-
* propagate_tags: (Optional) Specifies whether to propagate the tags from the task definition or the service to the tasks. The valid values are SERVICE and TASK_DEFINITION. Default to SERVICE.
53-
* ordered_placement_strategy: (Optional) Service level strategy rules that are taken into consideration during task placement. List from top to bottom in order of precedence. The maximum number of ordered_placement_strategy blocks is 5. This is a list of maps where each map should contain "id" and "field".
54-
* health_check_grace_period_seconds: (Optional) Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 2147483647. Only valid for services configured to use load balancers.
55-
* health_check_path: (Optional) The destination for the health check request.
56-
* placement_constraints: (Optional) rules that are taken into consideration during task placement. Maximum number of placement_constraints is 10. This is a list of maps, where each map should contain "type" and "expression".
57-
* service_registries: (Optional) The service discovery registries for the service. The maximum number of service_registries blocks is 1. This is a map that should contain the following fields "registry_arn", "port", "container_port" and "container_name".
58-
* security_groups: (Optional) The security groups associated with the task or service. If you do not specify a security group, the default security group for the VPC is used.
59-
* assign_public_ip: (Optional) Assign a public IP address to the ENI (Fargate launch type only). Valid values are true or false. Default false.
60-
* lb_health_check_path: (Optional) Health check path for the Load Balancer
61-
* internal_lb: (Optional) Sets ECS service load balancer to internal and disables ECS service public ip assignment. Default false
62-
63-
## Output values
64-
65-
* aws_ecs_service_service_id: The Amazon Resource Name (ARN) that identifies the service.
66-
* aws_ecs_service_service_name: The name of the service.
67-
* aws_ecs_service_service_cluster: The Amazon Resource Name (ARN) of cluster which the service runs on.
68-
* aws_ecs_service_service_desired_count: The number of instances of the task definition.
69-
* lb_id: Load Balancer ID.
70-
* lb_arn: Load Balancer ARN.
71-
* lb_arn_suffix: Load Balancer ARN Suffix.
72-
* lb_dns_name: Load Balancer DNS Name.
73-
* lb_zone_id: Load Balancer Zone ID.
74-
* lb_sg_id: Load Balancer Security Group - The ID of the security group.
75-
* lb_sg_arn: Load Balancer Security Group - The ARN of the security group.
76-
* lb_sg_name: Load Balancer Security Group - The name of the security group.
77-
* lb_sg_description: Load Balancer Security Group - The description of the security group.
78-
* ecs_tasks_sg_id: ECS Tasks Security Group - The ID of the security group.
79-
* ecs_tasks_sg_arn: ECS Tasks Security Group - The ARN of the security group.
80-
* ecs_tasks_sg_name: ECS Tasks Security Group - The name of the security group.
81-
* ecs_tasks_sg_description: ECS Tasks Security Group - The description of the security group.
82-
8318
## Other modules that you may need to use this module
8419

85-
The networking module should look like this:
86-
87-
module "networking" {
88-
source = "cn-terraform/networking/aws"
89-
version = "2.0.3"
90-
name_preffix = var.name_preffix
91-
profile = var.profile
92-
region = var.region
93-
vpc_cidr_block = "192.168.0.0/16"
94-
availability_zones = [ "us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d" ]
95-
public_subnets_cidrs_per_availability_zone = [ "192.168.0.0/19", "192.168.32.0/19", "192.168.64.0/19", "192.168.96.0/19" ]
96-
private_subnets_cidrs_per_availability_zone = [ "192.168.128.0/19", "192.168.160.0/19", "192.168.192.0/19", "192.168.224.0/19" ]
97-
}
98-
99-
Check versions for this module on:
20+
The networking module:
10021
* Github Releases: <https://github.com/cn-terraform/terraform-aws-networking/releases>
10122
* Terraform Module Registry: <https://registry.terraform.io/modules/cn-terraform/networking/aws>
10223

103-
The ECS cluster module should look like this:
104-
105-
module "ecs-cluster" {
106-
source = "cn-terraform/ecs-cluster/aws"
107-
version = "1.0.2"
108-
name_preffix = var.name_preffix
109-
profile = var.profile
110-
region = var.region
111-
}
112-
113-
Check versions for this module on:
24+
The ECS cluster module:
11425
* Github Releases: <https://github.com/cn-terraform/terraform-aws-ecs-cluster/releases>
11526
* Terraform Module Registry: <https://registry.terraform.io/modules/cn-terraform/ecs-cluster/aws>
11627

117-
The task definition module should like this:
118-
119-
module "td" {
120-
source = "cn-terraform/ecs-fargate-task-definition/aws"
121-
version = "1.0.1"
122-
name_preffix = var.name_preffix
123-
profile = var.profile
124-
region = var.region
125-
container_name = "${var.name_preffix}-<NAME>"
126-
container_image = "<IMAGE_NAME>:<IMAGE_TAG>"
127-
container_port = <PORT>
128-
}
129-
130-
Check versions for this module on:
28+
The task definition:
13129
* Github Releases: <https://github.com/cn-terraform/terraform-aws-ecs-fargate-task-definition/releases>
13230
* Terraform Module Registry: <https://registry.terraform.io/modules/cn-terraform/ecs-fargate-task-definition/aws>
13331

32+
The ECS ALB module:
33+
* Github Releases: <https://github.com/cn-terraform/terraform-aws-ecs-alb/releases>
34+
* Terraform Module Registry: <https://registry.terraform.io/modules/cn-terraform/ecs-alb>

autoscaling.tf

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# AWS ECS Auto Scale Role
3+
# ---------------------------------------------------------------------------------------------------------------------
4+
resource "aws_iam_role" "ecs_autoscale_role" {
5+
name = "${var.name_preffix}-ecs-autoscale-role"
6+
assume_role_policy = file("${path.module}/files/iam/ecs_autoscale_iam_role.json")
7+
}
8+
9+
resource "aws_iam_role_policy" "ecs_autoscale_role_policy" {
10+
name = "${var.name_preffix}-ecs-autoscale-role-policy"
11+
role = aws_iam_role.ecs_autoscale_role.id
12+
policy = file(
13+
"${path.module}/files/iam/ecs_autoscale_iam_role_policy.json",
14+
)
15+
}
16+
17+
# ---------------------------------------------------------------------------------------------------------------------
18+
# AWS Auto Scaling - CloudWatch Alarm CPU High
19+
# ---------------------------------------------------------------------------------------------------------------------
20+
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
21+
alarm_name = "${var.name_preffix}-cpu-high"
22+
comparison_operator = "GreaterThanOrEqualToThreshold"
23+
evaluation_periods = "3"
24+
metric_name = "CPUUtilization"
25+
namespace = "AWS/ECS"
26+
period = "60"
27+
statistic = "Maximum"
28+
threshold = "85"
29+
dimensions = {
30+
ClusterName = var.ecs_cluster_name
31+
ServiceName = aws_ecs_service.service.name
32+
}
33+
alarm_actions = [aws_appautoscaling_policy.scale_up_policy.arn]
34+
}
35+
36+
# ---------------------------------------------------------------------------------------------------------------------
37+
# AWS Auto Scaling - CloudWatch Alarm CPU Low
38+
# ---------------------------------------------------------------------------------------------------------------------
39+
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
40+
alarm_name = "${var.name_preffix}-cpu-low"
41+
comparison_operator = "LessThanOrEqualToThreshold"
42+
evaluation_periods = "3"
43+
metric_name = "CPUUtilization"
44+
namespace = "AWS/ECS"
45+
period = "60"
46+
statistic = "Average"
47+
threshold = "10"
48+
dimensions = {
49+
ClusterName = var.ecs_cluster_name
50+
ServiceName = aws_ecs_service.service.name
51+
}
52+
alarm_actions = [aws_appautoscaling_policy.scale_down_policy.arn]
53+
}
54+
55+
# ---------------------------------------------------------------------------------------------------------------------
56+
# AWS Auto Scaling - Scaling Up Policy
57+
# ---------------------------------------------------------------------------------------------------------------------
58+
resource "aws_appautoscaling_policy" "scale_up_policy" {
59+
name = "${var.name_preffix}-scale-up-policy"
60+
depends_on = [aws_appautoscaling_target.scale_target]
61+
service_namespace = "ecs"
62+
resource_id = "service/${var.ecs_cluster_name}/${aws_ecs_service.service.name}"
63+
scalable_dimension = "ecs:service:DesiredCount"
64+
step_scaling_policy_configuration {
65+
adjustment_type = "ChangeInCapacity"
66+
cooldown = 60
67+
metric_aggregation_type = "Maximum"
68+
step_adjustment {
69+
metric_interval_lower_bound = 0
70+
scaling_adjustment = 1
71+
}
72+
}
73+
}
74+
75+
# ---------------------------------------------------------------------------------------------------------------------
76+
# AWS Auto Scaling - Scaling Down Policy
77+
# ---------------------------------------------------------------------------------------------------------------------
78+
resource "aws_appautoscaling_policy" "scale_down_policy" {
79+
name = "${var.name_preffix}-scale-down-policy"
80+
depends_on = [aws_appautoscaling_target.scale_target]
81+
service_namespace = "ecs"
82+
resource_id = "service/${var.ecs_cluster_name}/${aws_ecs_service.service.name}"
83+
scalable_dimension = "ecs:service:DesiredCount"
84+
step_scaling_policy_configuration {
85+
adjustment_type = "ChangeInCapacity"
86+
cooldown = 60
87+
metric_aggregation_type = "Maximum"
88+
step_adjustment {
89+
metric_interval_lower_bound = 0
90+
scaling_adjustment = -1
91+
}
92+
}
93+
}
94+
95+
# ---------------------------------------------------------------------------------------------------------------------
96+
# AWS Auto Scaling - Scaling Target
97+
# ---------------------------------------------------------------------------------------------------------------------
98+
resource "aws_appautoscaling_target" "scale_target" {
99+
service_namespace = "ecs"
100+
resource_id = "service/${var.ecs_cluster_name}/${aws_ecs_service.service.name}"
101+
scalable_dimension = "ecs:service:DesiredCount"
102+
role_arn = aws_iam_role.ecs_autoscale_role.arn
103+
min_capacity = 1
104+
max_capacity = 5
105+
}

0 commit comments

Comments
 (0)