Skip to content

Commit 76acddb

Browse files
authored
feat: Add support for easily enabling ECS Exec support (#127)
1 parent 2f31eb0 commit 76acddb

File tree

7 files changed

+35
-3
lines changed

7 files changed

+35
-3
lines changed

examples/fargate/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ module "ecs_service" {
6161
cpu = 1024
6262
memory = 4096
6363

64+
# Enables ECS Exec
65+
enable_execute_command = true
66+
6467
# Container definition(s)
6568
container_definitions = {
6669

modules/container-definition/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ No modules.
152152
| <a name="input_docker_labels"></a> [docker\_labels](#input\_docker\_labels) | A key/value map of labels to add to the container | `map(string)` | `{}` | no |
153153
| <a name="input_docker_security_options"></a> [docker\_security\_options](#input\_docker\_security\_options) | A list of strings to provide custom labels for SELinux and AppArmor multi-level security systems. This field isn't valid for containers in tasks using the Fargate launch type | `list(string)` | `[]` | no |
154154
| <a name="input_enable_cloudwatch_logging"></a> [enable\_cloudwatch\_logging](#input\_enable\_cloudwatch\_logging) | Determines whether CloudWatch logging is configured for this container definition. Set to `false` to use other logging drivers | `bool` | `true` | no |
155+
| <a name="input_enable_execute_command"></a> [enable\_execute\_command](#input\_enable\_execute\_command) | Specifies whether to enable Amazon ECS Exec for the tasks within the service | `bool` | `false` | no |
155156
| <a name="input_entrypoint"></a> [entrypoint](#input\_entrypoint) | The entry point that is passed to the container | `list(string)` | `[]` | no |
156157
| <a name="input_environment"></a> [environment](#input\_environment) | The environment variables to pass to the container | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
157158
| <a name="input_environment_files"></a> [environment\_files](#input\_environment\_files) | A list of files containing the environment variables to pass to a container | <pre>list(object({<br> value = string<br> type = string<br> }))</pre> | `[]` | no |

modules/container-definition/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ locals {
1717
var.log_configuration
1818
)
1919

20+
linux_parameters = var.enable_execute_command ? merge({ "initProcessEnabled" : true }, var.linux_parameters) : var.linux_parameters
21+
2022
definition = {
2123
command = length(var.command) > 0 ? var.command : null
2224
cpu = var.cpu
@@ -37,7 +39,7 @@ locals {
3739
image = var.image
3840
interactive = var.interactive
3941
links = local.is_not_windows && length(var.links) > 0 ? var.links : null
40-
linuxParameters = local.is_not_windows && length(var.linux_parameters) > 0 ? var.linux_parameters : null
42+
linuxParameters = local.is_not_windows && length(local.linux_parameters) > 0 ? local.linux_parameters : null
4143
logConfiguration = length(local.log_configuration) > 0 ? local.log_configuration : null
4244
memory = var.memory
4345
memoryReservation = var.memory_reservation

modules/container-definition/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ variable "docker_security_options" {
5959
default = []
6060
}
6161

62+
variable "enable_execute_command" {
63+
description = "Specifies whether to enable Amazon ECS Exec for the tasks within the service"
64+
type = bool
65+
default = false
66+
}
67+
6268
variable "entrypoint" {
6369
description = "The entry point that is passed to the container"
6470
type = list(string)

modules/service/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ module "ecs_service" {
130130
# Disable creation of service and all resources
131131
create = false
132132
133+
# Enable ECS Exec
134+
enable_execute_command = true
135+
133136
# Disable creation of the service IAM role; `iam_role_arn` should be provided
134137
create_iam_role = false
135138

modules/service/main.tf

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ module "container_definition" {
536536
dns_servers = try(each.value.dns_servers, var.container_definition_defaults.dns_servers, [])
537537
docker_labels = try(each.value.docker_labels, var.container_definition_defaults.docker_labels, {})
538538
docker_security_options = try(each.value.docker_security_options, var.container_definition_defaults.docker_security_options, [])
539+
enable_execute_command = try(each.value.enable_execute_command, var.container_definition_defaults.enable_execute_command, var.enable_execute_command)
539540
entrypoint = try(each.value.entrypoint, var.container_definition_defaults.entrypoint, [])
540541
environment = try(each.value.environment, var.container_definition_defaults.environment, [])
541542
environment_files = try(each.value.environment_files, var.container_definition_defaults.environment_files, [])
@@ -951,7 +952,22 @@ resource "aws_iam_role_policy_attachment" "tasks" {
951952
}
952953

953954
data "aws_iam_policy_document" "tasks" {
954-
count = local.create_tasks_iam_role && length(var.tasks_iam_role_statements) > 0 ? 1 : 0
955+
count = local.create_tasks_iam_role && (length(var.tasks_iam_role_statements) > 0 || var.enable_execute_command) ? 1 : 0
956+
957+
dynamic "statement" {
958+
for_each = var.enable_execute_command ? [1] : []
959+
960+
content {
961+
sid = "ECSExec"
962+
actions = [
963+
"ssmmessages:CreateControlChannel",
964+
"ssmmessages:CreateDataChannel",
965+
"ssmmessages:OpenControlChannel",
966+
"ssmmessages:OpenDataChannel",
967+
]
968+
resources = ["*"]
969+
}
970+
}
955971

956972
dynamic "statement" {
957973
for_each = var.tasks_iam_role_statements
@@ -996,7 +1012,7 @@ data "aws_iam_policy_document" "tasks" {
9961012
}
9971013

9981014
resource "aws_iam_role_policy" "tasks" {
999-
count = local.create_tasks_iam_role && length(var.tasks_iam_role_statements) > 0 ? 1 : 0
1015+
count = local.create_tasks_iam_role && (length(var.tasks_iam_role_statements) > 0 || var.enable_execute_command) ? 1 : 0
10001016

10011017
name = var.tasks_iam_role_use_name_prefix ? null : local.tasks_iam_role_name
10021018
name_prefix = var.tasks_iam_role_use_name_prefix ? "${local.tasks_iam_role_name}-" : null

wrappers/container-definition/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module "wrapper" {
1616
docker_labels = try(each.value.docker_labels, var.defaults.docker_labels, {})
1717
docker_security_options = try(each.value.docker_security_options, var.defaults.docker_security_options, [])
1818
enable_cloudwatch_logging = try(each.value.enable_cloudwatch_logging, var.defaults.enable_cloudwatch_logging, true)
19+
enable_execute_command = try(each.value.enable_execute_command, var.defaults.enable_execute_command, false)
1920
entrypoint = try(each.value.entrypoint, var.defaults.entrypoint, [])
2021
environment = try(each.value.environment, var.defaults.environment, [])
2122
environment_files = try(each.value.environment_files, var.defaults.environment_files, [])

0 commit comments

Comments
 (0)