Skip to content

Commit 03ccda2

Browse files
Fix CodeDeploy compatibility issues / Adds additional load balancer feature / Fixes tests (#97)
Co-authored-by: Jared Darling <jdarling@aimconsulting.com>
1 parent dbd4bf1 commit 03ccda2

File tree

6 files changed

+136
-60
lines changed

6 files changed

+136
-60
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ In order to run all checks at any point run the following command:
6666

6767
| Name | Source | Version |
6868
|------|--------|---------|
69-
| <a name="module_ecs-alb"></a> [ecs-alb](#module\_ecs-alb) | cn-terraform/ecs-alb/aws | 1.0.28 |
70-
| <a name="module_ecs-autoscaling"></a> [ecs-autoscaling](#module\_ecs-autoscaling) | cn-terraform/ecs-service-autoscaling/aws | 1.0.6 |
69+
| <a name="module_ecs-alb"></a> [ecs-alb](#module\_ecs-alb) | cn-terraform/ecs-alb/aws | 1.0.32 |
70+
| <a name="module_ecs-autoscaling"></a> [ecs-autoscaling](#module\_ecs-autoscaling) | cn-terraform/ecs-service-autoscaling/aws | 1.0.6 |
7171

7272
## Resources
7373

examples/test/.terraform.lock.hcl

+42-44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/test/main.tf

+47-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
1+
locals {
2+
public_subnet_ids = [for s in module.base-network.public_subnets : s.id]
3+
private_subnet_ids = [for s in module.base-network.private_subnets : s.id]
4+
}
5+
16
module "cluster" {
27
source = "cn-terraform/ecs-cluster/aws"
38
name = "test-cluster"
49
}
510

611
module "base-network" {
7-
source = "cn-terraform/networking/aws"
8-
name_prefix = "test-networking"
9-
vpc_cidr_block = "192.168.0.0/16"
10-
availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"]
11-
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"]
12-
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"]
12+
source = "cn-terraform/networking/aws"
13+
14+
cidr_block = "192.168.0.0/16"
15+
16+
vpc_additional_tags = {
17+
vpc_tag1 = "tag1",
18+
vpc_tag2 = "tag2",
19+
}
20+
21+
public_subnets = {
22+
first_public_subnet = {
23+
availability_zone = "us-east-1a"
24+
cidr_block = "192.168.0.0/19"
25+
}
26+
second_public_subnet = {
27+
availability_zone = "us-east-1b"
28+
cidr_block = "192.168.32.0/19"
29+
}
30+
}
31+
32+
public_subnets_additional_tags = {
33+
public_subnet_tag1 = "tag1",
34+
public_subnet_tag2 = "tag2",
35+
}
36+
37+
private_subnets = {
38+
first_private_subnet = {
39+
availability_zone = "us-east-1a"
40+
cidr_block = "192.168.128.0/19"
41+
}
42+
second_private_subnet = {
43+
availability_zone = "us-east-1b"
44+
cidr_block = "192.168.160.0/19"
45+
}
46+
}
47+
48+
private_subnets_additional_tags = {
49+
private_subnet_tag1 = "tag1",
50+
private_subnet_tag2 = "tag2",
51+
}
1352
}
1453

1554
module "td" {
@@ -25,8 +64,8 @@ module "service" {
2564
vpc_id = module.base-network.vpc_id
2665
ecs_cluster_arn = module.cluster.aws_ecs_cluster_cluster_arn
2766
task_definition_arn = module.td.aws_ecs_task_definition_td_arn
28-
public_subnets = module.base-network.public_subnets_ids
29-
private_subnets = module.base-network.private_subnets_ids
67+
public_subnets = local.public_subnet_ids
68+
private_subnets = local.private_subnet_ids
3069
container_name = "test"
3170
ecs_cluster_name = module.cluster.aws_ecs_cluster_cluster_name
3271
}

examples/test/mock_provider.tf

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ provider "aws" {
2424
cloudformation = "http://localstack:4566"
2525
cloudwatch = "http://localstack:4566"
2626
dynamodb = "http://localstack:4566"
27+
ec2 = "http://localstack:4566"
28+
ecs = "http://localstack:4566" #<--Requires Pro version
2729
es = "http://localstack:4566"
2830
firehose = "http://localstack:4566"
2931
iam = "http://localstack:4566"

main.tf

+21-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module "ecs-alb" {
55
count = var.custom_lb_arn == null ? 1 : 0
66

77
source = "cn-terraform/ecs-alb/aws"
8-
version = "1.0.31"
9-
8+
version = "1.0.32"
109

1110
name_prefix = var.name_prefix
1211
vpc_id = var.vpc_id
@@ -95,6 +94,15 @@ resource "aws_ecs_service" "service" {
9594
container_port = load_balancer.value
9695
}
9796
}
97+
dynamic "load_balancer" {
98+
for_each = var.additional_lbs
99+
content {
100+
target_group_arn = load_balancer.value.target_group_arn
101+
container_name = var.container_name
102+
container_port = load_balancer.value.container_port
103+
}
104+
}
105+
98106
network_configuration {
99107
security_groups = concat([aws_security_group.ecs_tasks_sg.id], var.security_groups)
100108
subnets = var.assign_public_ip ? var.public_subnets : var.private_subnets
@@ -135,13 +143,23 @@ resource "aws_ecs_service" "service" {
135143
container_port = lookup(service_registries.value, "container_port", null)
136144
}
137145
}
138-
task_definition = var.task_definition_arn
146+
#When deployment_controller is EXTERNAL, task_definition must not be used
147+
task_definition = lookup(one(var.deployment_controller[*]), "type", "ECS") != "EXTERNAL" ? var.task_definition_arn : null
148+
139149
tags = merge(
140150
var.tags,
141151
{
142152
Name = "${var.name_prefix}-ecs-tasks-sg"
143153
},
144154
)
155+
156+
lifecycle {
157+
ignore_changes = [
158+
desired_count, #Can be changed by autoscaling
159+
task_definition, #Can be changed by deployments (CodeDeploy)
160+
deployment_circuit_breaker
161+
]
162+
}
145163
}
146164

147165
#------------------------------------------------------------------------------

variables.tf

+22-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ variable "ordered_placement_strategy" {
7474
}
7575

7676
variable "deployment_controller" {
77-
description = "(Optional) Deployment controller"
77+
description = "(Optional) Deployment controller default: 'ECS'"
7878
type = list(any)
79-
default = []
79+
default = [
80+
{
81+
type = "ECS"
82+
}
83+
]
8084
}
8185

8286
variable "placement_constraints" {
@@ -102,7 +106,9 @@ variable "service_registries" {
102106
}
103107

104108
variable "task_definition_arn" {
105-
description = "(Required) The full ARN of the task definition that you want to run in your service."
109+
description = "(Optional) The full ARN of the task definition that you want to run in your service."
110+
default = ""
111+
type = string
106112
}
107113

108114
variable "force_new_deployment" {
@@ -224,6 +230,19 @@ variable "custom_lb_arn" {
224230
default = null
225231
}
226232

233+
variable "additional_lbs" {
234+
default = {}
235+
description = "Additional load balancers to add to ECS service"
236+
type = map(object
237+
(
238+
{
239+
target_group_arn = string
240+
container_port = number
241+
}
242+
)
243+
)
244+
}
245+
227246
variable "lb_internal" {
228247
description = "(Optional) If true, the LB will be internal."
229248
type = bool

0 commit comments

Comments
 (0)