Skip to content

Commit

Permalink
Fix CodeDeploy compatibility issues / Adds additional load balancer f…
Browse files Browse the repository at this point in the history
…eature / Fixes tests (#97)

Co-authored-by: Jared Darling <jdarling@aimconsulting.com>
  • Loading branch information
JaredDarling and jdarling-aim authored Oct 2, 2023
1 parent dbd4bf1 commit 03ccda2
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 60 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ In order to run all checks at any point run the following command:

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

## Resources

Expand Down
86 changes: 42 additions & 44 deletions examples/test/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 47 additions & 8 deletions examples/test/main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
locals {
public_subnet_ids = [for s in module.base-network.public_subnets : s.id]
private_subnet_ids = [for s in module.base-network.private_subnets : s.id]
}

module "cluster" {
source = "cn-terraform/ecs-cluster/aws"
name = "test-cluster"
}

module "base-network" {
source = "cn-terraform/networking/aws"
name_prefix = "test-networking"
vpc_cidr_block = "192.168.0.0/16"
availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"]
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"]
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"]
source = "cn-terraform/networking/aws"

cidr_block = "192.168.0.0/16"

vpc_additional_tags = {
vpc_tag1 = "tag1",
vpc_tag2 = "tag2",
}

public_subnets = {
first_public_subnet = {
availability_zone = "us-east-1a"
cidr_block = "192.168.0.0/19"
}
second_public_subnet = {
availability_zone = "us-east-1b"
cidr_block = "192.168.32.0/19"
}
}

public_subnets_additional_tags = {
public_subnet_tag1 = "tag1",
public_subnet_tag2 = "tag2",
}

private_subnets = {
first_private_subnet = {
availability_zone = "us-east-1a"
cidr_block = "192.168.128.0/19"
}
second_private_subnet = {
availability_zone = "us-east-1b"
cidr_block = "192.168.160.0/19"
}
}

private_subnets_additional_tags = {
private_subnet_tag1 = "tag1",
private_subnet_tag2 = "tag2",
}
}

module "td" {
Expand All @@ -25,8 +64,8 @@ module "service" {
vpc_id = module.base-network.vpc_id
ecs_cluster_arn = module.cluster.aws_ecs_cluster_cluster_arn
task_definition_arn = module.td.aws_ecs_task_definition_td_arn
public_subnets = module.base-network.public_subnets_ids
private_subnets = module.base-network.private_subnets_ids
public_subnets = local.public_subnet_ids
private_subnets = local.private_subnet_ids
container_name = "test"
ecs_cluster_name = module.cluster.aws_ecs_cluster_cluster_name
}
2 changes: 2 additions & 0 deletions examples/test/mock_provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ provider "aws" {
cloudformation = "http://localstack:4566"
cloudwatch = "http://localstack:4566"
dynamodb = "http://localstack:4566"
ec2 = "http://localstack:4566"
ecs = "http://localstack:4566" #<--Requires Pro version
es = "http://localstack:4566"
firehose = "http://localstack:4566"
iam = "http://localstack:4566"
Expand Down
24 changes: 21 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ module "ecs-alb" {
count = var.custom_lb_arn == null ? 1 : 0

source = "cn-terraform/ecs-alb/aws"
version = "1.0.31"

version = "1.0.32"

name_prefix = var.name_prefix
vpc_id = var.vpc_id
Expand Down Expand Up @@ -95,6 +94,15 @@ resource "aws_ecs_service" "service" {
container_port = load_balancer.value
}
}
dynamic "load_balancer" {
for_each = var.additional_lbs
content {
target_group_arn = load_balancer.value.target_group_arn
container_name = var.container_name
container_port = load_balancer.value.container_port
}
}

network_configuration {
security_groups = concat([aws_security_group.ecs_tasks_sg.id], var.security_groups)
subnets = var.assign_public_ip ? var.public_subnets : var.private_subnets
Expand Down Expand Up @@ -135,13 +143,23 @@ resource "aws_ecs_service" "service" {
container_port = lookup(service_registries.value, "container_port", null)
}
}
task_definition = var.task_definition_arn
#When deployment_controller is EXTERNAL, task_definition must not be used
task_definition = lookup(one(var.deployment_controller[*]), "type", "ECS") != "EXTERNAL" ? var.task_definition_arn : null

tags = merge(
var.tags,
{
Name = "${var.name_prefix}-ecs-tasks-sg"
},
)

lifecycle {
ignore_changes = [
desired_count, #Can be changed by autoscaling
task_definition, #Can be changed by deployments (CodeDeploy)
deployment_circuit_breaker
]
}
}

#------------------------------------------------------------------------------
Expand Down
25 changes: 22 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ variable "ordered_placement_strategy" {
}

variable "deployment_controller" {
description = "(Optional) Deployment controller"
description = "(Optional) Deployment controller default: 'ECS'"
type = list(any)
default = []
default = [
{
type = "ECS"
}
]
}

variable "placement_constraints" {
Expand All @@ -102,7 +106,9 @@ variable "service_registries" {
}

variable "task_definition_arn" {
description = "(Required) The full ARN of the task definition that you want to run in your service."
description = "(Optional) The full ARN of the task definition that you want to run in your service."
default = ""
type = string
}

variable "force_new_deployment" {
Expand Down Expand Up @@ -224,6 +230,19 @@ variable "custom_lb_arn" {
default = null
}

variable "additional_lbs" {
default = {}
description = "Additional load balancers to add to ECS service"
type = map(object
(
{
target_group_arn = string
container_port = number
}
)
)
}

variable "lb_internal" {
description = "(Optional) If true, the LB will be internal."
type = bool
Expand Down

0 comments on commit 03ccda2

Please sign in to comment.