Skip to content

Commit b25e974

Browse files
authored
Add aws_ecs_task_ignore_definition (#95)
* Add aws_ecs_task_ignore_definition * Using ecs_task defaults * Fix ECS def'n * Adding data-source for elb zone
1 parent 3de8c37 commit b25e974

File tree

8 files changed

+81
-6
lines changed

8 files changed

+81
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ The following inputs can be used as `step.with` keys
431431
| `aws_ecs_service_launch_type`| String | Configuration type. Could be `EC2`, `FARGATE` or `EXTERNAL`. Defaults to `FARGATE`. |
432432
| `aws_ecs_task_type`| String | Configuration type. Could be `EC2`, `FARGATE` or empty. Will default to `aws_ecs_service_launch_type` if none defined. (Blank if `EXTERNAL`). |
433433
| `aws_ecs_task_name`| String | Elastic Container Service task name. If task is defined with a JSON file, should be the same as the container name. |
434+
| `aws_ecs_task_ignore_definition`| Boolean | Toggle to ignore task definition changes after first deployment. Useful when using external tools to manage the task definition. Default: `false`. |
434435
| `aws_ecs_task_execution_role`| String | Elastic Container Service task execution role name from IAM. Defaults to `ecsTaskExecutionRole`. |
435436
| `aws_ecs_task_json_definition_file`| String | Name of the json file containing task definition. Overrides every other input. |
436437
| `aws_ecs_task_network_mode`| String | Network type to use in task definition. One of `none`, `bridge`, `awsvpc`, and `host`. |

action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,9 @@ inputs:
808808
aws_ecs_task_name:
809809
description: 'Elastic Container Service task name'
810810
required: false
811+
aws_ecs_task_ignore_definition:
812+
description: 'Toggle to ignore task definition changes after first deployment. Useful when using external tools to manage the task definition.'
813+
required: false
811814
aws_ecs_task_execution_role:
812815
description: 'Elastic Container Service task execution role name from IAM. Defaults to "ecsTaskExecutionRole"'
813816
required: false
@@ -1438,6 +1441,7 @@ runs:
14381441
AWS_ECS_SERVICE_LAUNCH_TYPE : ${{ inputs.aws_ecs_service_launch_type }}
14391442
AWS_ECS_TASK_TYPE : ${{ inputs.aws_ecs_task_type }}
14401443
AWS_ECS_TASK_NAME: ${{ inputs.aws_ecs_task_name }}
1444+
AWS_ECS_TASK_IGNORE_DEFINITION: ${{ inputs.aws_ecs_task_ignore_definition }}
14411445
AWS_ECS_TASK_EXECUTION_ROLE: ${{ inputs.aws_ecs_task_execution_role }}
14421446
AWS_ECS_TASK_JSON_DEFINITION_FILE: ${{ inputs.aws_ecs_task_json_definition_file }}
14431447
AWS_ECS_TASK_NETWORK_MODE: ${{ inputs.aws_ecs_task_network_mode }}

operations/_scripts/generate/generate_vars_terraform.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ if [[ $(alpha_only "$AWS_ECS_ENABLE") == true ]]; then
317317
aws_ecs_service_launch_type=$(generate_var aws_ecs_service_launch_type $AWS_ECS_SERVICE_LAUNCH_TYPE)
318318
aws_ecs_task_type=$(generate_var aws_ecs_task_type $AWS_ECS_TASK_TYPE)
319319
aws_ecs_task_name=$(generate_var aws_ecs_task_name $AWS_ECS_TASK_NAME)
320+
aws_ecs_task_ignore_definition=$(generate_var aws_ecs_task_ignore_definition $AWS_ECS_TASK_IGNORE_DEFINITION)
320321
aws_ecs_task_execution_role=$(generate_var aws_ecs_task_execution_role $AWS_ECS_TASK_EXECUTION_ROLE)
321322
aws_ecs_task_json_definition_file=$(generate_var aws_ecs_task_json_definition_file $AWS_ECS_TASK_JSON_DEFINITION_FILE)
322323
aws_ecs_task_network_mode=$(generate_var aws_ecs_task_network_mode $AWS_ECS_TASK_NETWORK_MODE)
@@ -665,6 +666,7 @@ $aws_ecs_cluster_name
665666
$aws_ecs_service_launch_type
666667
$aws_ecs_task_type
667668
$aws_ecs_task_name
669+
$aws_ecs_task_ignore_definition
668670
$aws_ecs_task_execution_role
669671
$aws_ecs_task_json_definition_file
670672
$aws_ecs_task_network_mode

operations/deployment/terraform/aws/aws_variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,12 @@ variable "aws_ecs_task_name" {
13491349
default = ""
13501350
}
13511351

1352+
variable "aws_ecs_task_ignore_definition" {
1353+
type = bool
1354+
description = "Toggle ignoring changes to the task definition"
1355+
default = false
1356+
}
1357+
13521358
variable "aws_ecs_task_execution_role" {
13531359
type = string
13541360
description = "Elastic Container Service task execution role name."

operations/deployment/terraform/aws/bitovi_main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ module "aws_ecs" {
468468
aws_ecs_service_launch_type = var.aws_ecs_service_launch_type
469469
aws_ecs_task_type = var.aws_ecs_task_type
470470
aws_ecs_task_name = var.aws_ecs_task_name
471+
aws_ecs_task_ignore_definition = var.aws_ecs_task_ignore_definition
471472
aws_ecs_task_execution_role = var.aws_ecs_task_execution_role
472473
aws_ecs_task_json_definition_file = var.aws_ecs_task_json_definition_file
473474
aws_ecs_task_network_mode = var.aws_ecs_task_network_mode

operations/deployment/terraform/modules/aws/ecs/aws_ecs.tf

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ locals {
2525
}
2626

2727
resource "aws_ecs_task_definition" "ecs_task" {
28-
count = length(local.aws_ecs_app_image)
28+
count = var.aws_ecs_task_ignore_definition ? 0 : length(local.aws_ecs_app_image)
2929
family = var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}"
3030
network_mode = local.aws_ecs_task_network_mode[count.index]
3131
requires_compatibilities = [local.aws_ecs_task_type[count.index]]
@@ -63,7 +63,7 @@ resource "aws_ecs_task_definition" "ecs_task" {
6363
}
6464

6565
resource "aws_ecs_task_definition" "ecs_task_from_json" {
66-
count = length(local.aws_ecs_task_json_definition_file)
66+
count = var.aws_ecs_task_ignore_definition ? 0 : length(local.aws_ecs_task_json_definition_file)
6767
family = var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index + length(local.aws_ecs_app_image)] : "${local.aws_ecs_task_name[count.index + length(local.aws_ecs_app_image)]}${count.index+length(local.aws_ecs_app_image)}"
6868
network_mode = local.aws_ecs_task_network_mode[count.index + length(local.aws_ecs_app_image)]
6969
requires_compatibilities = ["${local.aws_ecs_task_type[count.index +length(local.aws_ecs_app_image)]}"]
@@ -73,13 +73,63 @@ resource "aws_ecs_task_definition" "ecs_task_from_json" {
7373
container_definitions = sensitive(file("../../ansible/clone_repo/app/${var.app_repo_name}/${local.aws_ecs_task_json_definition_file[count.index]}"))
7474
}
7575

76+
resource "aws_ecs_task_definition" "aws_ecs_task_ignore_definition" {
77+
count = var.aws_ecs_task_ignore_definition ? 1 : 0
78+
family = var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}"
79+
network_mode = local.aws_ecs_task_network_mode[count.index]
80+
requires_compatibilities = [local.aws_ecs_task_type[count.index]]
81+
cpu = local.aws_ecs_task_cpu[count.index]
82+
memory = local.aws_ecs_task_mem[count.index]
83+
execution_role_arn = local.ecsTaskExecutionRole
84+
container_definitions = sensitive(jsonencode([
85+
{
86+
"name": var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}",
87+
"image": "nginx:alpine",
88+
"essential": true,
89+
"portMappings": [
90+
{
91+
"containerPort": 80,
92+
"protocol": "tcp",
93+
"hostPort": 80,
94+
"appProtocol": "http"
95+
}
96+
]
97+
}
98+
]))
99+
lifecycle {
100+
ignore_changes = [container_definitions]
101+
}
102+
}
103+
76104
locals {
77-
tasks_arns = concat(aws_ecs_task_definition.ecs_task[*].arn,aws_ecs_task_definition.ecs_task_from_json[*].arn)
78-
tasks_count = length(local.aws_ecs_app_image) + length(local.aws_ecs_task_json_definition_file)
105+
tasks_arns = concat(aws_ecs_task_definition.ecs_task[*].arn,aws_ecs_task_definition.ecs_task_from_json[*].arn,aws_ecs_task_definition.aws_ecs_task_ignore_definition[*].arn)
106+
tasks_count = var.aws_ecs_task_ignore_definition ? 1 : length(local.aws_ecs_app_image) + length(local.aws_ecs_task_json_definition_file)
79107
}
80108

81109
resource "aws_ecs_service" "ecs_service" {
82-
count = local.tasks_count
110+
count = var.aws_ecs_task_ignore_definition ? 0 : local.tasks_count
111+
name = var.aws_ecs_service_name != "" ? "${var.aws_ecs_service_name}${count.index}" : "${var.aws_resource_identifier}-${count.index}-service"
112+
cluster = aws_ecs_cluster.cluster.id
113+
task_definition = local.tasks_arns[count.index]
114+
115+
desired_count = local.aws_ecs_node_count[count.index]
116+
launch_type = var.aws_ecs_service_launch_type
117+
118+
network_configuration {
119+
security_groups = [aws_security_group.ecs_sg.id]
120+
subnets = var.aws_selected_subnets
121+
assign_public_ip = var.aws_ecs_assign_public_ip
122+
}
123+
124+
load_balancer {
125+
target_group_arn = aws_alb_target_group.lb_targets[count.index].id
126+
container_name = var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}"
127+
container_port = local.aws_ecs_container_port[count.index]
128+
}
129+
}
130+
131+
resource "aws_ecs_service" "ecs_service_ignore_definition" {
132+
count = var.aws_ecs_task_ignore_definition ? 1 : 0
83133
name = var.aws_ecs_service_name != "" ? "${var.aws_ecs_service_name}${count.index}" : "${var.aws_resource_identifier}-${count.index}-service"
84134
cluster = aws_ecs_cluster.cluster.id
85135
task_definition = local.tasks_arns[count.index]
@@ -98,6 +148,10 @@ resource "aws_ecs_service" "ecs_service" {
98148
container_name = var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}"
99149
container_port = local.aws_ecs_container_port[count.index]
100150
}
151+
152+
lifecycle {
153+
ignore_changes = [task_definition]
154+
}
101155
}
102156

