Skip to content

Commit ce9727a

Browse files
SherdycoderAbdul Wahid
andauthored
Add support for EnvironmentFiles in container definition (#60)
* Add support for environment files * formatting * add iam permissions * typo * add GetBucketLocation permissions to iam policy * Update Changelog Co-authored-by: Abdul Wahid <[email protected]>
1 parent 925e2ac commit ce9727a

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ All notable changes to this project will be documented in this file.
55
<a name="unreleased"></a>
66
## [Unreleased]
77

8-
- added option to allow customisation of os/cpu architecture
8+
- add GetBucketLocation permissions to iam policy
9+
- add iam permissions
10+
- Add support for environment files
11+
- Allow option to customise run_time platform ([#56](https://github.com/umotif-public/terraform-aws-ecs-fargate/issues/56))
912
- Enable containerDefinitions portMappings to use target_groups container_ports ([#59](https://github.com/umotif-public/terraform-aws-ecs-fargate/issues/59))
1013

1114

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ No modules.
107107
| [aws_iam_role.execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
108108
| [aws_iam_role.task](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
109109
| [aws_iam_role_policy.ecs_exec_inline_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
110+
| [aws_iam_role_policy.get_environment_files](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
110111
| [aws_iam_role_policy.log_agent](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
111112
| [aws_iam_role_policy.read_repository_credentials](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
112113
| [aws_iam_role_policy.task_execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
113114
| [aws_lb_target_group.task](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource |
114115
| [aws_security_group.ecs_service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
115116
| [aws_security_group_rule.egress_service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
116117
| [aws_ecs_task_definition.task](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ecs_task_definition) | data source |
118+
| [aws_iam_policy_document.get_environment_files](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
117119
| [aws_iam_policy_document.read_repository_credentials](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
118120
| [aws_iam_policy_document.task_assume](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
119121
| [aws_iam_policy_document.task_ecs_exec_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -159,6 +161,7 @@ No modules.
159161
| <a name="input_task_container_command"></a> [task\_container\_command](#input\_task\_container\_command) | The command that is passed to the container. | `list(string)` | `[]` | no |
160162
| <a name="input_task_container_cpu"></a> [task\_container\_cpu](#input\_task\_container\_cpu) | Amount of CPU to reserve for the container. | `number` | `null` | no |
161163
| <a name="input_task_container_environment"></a> [task\_container\_environment](#input\_task\_container\_environment) | The environment variables to pass to a container. | `map(string)` | `{}` | no |
164+
| <a name="input_task_container_environment_files"></a> [task\_container\_environment\_files](#input\_task\_container\_environment\_files) | The environment variable files (s3 object arns) to pass to a container. Files must use .env file extension. | `list(string)` | `[]` | no |
162165
| <a name="input_task_container_image"></a> [task\_container\_image](#input\_task\_container\_image) | The image used to start a container. | `string` | n/a | yes |
163166
| <a name="input_task_container_memory"></a> [task\_container\_memory](#input\_task\_container\_memory) | The hard limit (in MiB) of memory for the container. | `number` | `null` | no |
164167
| <a name="input_task_container_memory_reservation"></a> [task\_container\_memory\_reservation](#input\_task\_container\_memory\_reservation) | The soft limit (in MiB) of memory to reserve for the container. | `number` | `null` | no |

data.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ data "aws_iam_policy_document" "read_repository_credentials" {
9292
}
9393
}
9494

95+
data "aws_iam_policy_document" "get_environment_files" {
96+
count = length(var.task_container_environment_files) != 0 ? 1 : 0
97+
98+
statement {
99+
effect = "Allow"
100+
101+
resources = var.task_container_environment_files
102+
103+
actions = [
104+
"s3:GetObject"
105+
]
106+
}
107+
108+
statement {
109+
effect = "Allow"
110+
111+
resources = [for file in var.task_container_environment_files : split("/", file)[0]]
112+
113+
actions = [
114+
"s3:GetBucketLocation"
115+
]
116+
}
117+
}
118+
95119
data "aws_ecs_task_definition" "task" {
96120
task_definition = aws_ecs_task_definition.task.family
97121
}

main.tf

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ resource "aws_iam_role_policy" "read_repository_credentials" {
3434
policy = data.aws_iam_policy_document.read_repository_credentials[0].json
3535
}
3636

37+
resource "aws_iam_role_policy" "get_environment_files" {
38+
count = length(var.task_container_environment_files) != 0 ? 1 : 0
39+
40+
name = "${var.name_prefix}-read-repository-credentials"
41+
role = aws_iam_role.execution.id
42+
policy = data.aws_iam_policy_document.get_environment_files[0].json
43+
}
44+
3745
#####
3846
# IAM - Task role, basic. Append policies to this role for S3, DynamoDB etc.
3947
#####
@@ -147,6 +155,13 @@ locals {
147155
protocol = contains(keys(tg), "protocol") ? lower(tg.protocol) : "tcp"
148156
}
149157
]) : []
158+
159+
task_environment_files = [
160+
for file in var.task_container_environment_files : {
161+
value = file
162+
type = "s3"
163+
}
164+
]
150165
}
151166

152167
resource "aws_ecs_task_definition" "task" {
@@ -237,7 +252,8 @@ resource "aws_ecs_task_definition" "task" {
237252
%{if var.task_pseudo_terminal != null~}
238253
"pseudoTerminal": ${var.task_pseudo_terminal},
239254
%{~endif}
240-
"environment": ${jsonencode(local.task_environment)}
255+
"environment": ${jsonencode(local.task_environment)},
256+
"environmentFiles": ${jsonencode(local.task_environment_files)}
241257
}]
242258
EOF
243259

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ variable "task_container_environment" {
9999
type = map(string)
100100
}
101101

102+
variable "task_container_environment_files" {
103+
description = "The environment variable files (s3 object arns) to pass to a container. Files must use .env file extension."
104+
default = []
105+
type = list(string)
106+
}
107+
102108
variable "task_container_secrets" {
103109
description = "The secrets variables to pass to a container."
104110
default = null

0 commit comments

Comments
 (0)