Skip to content

Commit 44c1320

Browse files
committed
Fixing issues
1 parent bf618eb commit 44c1320

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Check valid versions on:
1919
vpc_id = module.networking.vpc_id
2020
task_definition_arn = module.td.aws_ecs_task_definition_td_arn
2121
container_port = module.td.container_port
22+
ecs_cluster_name = module.ecs-cluster.aws_ecs_cluster_cluster_name
2223
ecs_cluster_arn = module.ecs-cluster.aws_ecs_cluster_cluster_arn
2324
subnets = module.networking.private_subnets_ids
2425
}
@@ -32,8 +33,10 @@ Check the section "Other modules that you may need to use this module" for detai
3233
* region: AWS Region the infrastructure is hosted in.
3334
* vpc_id: ID of the VPC.
3435
* task_definition_arn: (Required) The full ARN of the task definition that you want to run in your service.
36+
* ecs_cluster_name = Name of the ECS cluster.
3537
* ecs_cluster_arn: ARN of an ECS cluster.
3638
* subnets: The subnets associated with the task or service.
39+
* container_name: Name of the running container.
3740
* container_port: Port on which the container is listening.
3841
* desired_count: (Optional) The number of instances of the task definition to place and keep running. Defaults to 1.
3942
* 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.
@@ -92,7 +95,7 @@ The ECS cluster module should look like this:
9295

9396
module "ecs-cluster":
9497
source = "jnonino/ecs-cluster/aws"
95-
version = "1.0.0"
98+
version = "1.0.1"
9699
name_preffix = var.name_preffix
97100
profile = var.profile
98101
region = var.region

main.tf

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ resource "aws_kms_key" "encryption_key" {
1515
enable_key_rotation = true
1616
}
1717

18-
# ---------------------------------------------------------------------------------------------------------------------
19-
# AWS Cloudwatch Logs
20-
# ---------------------------------------------------------------------------------------------------------------------
21-
module "aws_cw_logs" {
22-
source = "jnonino/cloudwatch-logs/aws"
23-
version = "1.0.2"
24-
logs_path = local.log_options["awslogs-group"]
25-
profile = var.profile
26-
region = var.region
27-
}
28-
2918
# ---------------------------------------------------------------------------------------------------------------------
3019
# AWS SECURITY GROUP - ECS Tasks, allow traffic only from Load Balancer
3120
# ---------------------------------------------------------------------------------------------------------------------
@@ -65,10 +54,30 @@ resource "aws_ecs_service" "service" {
6554
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
6655
enable_ecs_managed_tags = var.enable_ecs_managed_tags
6756
propagate_tags = var.propagate_tags
68-
ordered_placement_strategy = var.ordered_placement_strategy
57+
dynamic "ordered_placement_strategy" {
58+
for_each = var.ordered_placement_strategy
59+
content {
60+
type = ordered_placement_strategy.value.type
61+
field = lookup(ordered_placement_strategy.value, "field", null)
62+
}
63+
}
6964
health_check_grace_period_seconds = var.health_check_grace_period_seconds
70-
placement_constraints = var.placement_constraints
71-
service_registries = var.service_registries
65+
dynamic "placement_constraints" {
66+
for_each = var.placement_constraints
67+
content {
68+
expression = lookup(placement_constraints.value, "expression", null)
69+
type = placement_constraints.value.type
70+
}
71+
}
72+
dynamic "service_registries" {
73+
for_each = var.service_registries
74+
content {
75+
registry_arn = service_registries.value.registry_arn
76+
port = lookup(service_registries.value, "port", null)
77+
container_name = lookup(service_registries.value, "container_name", null)
78+
container_port = lookup(service_registries.value, "container_port", null)
79+
}
80+
}
7281
network_configuration {
7382
security_groups =[aws_security_group.ecs_tasks_sg.id]
7483
subnets = var.subnets
@@ -110,7 +119,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu_high" {
110119
statistic = "Maximum"
111120
threshold = "85"
112121
dimensions = {
113-
ClusterName = aws_ecs_cluster.cluster.name
122+
ClusterName = var.ecs_cluster_name
114123
ServiceName = aws_ecs_service.service.name
115124
}
116125
alarm_actions = [aws_appautoscaling_policy.scale_up_policy.arn]
@@ -129,7 +138,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu_low" {
129138
statistic = "Average"
130139
threshold = "10"
131140
dimensions = {
132-
ClusterName = aws_ecs_cluster.cluster.name
141+
ClusterName = var.ecs_cluster_name
133142
ServiceName = aws_ecs_service.service.name
134143
}
135144
alarm_actions = [aws_appautoscaling_policy.scale_down_policy.arn]
@@ -142,7 +151,7 @@ resource "aws_appautoscaling_policy" "scale_up_policy" {
142151
name = "${var.name_preffix}-scale-up-policy"
143152
depends_on = [aws_appautoscaling_target.scale_target]
144153
service_namespace = "ecs"
145-
resource_id = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
154+
resource_id = "service/${var.ecs_cluster_name}/${aws_ecs_service.service.name}"
146155
scalable_dimension = "ecs:service:DesiredCount"
147156
step_scaling_policy_configuration {
148157
adjustment_type = "ChangeInCapacity"
@@ -162,7 +171,7 @@ resource "aws_appautoscaling_policy" "scale_down_policy" {
162171
name = "${var.name_preffix}-scale-down-policy"
163172
depends_on = [aws_appautoscaling_target.scale_target]
164173
service_namespace = "ecs"
165-
resource_id = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
174+
resource_id = "service/${var.ecs_cluster_name}/${aws_ecs_service.service.name}"
166175
scalable_dimension = "ecs:service:DesiredCount"
167176
step_scaling_policy_configuration {
168177
adjustment_type = "ChangeInCapacity"
@@ -180,7 +189,7 @@ resource "aws_appautoscaling_policy" "scale_down_policy" {
180189
# ---------------------------------------------------------------------------------------------------------------------
181190
resource "aws_appautoscaling_target" "scale_target" {
182191
service_namespace = "ecs"
183-
resource_id = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
192+
resource_id = "service/${var.ecs_cluster_name}/${aws_ecs_service.service.name}"
184193
scalable_dimension = "ecs:service:DesiredCount"
185194
role_arn = aws_iam_role.ecs_autoscale_role.arn
186195
min_capacity = 1

variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ variable "task_definition_arn" {
3030
description = "(Required) The full ARN of the task definition that you want to run in your service."
3131
}
3232

33+
variable "ecs_cluster_name" {
34+
description = "Name of the ECS cluster"
35+
}
36+
3337
variable "ecs_cluster_arn" {
3438
description = "ARN of an ECS cluster"
3539
}
@@ -39,6 +43,10 @@ variable "subnets" {
3943
type = list
4044
}
4145

46+
variable "container_name" {
47+
description = "Name of the running container"
48+
}
49+
4250
variable "container_port" {
4351
description = "Port on which the container is listening"
4452
}

0 commit comments

Comments
 (0)