103157
# Cloudwatch config

operations/deployment/terraform/modules/aws/ecs/aws_ecs_networking.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ resource "aws_alb" "ecs_lb" {
4545
}
4646
}
4747

48+
data "aws_alb" "selected_lb" {
49+
name = var.aws_resource_identifier_supershort
50+
depends_on = [ aws_alb.ecs_lb ]
51+
}
52+
4853
resource "aws_alb_target_group" "lb_targets" {
4954
count = length(local.aws_ecs_container_port)
5055
name = "${var.aws_resource_identifier_supershort}${count.index}"
@@ -233,7 +238,8 @@ output "load_balancer_protocol" {
233238
}
234239

235240
output "load_balancer_zone_id" {
236-
value = aws_alb.ecs_lb.zone_id
241+
#value = aws_alb.ecs_lb.zone_id
242+
value = data.aws_alb.selected_lb.zone_id
237243
}
238244

239245
output "load_balancer_arn" {

operations/deployment/terraform/modules/aws/ecs/aws_ecs_vars.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ variable "aws_ecs_cluster_name" {}
33
variable "aws_ecs_service_launch_type" {}
44
variable "aws_ecs_task_type" {}
55
variable "aws_ecs_task_name" {}
6+
variable "aws_ecs_task_ignore_definition" {}
67
variable "aws_ecs_task_execution_role" {}
78
variable "aws_ecs_task_json_definition_file" {}
89
variable "aws_ecs_task_network_mode" {}

0 commit comments

Comments
 (0)