Skip to content

Commit

Permalink
refactor: remove ecs service declaration
Browse files Browse the repository at this point in the history
Remove conditional ECS cluster creation. Refactor variables, outputs, and complete example to account for the changes.
  • Loading branch information
saumitra-dev committed Jul 15, 2024
1 parent 0ba2418 commit 0416aa7
Show file tree
Hide file tree
Showing 9 changed files with 5 additions and 231 deletions.
2 changes: 1 addition & 1 deletion .header.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# terraform-aws-ecs

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

## Notice

Expand Down
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- BEGIN_TF_DOCS -->
# terraform-aws-ecs

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

## Notice

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

## Resources

Expand All @@ -32,20 +31,12 @@ No resources.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <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 |
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the ECS Cluster to create | `string` | `""` | no |
| <a name="input_cluster_tags"></a> [cluster\_tags](#input\_cluster\_tags) | Resource Tags for ECS Cluster | `map(any)` | `{}` | no |
| <a name="input_create_cluster"></a> [create\_cluster](#input\_create\_cluster) | Creates an ECS cluster | `bool` | `true` | no |
| <a name="input_service_cpu"></a> [service\_cpu](#input\_service\_cpu) | CPU allocation for ECS task definitions | `number` | n/a | yes |
| <a name="input_service_memory"></a> [service\_memory](#input\_service\_memory) | Memory allocation for ECS task definitions | `number` | n/a | yes |
| <a name="input_service_name"></a> [service\_name](#input\_service\_name) | Name of the ECS Service | `string` | n/a | yes |
| <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 |
| <a name="input_service_tags"></a> [service\_tags](#input\_service\_tags) | Resource Tags for ECS Service | `map(any)` | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | ARN of the ECS Cluster |
| <a name="output_service_id"></a> [service\_id](#output\_service\_id) | Identifier of the ECS Service |
<!-- END_TF_DOCS -->
1 change: 0 additions & 1 deletion examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ No inputs.
| Name | Description |
|------|-------------|
| <a name="output_ecs_cluster_arn"></a> [ecs\_cluster\_arn](#output\_ecs\_cluster\_arn) | ARN of the ECS Cluster |
| <a name="output_ecs_service_id"></a> [ecs\_service\_id](#output\_ecs\_service\_id) | Identifier of the ECS Service |
| <a name="output_private_subnets"></a> [private\_subnets](#output\_private\_subnets) | Identifiers of the Private Subnets |
| <a name="output_private_subnets_arns"></a> [private\_subnets\_arns](#output\_private\_subnets\_arns) | ARNs of the Private Subnets |
| <a name="output_private_subnets_cidr_blocks"></a> [private\_subnets\_cidr\_blocks](#output\_private\_subnets\_cidr\_blocks) | CIDR Blocks of the Private Subnets |
Expand Down
14 changes: 0 additions & 14 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ locals {
# ECS Cluster
cluster_name = "${local.name_prefix}my-cluster"

# ECS Service
service_name = "${local.name_prefix}my-service"
service_task_resource_allocation = {
cpu = 512
memory = 512
}

default_tags = {
Environment = "Dev"
ManagedBy = "Terraform"
Expand All @@ -39,14 +32,7 @@ locals {
module "ecs" {
source = "../../"

# Cluster
cluster_name = local.cluster_name

# Services
service_name = local.service_name
service_subnet_ids = flatten([module.vpc.private_subnets, module.vpc.public_subnets])
service_cpu = local.service_task_resource_allocation.cpu
service_memory = local.service_task_resource_allocation.memory
}

################################################################################
Expand Down
5 changes: 0 additions & 5 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,3 @@ output "ecs_cluster_arn" {
description = "ARN of the ECS Cluster"
value = module.ecs.cluster_arn
}

output "ecs_service_id" {
description = "Identifier of the ECS Service"
value = module.ecs.service_id
}
41 changes: 0 additions & 41 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,10 @@ module "ecs_cluster" {
source = "terraform-aws-modules/ecs/aws//modules/cluster"
version = "~> 5.11.3"

count = var.create_cluster ? 1 : 0

cluster_name = var.cluster_name
default_capacity_provider_use_fargate = false

create_cloudwatch_log_group = false

tags = var.cluster_tags
}

################################################################################
# ECS Service
################################################################################

module "ecs_service" {
source = "terraform-aws-modules/ecs/aws//modules/service"
version = "~> 5.11.3"

name = var.service_name
cluster_arn = var.create_cluster ? module.ecs_cluster[0].arn : var.cluster_arn
subnet_ids = var.service_subnet_ids

cpu = var.service_cpu
memory = var.service_memory

launch_type = "EC2"
requires_compatibilities = ["EC2"]

container_definitions = {
demo = {
image = "amazon/amazon-ecs-sample:latest"
port_mappings = []

readonly_root_filesystem = false

enable_cloudwatch_logging = false
create_cloudwatch_log_group = false
}
}

create_task_exec_policy = false
create_security_group = false

enable_autoscaling = false
autoscaling_policies = {}

tags = var.service_tags
}
11 changes: 1 addition & 10 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,5 @@

output "cluster_arn" {
description = "ARN of the ECS Cluster"
value = var.create_cluster ? module.ecs_cluster[0].arn : var.cluster_arn
}

##############################
# ECS Service Outputs
##############################

output "service_id" {
description = "Identifier of the ECS Service"
value = module.ecs_service.id
value = module.ecs_cluster.arn
}
83 changes: 2 additions & 81 deletions tests/unit_tests.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,6 @@
# ECS Cluster Tests
###########################################################

run "do_not_create_cluster_when_false" {
command = plan

module {
source = "./"
}

variables {
create_cluster = false

service_name = "test-service"
cluster_arn = "arn:aws:ecs:ap-south-1:471112575944:cluster/default"
service_subnet_ids = ["subnet-0ba38303b254c0785"]
service_cpu = 128
service_memory = 256
}

assert {
condition = length(module.ecs_cluster) == 0
error_message = "ECS cluster was created even after setting `create_cluster` to `false`"
}
}

run "create_cluster_when_true" {
command = plan

module {
source = "./"
}

variables {
create_cluster = true
cluster_name = "test-cluster"

service_name = "test-service"
service_subnet_ids = ["subnet-0ba38303b254c0785"]
service_cpu = 128
service_memory = 256
}

assert {
condition = length(module.ecs_cluster) == 1
error_message = "ECS cluster was not created even after setting `create_cluster` to `true`"
}
}

run "cluster_name_match" {
command = plan

Expand All @@ -56,44 +10,11 @@ run "cluster_name_match" {
}

variables {
create_cluster = true
cluster_name = "test-cluster"

service_name = "test-service"
service_subnet_ids = ["subnet-0ba38303b254c0785"]
service_cpu = 128
service_memory = 256
}

assert {
condition = module.ecs_cluster[0].name == var.cluster_name
error_message = "ECS Cluster name mismatch after creation"
}
}

###########################################################
# ECS Service Tests
###########################################################

run "service_name_match" {
command = plan

module {
source = "./"
}

variables {
create_cluster = true
cluster_name = "test-cluster"

service_name = "test-service"
service_subnet_ids = ["subnet-0ba38303b254c0785"]
service_cpu = 128
service_memory = 256
cluster_name = "test-cluster"
}

assert {
condition = module.ecs_service.name == var.service_name
condition = module.ecs_cluster.name == var.cluster_name
error_message = "ECS Cluster name mismatch after creation"
}
}
68 changes: 0 additions & 68 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,13 @@
# ECS Cluster Variables
#######################

variable "create_cluster" {
description = "Creates an ECS cluster"
type = bool
default = true
}

variable "cluster_name" {
description = "Name of the ECS Cluster to create"
type = string
default = ""
}

variable "cluster_tags" {
description = "Resource Tags for ECS Cluster"
type = map(any)
default = {}
}

#######################
# ECS Service Variables
#######################

variable "service_name" {
description = "Name of the ECS Service"
type = string
}

variable "cluster_arn" {
description = "ARN of the ECS cluster under which the service will be created"
type = string
default = null

validation {
condition = var.cluster_arn == null || startswith(var.cluster_arn != null ? var.cluster_arn : "", "arn:")
error_message = "Specified Cluster ARN must be a valid ARN starting with \"arn:\"."
}
}

variable "service_subnet_ids" {
description = "VPC subnet ids where the ECS services will be deployed"
type = list(string)

validation {
condition = length(var.service_subnet_ids) > 0
error_message = "Specified subnet ids must contain at least one valid subnet identifier."
}

validation {
condition = alltrue([for subnet_id in var.service_subnet_ids : startswith(subnet_id, "subnet-")])
error_message = "Specified subnet ids must be valid subnet identifiers starting with \"subnet-\"."
}
}

variable "service_cpu" {
description = "CPU allocation for ECS task definitions"
type = number

validation {
condition = var.service_cpu > 0
error_message = "Specified CPU allocation must be a positive number."
}
}

variable "service_memory" {
description = "Memory allocation for ECS task definitions"
type = number

validation {
condition = var.service_memory > 0
error_message = "Specified Memory allocation must be a positive number."
}
}

variable "service_tags" {
description = "Resource Tags for ECS Service"
type = map(any)
default = {}
}

0 comments on commit 0416aa7

Please sign in to comment.