Skip to content

Commit 8d6f967

Browse files
authored
Merge pull request #11 from synapsestudios/add-container-def-variable-support
add container definition support
2 parents ceb1d4a + 215aa4b commit 8d6f967

5 files changed

+58
-22
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,29 @@ You can do this by commenting out the entire module, running a terraform apply,
5555
| Name | Description | Type | Default | Required |
5656
|------|-------------|------|---------|:--------:|
5757
| <a name="input_alb_security_group_id"></a> [alb\_security\_group\_id](#input\_alb\_security\_group\_id) | Security Group ID for the ALB | `string` | n/a | yes |
58+
| <a name="input_assign_public_ip"></a> [assign\_public\_ip](#input\_assign\_public\_ip) | Whether or not to assign a public IP to the task | `bool` | `false` | no |
5859
| <a name="input_azs"></a> [azs](#input\_azs) | Availability zones | `list(string)` | n/a | yes |
5960
| <a name="input_cluster_arn"></a> [cluster\_arn](#input\_cluster\_arn) | ECS cluster to deploy into | `string` | n/a | yes |
60-
| <a name="input_command"></a> [command](#input\_command) | Container startup command | `list(string)` | n/a | yes |
61-
| <a name="input_container_image"></a> [container\_image](#input\_container\_image) | Image tag of the Docker container to use for this service | `string` | n/a | yes |
61+
| <a name="input_command"></a> [command](#input\_command) | Container startup command (Use null if container\_definitions is set) | `list(string)` | n/a | yes |
62+
| <a name="input_container_definitions"></a> [container\_definitions](#input\_container\_definitions) | A list of valid container definitions provided as a single valid JSON document. By default, this module will generate a container definition for you. If you need to provide your own or have multiple, you can do so here. | `string` | `null` | no |
63+
| <a name="input_container_image"></a> [container\_image](#input\_container\_image) | Image tag of the Docker container to use for this service (Use null if container\_definitions is set) | `string` | n/a | yes |
6264
| <a name="input_container_port"></a> [container\_port](#input\_container\_port) | Port exposed by the container | `number` | n/a | yes |
63-
| <a name="input_container_secrets"></a> [container\_secrets](#input\_container\_secrets) | The Secrets to Pass to the container. | <pre>list(object({<br> name = string<br> valueFrom = string<br> }))</pre> | `[]` | no |
65+
| <a name="input_container_secrets"></a> [container\_secrets](#input\_container\_secrets) | The Secrets to Pass to the container. (Do not use if container\_definitions is set) | <pre>list(object({<br> name = string<br> valueFrom = string<br> }))</pre> | `[]` | no |
6466
| <a name="input_db_instance_class"></a> [db\_instance\_class](#input\_db\_instance\_class) | Size of instances within the RDS cluster | `string` | `"db.t4g.medium"` | no |
6567
| <a name="input_db_instance_count"></a> [db\_instance\_count](#input\_db\_instance\_count) | How many RDS instances to create | `number` | `1` | no |
6668
| <a name="input_db_name"></a> [db\_name](#input\_db\_name) | Name of the postgres database to create, if creating an RDS cluster | `string` | `"main"` | no |
6769
| <a name="input_ecs_desired_count"></a> [ecs\_desired\_count](#input\_ecs\_desired\_count) | How many tasks to launch in ECS service | `number` | `1` | no |
68-
| <a name="input_environment_variables"></a> [environment\_variables](#input\_environment\_variables) | The environment variables to pass to the container. This is a list of maps. | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
70+
| <a name="input_environment_variables"></a> [environment\_variables](#input\_environment\_variables) | The environment variables to pass to the container. This is a list of maps. (Do not use if container\_definitions is set) | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
6971
| <a name="input_health_check_path"></a> [health\_check\_path](#input\_health\_check\_path) | Path to use for health checks | `string` | n/a | yes |
70-
| <a name="input_host_port"></a> [host\_port](#input\_host\_port) | Port exposed by the host | `number` | `null` | no |
72+
| <a name="input_host_port"></a> [host\_port](#input\_host\_port) | Port exposed by the host (Do not use if container\_definitions is set) | `number` | `null` | no |
7173
| <a name="input_hostname"></a> [hostname](#input\_hostname) | Hostname to use for listener rule | `string` | n/a | yes |
7274
| <a name="input_listener_arn"></a> [listener\_arn](#input\_listener\_arn) | ALB listener ARN to add listener rule to | `string` | n/a | yes |
75+
| <a name="input_load_balancer_container_name"></a> [load\_balancer\_container\_name](#input\_load\_balancer\_container\_name) | Container name to use for load balancer target group forwarder | `string` | n/a | yes |
7376
| <a name="input_service_name"></a> [service\_name](#input\_service\_name) | Service directory in the application git repo | `string` | n/a | yes |
7477
| <a name="input_subnets"></a> [subnets](#input\_subnets) | List of subnet names the service will reside on. | `list(string)` | n/a | yes |
78+
| <a name="input_task_cpu"></a> [task\_cpu](#input\_task\_cpu) | Task CPU | `number` | `1024` | no |
79+
| <a name="input_task_memory"></a> [task\_memory](#input\_task\_memory) | Task memory | `number` | `2048` | no |
7580
| <a name="input_use_database_cluster"></a> [use\_database\_cluster](#input\_use\_database\_cluster) | Whether or not we should create a DB cluster and inject the database connection string into the container | `bool` | n/a | yes |
76-
| <a name="input_use_hostname"></a> [use\_hostname](#input\_use\_hostname) | Whether or not we should create a target group and listener to attach this service to a load balancer | `bool` | n/a | yes |
7781
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | VPC to deploy into | `string` | n/a | yes |
7882

7983
## Outputs

ecs_service.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ resource "aws_ecs_service" "this" {
77

88
load_balancer {
99
target_group_arn = aws_lb_target_group.this.arn
10-
container_name = var.service_name
10+
container_name = var.load_balancer_container_name != null ? var.load_balancer_container_name : var.service_name
1111
container_port = var.container_port
1212
}
1313

@@ -16,7 +16,7 @@ resource "aws_ecs_service" "this" {
1616
security_groups = [aws_security_group.ecs_task.id]
1717
# If you are using Fargate tasks, in order for the task to pull the container image it must either use a public subnet and be assigned a
1818
# public IP address or a private subnet that has a route to the internet or a NAT gateway that can route requests to the internet.
19-
assign_public_ip = false
19+
assign_public_ip = var.assign_public_ip
2020
}
2121

2222
# This allows dynamic scaling and external deployments

ecs_task_definitions.tf

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11

22
resource "aws_ecs_task_definition" "service" {
33
family = var.service_name
4-
container_definitions = "[${module.service_container_definition.json_map_encoded}]"
4+
container_definitions = var.container_definitions != null ? var.container_definitions : "[${module.service_container_definition.json_map_encoded}]"
55
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
66
task_role_arn = aws_iam_role.ecs_task_role.arn
77
network_mode = "awsvpc"
8-
memory = 2048
9-
cpu = 1024
8+
memory = var.task_memory
9+
cpu = var.task_cpu
1010
requires_compatibilities = ["FARGATE"]
11+
12+
runtime_platform {
13+
# Required if using Fargate launch type
14+
operating_system_family = "LINUX"
15+
}
16+
1117
}
1218

1319
#tfsec:ignore:aws-cloudwatch-log-group-customer-key
@@ -16,6 +22,8 @@ resource "aws_cloudwatch_log_group" "service" {
1622
}
1723

1824
module "service_container_definition" {
25+
count = var.container_definitions != null ? 0 : 1
26+
1927
source = "cloudposse/ecs-container-definition/aws"
2028
version = "0.58.1"
2129

ecs_task_execution_role.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ resource "aws_iam_role_policy_attachment" "ecs_task_execution_role" {
2626
resource "aws_iam_role_policy_attachment" "secrets_manager" {
2727
role = aws_iam_role.ecs_task_execution_role.name
2828
policy_arn = "arn:aws:iam::aws:policy/SecretsManagerReadWrite"
29-
}
29+
}

variables.tf

+34-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ variable "subnets" {
1515

1616
variable "container_image" {
1717
type = string
18-
description = "Image tag of the Docker container to use for this service"
18+
description = "Image tag of the Docker container to use for this service (Use null if container_definitions is set)"
1919
}
2020

2121
variable "azs" {
@@ -28,7 +28,7 @@ variable "environment_variables" {
2828
name = string
2929
value = string
3030
}))
31-
description = "The environment variables to pass to the container. This is a list of maps."
31+
description = "The environment variables to pass to the container. This is a list of maps. (Do not use if container_definitions is set)"
3232
default = []
3333
}
3434

@@ -37,7 +37,7 @@ variable "container_secrets" {
3737
name = string
3838
valueFrom = string
3939
}))
40-
description = "The Secrets to Pass to the container."
40+
description = "The Secrets to Pass to the container. (Do not use if container_definitions is set)"
4141
default = []
4242
}
4343

@@ -53,7 +53,7 @@ variable "alb_security_group_id" {
5353

5454
variable "command" {
5555
type = list(string)
56-
description = "Container startup command"
56+
description = "Container startup command (Use null if container_definitions is set)"
5757
}
5858

5959
variable "hostname" {
@@ -73,7 +73,7 @@ variable "container_port" {
7373

7474
variable "host_port" {
7575
type = number
76-
description = "Port exposed by the host"
76+
description = "Port exposed by the host (Do not use if container_definitions is set)"
7777
default = null
7878
}
7979

@@ -87,11 +87,6 @@ variable "use_database_cluster" {
8787
description = "Whether or not we should create a DB cluster and inject the database connection string into the container"
8888
}
8989

90-
variable "use_hostname" {
91-
type = bool
92-
description = "Whether or not we should create a target group and listener to attach this service to a load balancer"
93-
}
94-
9590
variable "ecs_desired_count" {
9691
type = number
9792
default = 1
@@ -115,3 +110,32 @@ variable "db_instance_count" {
115110
default = 1
116111
description = "How many RDS instances to create"
117112
}
113+
114+
variable "container_definitions" {
115+
type = string
116+
description = "A list of valid container definitions provided as a single valid JSON document. By default, this module will generate a container definition for you. If you need to provide your own or have multiple, you can do so here."
117+
default = null
118+
}
119+
120+
variable "task_memory" {
121+
type = number
122+
description = "Task memory"
123+
default = 2048
124+
}
125+
126+
variable "task_cpu" {
127+
type = number
128+
description = "Task CPU"
129+
default = 1024
130+
}
131+
132+
variable "load_balancer_container_name" {
133+
type = string
134+
description = "Container name to use for load balancer target group forwarder"
135+
}
136+
137+
variable "assign_public_ip" {
138+
type = bool
139+
description = "Whether or not to assign a public IP to the task"
140+
default = false
141+
}

0 commit comments

Comments
 (0)