Skip to content

Commit 0416aa7

Browse files
committed
refactor: remove ecs service declaration
Remove conditional ECS cluster creation. Refactor variables, outputs, and complete example to account for the changes.
1 parent 0ba2418 commit 0416aa7

File tree

9 files changed

+5
-231
lines changed

9 files changed

+5
-231
lines changed

.header.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# terraform-aws-ecs
22

3-
A Terraform module to create ECS Cluster and ECS Service that rely on self-managed EC2 instances.
3+
A Terraform module to create ECS Cluster that relies on self-managed EC2 instances.
44

55
## Notice
66

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- BEGIN_TF_DOCS -->
22
# terraform-aws-ecs
33

4-
A Terraform module to create ECS Cluster and ECS Service that rely on self-managed EC2 instances.
4+
A Terraform module to create ECS Cluster that relies on self-managed EC2 instances.
55

66
## Notice
77

@@ -22,7 +22,6 @@ No providers.
2222
| Name | Source | Version |
2323
|------|--------|---------|
2424
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | terraform-aws-modules/ecs/aws//modules/cluster | ~> 5.11.3 |
25-
| <a name="module_ecs_service"></a> [ecs\_service](#module\_ecs\_service) | terraform-aws-modules/ecs/aws//modules/service | ~> 5.11.3 |
2625

2726
## Resources
2827

@@ -32,20 +31,12 @@ No resources.
3231

3332
| Name | Description | Type | Default | Required |
3433
|------|-------------|------|---------|:--------:|
35-
| <a name="input_cluster_arn"></a> [cluster\_arn](#input\_cluster\_arn) | ARN of the ECS cluster under which the service will be created | `string` | `null` | no |
3634
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the ECS Cluster to create | `string` | `""` | no |
3735
| <a name="input_cluster_tags"></a> [cluster\_tags](#input\_cluster\_tags) | Resource Tags for ECS Cluster | `map(any)` | `{}` | no |
38-
| <a name="input_create_cluster"></a> [create\_cluster](#input\_create\_cluster) | Creates an ECS cluster | `bool` | `true` | no |
39-
| <a name="input_service_cpu"></a> [service\_cpu](#input\_service\_cpu) | CPU allocation for ECS task definitions | `number` | n/a | yes |
40-
| <a name="input_service_memory"></a> [service\_memory](#input\_service\_memory) | Memory allocation for ECS task definitions | `number` | n/a | yes |
41-
| <a name="input_service_name"></a> [service\_name](#input\_service\_name) | Name of the ECS Service | `string` | n/a | yes |
42-
| <a name="input_service_subnet_ids"></a> [service\_subnet\_ids](#input\_service\_subnet\_ids) | VPC subnet ids where the ECS services will be deployed | `list(string)` | n/a | yes |
43-
| <a name="input_service_tags"></a> [service\_tags](#input\_service\_tags) | Resource Tags for ECS Service | `map(any)` | `{}` | no |
4436

4537
## Outputs
4638

4739
| Name | Description |
4840
|------|-------------|
4941
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | ARN of the ECS Cluster |
50-
| <a name="output_service_id"></a> [service\_id](#output\_service\_id) | Identifier of the ECS Service |
5142
<!-- END_TF_DOCS -->

examples/complete/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ No inputs.
4949
| Name | Description |
5050
|------|-------------|
5151
| <a name="output_ecs_cluster_arn"></a> [ecs\_cluster\_arn](#output\_ecs\_cluster\_arn) | ARN of the ECS Cluster |
52-
| <a name="output_ecs_service_id"></a> [ecs\_service\_id](#output\_ecs\_service\_id) | Identifier of the ECS Service |
5352
| <a name="output_private_subnets"></a> [private\_subnets](#output\_private\_subnets) | Identifiers of the Private Subnets |
5453
| <a name="output_private_subnets_arns"></a> [private\_subnets\_arns](#output\_private\_subnets\_arns) | ARNs of the Private Subnets |
5554
| <a name="output_private_subnets_cidr_blocks"></a> [private\_subnets\_cidr\_blocks](#output\_private\_subnets\_cidr\_blocks) | CIDR Blocks of the Private Subnets |

examples/complete/main.tf

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ locals {
1919
# ECS Cluster
2020
cluster_name = "${local.name_prefix}my-cluster"
2121

22-
# ECS Service
23-
service_name = "${local.name_prefix}my-service"
24-
service_task_resource_allocation = {
25-
cpu = 512
26-
memory = 512
27-
}
28-
2922
default_tags = {
3023
Environment = "Dev"
3124
ManagedBy = "Terraform"
@@ -39,14 +32,7 @@ locals {
3932
module "ecs" {
4033
source = "../../"
4134

42-
# Cluster
4335
cluster_name = local.cluster_name
44-
45-
# Services
46-
service_name = local.service_name
47-
service_subnet_ids = flatten([module.vpc.private_subnets, module.vpc.public_subnets])
48-
service_cpu = local.service_task_resource_allocation.cpu
49-
service_memory = local.service_task_resource_allocation.memory
5036
}
5137

5238
################################################################################

examples/complete/outputs.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,3 @@ output "ecs_cluster_arn" {
5050
description = "ARN of the ECS Cluster"
5151
value = module.ecs.cluster_arn
5252
}
53-
54-
output "ecs_service_id" {
55-
description = "Identifier of the ECS Service"
56-
value = module.ecs.service_id
57-
}

main.tf

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,10 @@ module "ecs_cluster" {
66
source = "terraform-aws-modules/ecs/aws//modules/cluster"
77
version = "~> 5.11.3"
88

9-
count = var.create_cluster ? 1 : 0
10-
119
cluster_name = var.cluster_name
1210
default_capacity_provider_use_fargate = false
1311

1412
create_cloudwatch_log_group = false
1513

1614
tags = var.cluster_tags
1715
}
18-
19-
################################################################################
20-
# ECS Service
21-
################################################################################
22-
23-
module "ecs_service" {
24-
source = "terraform-aws-modules/ecs/aws//modules/service"
25-
version = "~> 5.11.3"
26-
27-
name = var.service_name
28-
cluster_arn = var.create_cluster ? module.ecs_cluster[0].arn : var.cluster_arn
29-
subnet_ids = var.service_subnet_ids
30-
31-
cpu = var.service_cpu
32-
memory = var.service_memory
33-
34-
launch_type = "EC2"
35-
requires_compatibilities = ["EC2"]
36-
37-
container_definitions = {
38-
demo = {
39-
image = "amazon/amazon-ecs-sample:latest"
40-
port_mappings = []
41-
42-
readonly_root_filesystem = false
43-
44-
enable_cloudwatch_logging = false
45-
create_cloudwatch_log_group = false
46-
}
47-
}
48-
49-
create_task_exec_policy = false
50-
create_security_group = false
51-
52-
enable_autoscaling = false
53-
autoscaling_policies = {}
54-
55-
tags = var.service_tags
56-
}

outputs.tf

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,5 @@
44

55
output "cluster_arn" {
66
description = "ARN of the ECS Cluster"
7-
value = var.create_cluster ? module.ecs_cluster[0].arn : var.cluster_arn
8-
}
9-
10-
##############################
11-
# ECS Service Outputs
12-
##############################
13-
14-
output "service_id" {
15-
description = "Identifier of the ECS Service"
16-
value = module.ecs_service.id
7+
value = module.ecs_cluster.arn
178
}

tests/unit_tests.tftest.hcl

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,6 @@
22
# ECS Cluster Tests
33
###########################################################
44

5-
run "do_not_create_cluster_when_false" {
6-
command = plan
7-
8-
module {
9-
source = "./"
10-
}
11-
12-
variables {
13-
create_cluster = false
14-
15-
service_name = "test-service"
16-
cluster_arn = "arn:aws:ecs:ap-south-1:471112575944:cluster/default"
17-
service_subnet_ids = ["subnet-0ba38303b254c0785"]
18-
service_cpu = 128
19-
service_memory = 256
20-
}
21-
22-
assert {
23-
condition = length(module.ecs_cluster) == 0
24-
error_message = "ECS cluster was created even after setting `create_cluster` to `false`"
25-
}
26-
}
27-
28-
run "create_cluster_when_true" {
29-
command = plan
30-
31-
module {
32-
source = "./"
33-
}
34-
35-
variables {
36-
create_cluster = true
37-
cluster_name = "test-cluster"
38-
39-
service_name = "test-service"
40-
service_subnet_ids = ["subnet-0ba38303b254c0785"]
41-
service_cpu = 128
42-
service_memory = 256
43-
}
44-
45-
assert {
46-
condition = length(module.ecs_cluster) == 1
47-
error_message = "ECS cluster was not created even after setting `create_cluster` to `true`"
48-
}
49-
}
50-
515
run "cluster_name_match" {
526
command = plan
537

@@ -56,44 +10,11 @@ run "cluster_name_match" {
5610
}
5711

5812
variables {
59-
create_cluster = true
60-
cluster_name = "test-cluster"
61-
62-
service_name = "test-service"
63-
service_subnet_ids = ["subnet-0ba38303b254c0785"]
64-
service_cpu = 128
65-
service_memory = 256
66-
}
67-
68-
assert {
69-
condition = module.ecs_cluster[0].name == var.cluster_name
70-
error_message = "ECS Cluster name mismatch after creation"
71-
}
72-
}
73-
74-
###########################################################
75-
# ECS Service Tests
76-
###########################################################
77-
78-
run "service_name_match" {
79-
command = plan
80-
81-
module {
82-
source = "./"
83-
}
84-
85-
variables {
86-
create_cluster = true
87-
cluster_name = "test-cluster"
88-
89-
service_name = "test-service"
90-
service_subnet_ids = ["subnet-0ba38303b254c0785"]
91-
service_cpu = 128
92-
service_memory = 256
13+
cluster_name = "test-cluster"
9314
}
9415

9516
assert {
96-
condition = module.ecs_service.name == var.service_name
17+
condition = module.ecs_cluster.name == var.cluster_name
9718
error_message = "ECS Cluster name mismatch after creation"
9819
}
9920
}

variables.tf

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,13 @@
22
# ECS Cluster Variables
33
#######################
44

5-
variable "create_cluster" {
6-
description = "Creates an ECS cluster"
7-
type = bool
8-
default = true
9-
}
10-
115
variable "cluster_name" {
126
description = "Name of the ECS Cluster to create"
137
type = string
14-
default = ""
158
}
169

1710
variable "cluster_tags" {
1811
description = "Resource Tags for ECS Cluster"
1912
type = map(any)
2013
default = {}
2114
}
22-
23-
#######################
24-
# ECS Service Variables
25-
#######################
26-
27-
variable "service_name" {
28-
description = "Name of the ECS Service"
29-
type = string
30-
}
31-
32-
variable "cluster_arn" {
33-
description = "ARN of the ECS cluster under which the service will be created"
34-
type = string
35-
default = null
36-
37-
validation {
38-
condition = var.cluster_arn == null || startswith(var.cluster_arn != null ? var.cluster_arn : "", "arn:")
39-
error_message = "Specified Cluster ARN must be a valid ARN starting with \"arn:\"."
40-
}
41-
}
42-
43-
variable "service_subnet_ids" {
44-
description = "VPC subnet ids where the ECS services will be deployed"
45-
type = list(string)
46-
47-
validation {
48-
condition = length(var.service_subnet_ids) > 0
49-
error_message = "Specified subnet ids must contain at least one valid subnet identifier."
50-
}
51-
52-
validation {
53-
condition = alltrue([for subnet_id in var.service_subnet_ids : startswith(subnet_id, "subnet-")])
54-
error_message = "Specified subnet ids must be valid subnet identifiers starting with \"subnet-\"."
55-
}
56-
}
57-
58-
variable "service_cpu" {
59-
description = "CPU allocation for ECS task definitions"
60-
type = number
61-
62-
validation {
63-
condition = var.service_cpu > 0
64-
error_message = "Specified CPU allocation must be a positive number."
65-
}
66-
}
67-
68-
variable "service_memory" {
69-
description = "Memory allocation for ECS task definitions"
70-
type = number
71-
72-
validation {
73-
condition = var.service_memory > 0
74-
error_message = "Specified Memory allocation must be a positive number."
75-
}
76-
}
77-
78-
variable "service_tags" {
79-
description = "Resource Tags for ECS Service"
80-
type = map(any)
81-
default = {}
82-
}

0 commit comments

Comments
 (0)