Skip to content

Commit 217ac76

Browse files
authored
feat: Add specific policy for Autoscaling service linked roles (#15)
1 parent f6d3c99 commit 217ac76

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.77.0
3+
rev: v1.77.1
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Terraform module which creates AWS KMS resources.
88

99
See [`examples`](https://github.com/terraform-aws-modules/terraform-aws-kms/tree/master/examples) directory for working examples to reference:
1010

11-
### Service
11+
### Autoscaling Service Linked Role
1212

1313
Reference usage for EC2 AutoScaling service linked role to launch encrypted EBS volumes:
1414

@@ -20,9 +20,8 @@ module "kms" {
2020
key_usage = "ENCRYPT_DECRYPT"
2121
2222
# Policy
23-
key_administrators = ["arn:aws:iam::012345678901:role/admin"]
24-
key_users = ["arn:aws:iam::012345678901:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"]
25-
key_service_users = ["arn:aws:iam::012345678901:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"]
23+
key_administrators = ["arn:aws:iam::012345678901:role/admin"]
24+
key_service_roles_for_autoscaling = ["arn:aws:iam::012345678901:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"]
2625
2726
# Aliases
2827
aliases = ["mycompany/ebs"]
@@ -200,6 +199,7 @@ No modules.
200199
| <a name="input_key_hmac_users"></a> [key\_hmac\_users](#input\_key\_hmac\_users) | A list of IAM ARNs for [key HMAC users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto) | `list(string)` | `[]` | no |
201200
| <a name="input_key_material_base64"></a> [key\_material\_base64](#input\_key\_material\_base64) | Base64 encoded 256-bit symmetric encryption key material to import. The CMK is permanently associated with this key material. External key only | `string` | `null` | no |
202201
| <a name="input_key_owners"></a> [key\_owners](#input\_key\_owners) | A list of IAM ARNs for those who will have full key permissions (`kms:*`) | `list(string)` | `[]` | no |
202+
| <a name="input_key_service_roles_for_autoscaling"></a> [key\_service\_roles\_for\_autoscaling](#input\_key\_service\_roles\_for\_autoscaling) | A list of IAM ARNs for [AWSServiceRoleForAutoScaling roles](https://docs.aws.amazon.com/autoscaling/ec2/userguide/key-policy-requirements-EBS-encryption.html#policy-example-cmk-access) | `list(string)` | `[]` | no |
203203
| <a name="input_key_service_users"></a> [key\_service\_users](#input\_key\_service\_users) | A list of IAM ARNs for [key service users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-service-integration) | `list(string)` | `[]` | no |
204204
| <a name="input_key_statements"></a> [key\_statements](#input\_key\_statements) | A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage | `any` | `{}` | no |
205205
| <a name="input_key_symmetric_encryption_users"></a> [key\_symmetric\_encryption\_users](#input\_key\_symmetric\_encryption\_users) | A list of IAM ARNs for [key symmetric encryption users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto) | `list(string)` | `[]` | no |

examples/complete/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module "kms_complete" {
3737
key_administrators = [local.current_identity]
3838
key_users = [local.current_identity]
3939
key_service_users = [local.current_identity]
40+
key_service_roles_for_autoscaling = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"]
4041
key_symmetric_encryption_users = [local.current_identity]
4142
key_hmac_users = [local.current_identity]
4243
key_asymmetric_public_encryption_users = [local.current_identity]

main.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,51 @@ data "aws_iam_policy_document" "this" {
197197
}
198198
}
199199

200+
# Key service roles for autoscaling - https://docs.aws.amazon.com/autoscaling/ec2/userguide/key-policy-requirements-EBS-encryption.html#policy-example-cmk-access
201+
dynamic "statement" {
202+
for_each = length(var.key_service_roles_for_autoscaling) > 0 ? [1] : []
203+
204+
content {
205+
sid = "KeyServiceRolesASG"
206+
actions = [
207+
"kms:Encrypt",
208+
"kms:Decrypt",
209+
"kms:ReEncrypt*",
210+
"kms:GenerateDataKey*",
211+
"kms:DescribeKey",
212+
]
213+
resources = ["*"]
214+
215+
principals {
216+
type = "AWS"
217+
identifiers = var.key_service_roles_for_autoscaling
218+
}
219+
}
220+
}
221+
222+
dynamic "statement" {
223+
for_each = length(var.key_service_roles_for_autoscaling) > 0 ? [1] : []
224+
225+
content {
226+
sid = "KeyServiceRolesASGPersistentVol"
227+
actions = [
228+
"kms:CreateGrant"
229+
]
230+
resources = ["*"]
231+
232+
principals {
233+
type = "AWS"
234+
identifiers = var.key_service_roles_for_autoscaling
235+
}
236+
237+
condition {
238+
test = "Bool"
239+
variable = "kms:GrantIsForAWSResource"
240+
values = [true]
241+
}
242+
}
243+
}
244+
200245
# Key cryptographic operations - https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto
201246
dynamic "statement" {
202247
for_each = length(var.key_symmetric_encryption_users) > 0 ? [1] : []

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ variable "key_service_users" {
116116
default = []
117117
}
118118

119+
variable "key_service_roles_for_autoscaling" {
120+
description = "A list of IAM ARNs for [AWSServiceRoleForAutoScaling roles](https://docs.aws.amazon.com/autoscaling/ec2/userguide/key-policy-requirements-EBS-encryption.html#policy-example-cmk-access)"
121+
type = list(string)
122+
default = []
123+
}
124+
119125
variable "key_symmetric_encryption_users" {
120126
description = "A list of IAM ARNs for [key symmetric encryption users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto)"
121127
type = list(string)

0 commit comments

Comments
 (0)