Skip to content

Commit d415eb9

Browse files
authored
fix: Fixes KMS permissions for ECS task container
* fix: Adds kms:Decrypt to ECS task execution role if KMS key is being used * fix: Fixes dependencies
1 parent dec4647 commit d415eb9

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Before using this module, ensure you have the following:
122122
| [aws_iam_group_membership.directus](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_group_membership) | resource |
123123
| [aws_iam_group_policy.s3_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_group_policy) | resource |
124124
| [aws_iam_policy.cloudwatch_logs_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
125+
| [aws_iam_policy.kms_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
125126
| [aws_iam_role.ecs_ebs_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
126127
| [aws_iam_role.ecs_service_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
127128
| [aws_iam_role.ecs_task_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
@@ -148,6 +149,7 @@ Before using this module, ensure you have the following:
148149
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
149150
| [aws_iam_policy_document.cloudwatch_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
150151
| [aws_iam_policy_document.kms_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
152+
| [aws_iam_policy_document.kms_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
151153
| [aws_iam_policy_document.s3_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
152154
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
153155
| [aws_s3_bucket.directus](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket) | data source |
@@ -168,6 +170,7 @@ Before using this module, ensure you have the following:
168170
| <a name="input_ecs_service_enable_execute_command"></a> [ecs\_service\_enable\_execute\_command](#input\_ecs\_service\_enable\_execute\_command) | Whether to enable ECS service execute command | `bool` | `false` | no |
169171
| <a name="input_enable_alb_access_logs"></a> [enable\_alb\_access\_logs](#input\_enable\_alb\_access\_logs) | Whether to enable access logs of the Load Balancer | `bool` | `false` | no |
170172
| <a name="input_enable_ecs_volume"></a> [enable\_ecs\_volume](#input\_enable\_ecs\_volume) | Whether to enable ECS volume | `bool` | `false` | no |
173+
| <a name="input_enable_kms_encryption"></a> [enable\_kms\_encryption](#input\_enable\_kms\_encryption) | Whether to enable KMS encryption | `bool` | `false` | no |
171174
| <a name="input_enable_s3_bucket_versioning"></a> [enable\_s3\_bucket\_versioning](#input\_enable\_s3\_bucket\_versioning) | Whether to enable S3 bucket versioning | `bool` | `true` | no |
172175
| <a name="input_enable_ses_emails_sending"></a> [enable\_ses\_emails\_sending](#input\_enable\_ses\_emails\_sending) | Whether to enable sending emails using SES | `bool` | `false` | no |
173176
| <a name="input_force_new_ecs_deployment_on_apply"></a> [force\_new\_ecs\_deployment\_on\_apply](#input\_force\_new\_ecs\_deployment\_on\_apply) | Whether to force a new deployment of the ECS service on apply | `bool` | `false` | no |
@@ -200,6 +203,7 @@ Before using this module, ensure you have the following:
200203
| <a name="output_load_balancer_dns_name"></a> [load\_balancer\_dns\_name](#output\_load\_balancer\_dns\_name) | The DNS name of the load balancer |
201204
| <a name="output_load_balancer_listener_arn"></a> [load\_balancer\_listener\_arn](#output\_load\_balancer\_listener\_arn) | The ARN of the load balancer listener |
202205
| <a name="output_load_balancer_target_group_arn"></a> [load\_balancer\_target\_group\_arn](#output\_load\_balancer\_target\_group\_arn) | The ARN of the load balancer target group |
206+
| <a name="output_public_url"></a> [public\_url](#output\_public\_url) | The public URL of the Directus service |
203207
| <a name="output_s3_bucket_arn"></a> [s3\_bucket\_arn](#output\_s3\_bucket\_arn) | The ARN of the S3 bucket |
204208
| <a name="output_s3_bucket_name"></a> [s3\_bucket\_name](#output\_s3\_bucket\_name) | The name of the S3 bucket |
205209
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ module "directus" {
7474
create_s3_bucket = true # If you do not create an S3 bucket, you will need to provide an existing S3 bucket name
7575
s3_bucket_name = "terraform-aws-directus-${local.region}"
7676

77-
kms_key_id = aws_kms_key.directus.id
77+
enable_kms_encryption = true
78+
kms_key_id = aws_kms_key.directus.id
7879

7980
image_tag = "10.12"
8081

iam.tf

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ resource "aws_iam_role_policy_attachment" "ecs_ebs_role_policy" {
2828
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSInfrastructureRolePolicyForVolumes"
2929
}
3030

31+
data "aws_iam_policy_document" "kms_policy" {
32+
count = var.enable_kms_encryption ? 1 : 0
33+
34+
statement {
35+
sid = "KmsAccess"
36+
37+
actions = [
38+
"kms:Decrypt"
39+
]
40+
41+
resources = [local.kms_key_arn]
42+
}
43+
}
44+
45+
resource "aws_iam_policy" "kms_policy" {
46+
count = var.enable_kms_encryption ? 1 : 0
47+
48+
name = "${var.application_name}-kms-policy"
49+
path = "/${var.application_name}/"
50+
policy = data.aws_iam_policy_document.kms_policy[0].json
51+
}
52+
3153
resource "aws_iam_role" "ecs_service_role" {
3254
name = "${var.application_name}-ecs-service-role"
3355

@@ -116,7 +138,8 @@ resource "aws_iam_group_policy" "s3_policy" {
116138
}
117139

118140
resource "aws_iam_user_policy" "kms_access" {
119-
count = var.kms_key_id != "" ? 1 : 0
141+
count = var.enable_kms_encryption ? 1 : 0
142+
120143
name = "${var.application_name}-kms-policy"
121144
user = aws_iam_user.directus.name
122145
policy = data.aws_iam_policy_document.kms_access_policy[0].json
@@ -141,7 +164,7 @@ data "aws_iam_policy_document" "s3_policy" {
141164
}
142165

143166
data "aws_iam_policy_document" "kms_access_policy" {
144-
count = var.kms_key_id != "" ? 1 : 0
167+
count = var.enable_kms_encryption ? 1 : 0
145168
statement {
146169
sid = "KmsUsage"
147170

main.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ resource "aws_s3_bucket" "directus" {
124124
}
125125

126126
resource "aws_s3_bucket_server_side_encryption_configuration" "example" {
127-
count = var.create_s3_bucket && var.kms_key_id != "" ? 1 : 0
127+
count = var.create_s3_bucket && var.enable_kms_encryption ? 1 : 0
128128
bucket = aws_s3_bucket.directus[0].id
129129

130130
rule {
@@ -230,9 +230,11 @@ module "ecs" {
230230

231231
task_exec_iam_role_name = "${local.cluster_name}-task-exec-role"
232232
task_exec_iam_role_path = "/ecs/${local.cluster_name}/"
233-
task_exec_iam_role_policies = {
233+
task_exec_iam_role_policies = merge({
234234
"awslogs" : aws_iam_policy.cloudwatch_logs_policy.arn
235-
}
235+
}, var.enable_kms_encryption ? {
236+
"kms" : aws_iam_policy.kms_policy[0].arn
237+
} : {})
236238

237239
task_exec_secret_arns = [
238240
aws_secretsmanager_secret.directus_secret.arn,

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ output "load_balancer_listener_arn" {
2222
value = aws_lb_listener.directus_lb_listener.arn
2323
description = "The ARN of the load balancer listener"
2424
}
25+
26+
output "public_url" {
27+
value = local.public_url
28+
description = "The public URL of the Directus service"
29+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ variable "s3_bucket_versioning_configuration" {
6868
}
6969
}
7070

71+
variable "enable_kms_encryption" {
72+
description = "Whether to enable KMS encryption"
73+
type = bool
74+
default = false
75+
}
76+
7177
variable "kms_key_id" {
7278
description = "The ID of the KMS key"
7379
type = string

0 commit comments

Comments
 (0)