Skip to content

Commit 6ce1780

Browse files
committed
Add ephemeral storage encryption
1 parent 61d9630 commit 6ce1780

File tree

4 files changed

+118
-12
lines changed

4 files changed

+118
-12
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ performance and health.
4545
| Name | Description | Type | Default | Required |
4646
|------|-------------|------|---------|:--------:|
4747
| <a name="input_enable_container_insights"></a> [enable\_container\_insights](#input\_enable\_container\_insights) | Enable CloudWatch Container Insights for the cluster. | `bool` | `true` | no |
48+
| <a name="input_encrypt_ephemeral_storage"></a> [encrypt\_ephemeral\_storage](#input\_encrypt\_ephemeral\_storage) | Encrypt the ECS ephemeral storage for the cluster. | `bool` | `false` | no |
4849
| <a name="input_encrypt_execute_command_session"></a> [encrypt\_execute\_command\_session](#input\_encrypt\_execute\_command\_session) | Encrypt execute command session for the cluster. | `bool` | `false` | no |
4950
| <a name="input_logging_execute_command_session"></a> [logging\_execute\_command\_session](#input\_logging\_execute\_command\_session) | Log execute command session for the cluster. | `string` | `"DEFAULT"` | no |
5051
| <a name="input_name"></a> [name](#input\_name) | Name of the ECS cluster. | `string` | n/a | yes |
@@ -63,13 +64,15 @@ performance and health.
6364

6465
| Name | Version |
6566
|------|---------|
66-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.36 |
67+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
6768

6869
## Resources
6970

70-
- resource.aws_cloudwatch_log_group.container_insights (main.tf#62)
71-
- resource.aws_cloudwatch_log_group.main (main.tf#55)
71+
- resource.aws_cloudwatch_log_group.container_insights (main.tf#75)
72+
- resource.aws_cloudwatch_log_group.main (main.tf#68)
7273
- resource.aws_ecs_cluster.main (main.tf#10)
74+
- data source.aws_caller_identity.current (main.tf#95)
75+
- data source.aws_iam_policy_document.kms_ephemeral (main.tf#96)
7376

7477
# Examples
7578
### Basic Example

main.tf

+105-8
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,36 @@ resource "aws_ecs_cluster" "main" {
1919
}
2020
}
2121

22+
2223
dynamic "configuration" {
2324
for_each = var.encrypt_execute_command_session || var.logging_execute_command_session != "DEFAULT" ? [true] : []
2425

2526
content {
26-
execute_command_configuration {
27-
kms_key_id = var.encrypt_execute_command_session ? module.kms[0].key_id : null
28-
logging = var.logging_execute_command_session
27+
dynamic "execute_command_configuration" {
28+
for_each = var.encrypt_execute_command_session || var.logging_execute_command_session != "DEFAULT" ? [true] : []
29+
30+
content {
31+
kms_key_id = var.encrypt_execute_command_session ? module.kms[0].key_id : null
32+
logging = var.logging_execute_command_session
2933

30-
dynamic "log_configuration" {
31-
for_each = var.logging_execute_command_session == "OVERRIDE" ? [true] : []
34+
dynamic "log_configuration" {
35+
for_each = var.logging_execute_command_session == "OVERRIDE" ? [true] : []
3236

33-
content {
34-
cloud_watch_encryption_enabled = false
35-
cloud_watch_log_group_name = aws_cloudwatch_log_group.main[0].name
37+
content {
38+
cloud_watch_encryption_enabled = false
39+
cloud_watch_log_group_name = aws_cloudwatch_log_group.main[0].name
40+
}
3641
}
3742
}
3843
}
44+
45+
dynamic "managed_storage_configuration" {
46+
for_each = var.encrypt_ephemeral_storage ? [true] : []
47+
48+
content {
49+
fargate_ephemeral_storage_kms_key_id = module.kms_ephemeral[0].key_id
50+
}
51+
}
3952
}
4053
}
4154

@@ -67,3 +80,87 @@ resource "aws_cloudwatch_log_group" "container_insights" {
6780

6881
tags = var.tags
6982
}
83+
84+
module "kms_ephemeral" {
85+
count = var.encrypt_ephemeral_storage ? 1 : 0
86+
87+
source = "geekcell/kms/aws"
88+
version = ">= 1.0.0, < 2.0.0"
89+
policy = data.aws_iam_policy_document.kms_ephemeral[0].json
90+
91+
alias = "ecs/cluster/${var.name}/ephemeral-storage"
92+
tags = var.tags
93+
}
94+
95+
data "aws_caller_identity" "current" {}
96+
data "aws_iam_policy_document" "kms_ephemeral" {
97+
count = var.encrypt_ephemeral_storage ? 1 : 0
98+
99+
statement {
100+
sid = "Enable IAM User Permissions."
101+
effect = "Allow"
102+
actions = ["kms:*"]
103+
resources = ["*"]
104+
105+
principals {
106+
identifiers = ["*"]
107+
type = "AWS"
108+
}
109+
}
110+
111+
statement {
112+
sid = "Allow generate data key access for Fargate tasks."
113+
effect = "Allow"
114+
actions = ["kms:GenerateDataKeyWithoutPlaintext"]
115+
116+
principals {
117+
identifiers = ["fargate.amazonaws.com"]
118+
type = "Service"
119+
}
120+
121+
condition {
122+
test = "StringEquals"
123+
variable = "kms:EncryptionContext:aws:ecs:clusterAccount"
124+
values = [data.aws_caller_identity.current.account_id]
125+
}
126+
127+
condition {
128+
test = "StringEquals"
129+
variable = "kms:EncryptionContext:aws:ecs:clusterName"
130+
values = [var.name]
131+
}
132+
133+
resources = ["*"]
134+
}
135+
136+
statement {
137+
sid = "Allow grant creation permission for Fargate tasks."
138+
effect = "Allow"
139+
actions = ["kms:CreateGrant"]
140+
141+
principals {
142+
identifiers = ["fargate.amazonaws.com"]
143+
type = "Service"
144+
}
145+
146+
condition {
147+
test = "StringEquals"
148+
variable = "kms:EncryptionContext:aws:ecs:clusterAccount"
149+
values = [data.aws_caller_identity.current.account_id]
150+
}
151+
152+
condition {
153+
test = "StringEquals"
154+
variable = "kms:EncryptionContext:aws:ecs:clusterName"
155+
values = [var.name]
156+
}
157+
158+
condition {
159+
test = "ForAllValues:StringEquals"
160+
variable = "kms:GrantOperations"
161+
values = ["Decrypt"]
162+
}
163+
164+
resources = ["*"]
165+
}
166+
}

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ variable "encrypt_execute_command_session" {
2323
type = bool
2424
}
2525

26+
variable "encrypt_ephemeral_storage" {
27+
description = "Encrypt the ECS ephemeral storage for the cluster."
28+
default = false
29+
type = bool
30+
}
31+
2632
variable "logging_execute_command_session" {
2733
description = "Log execute command session for the cluster."
2834
default = "DEFAULT"

versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.36"
7+
version = ">= 5.59"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)