44resource "aws_kms_key" "aft" {
55 description = " AFT KMS key"
66 enable_key_rotation = " true"
7- }
8-
9- resource "aws_kms_alias" "aft" {
10- name = " alias/aft"
11- target_key_id = aws_kms_key. aft . key_id
12- }
13-
14- resource "aws_kms_key_policy" "aft" {
15- key_id = aws_kms_key. aft . id
7+ # Use inline policy instead of 'aws_kms_key_policy' resource
8+ # to always make sure policy is fully propagated before the key is used
9+ # e.g. for CloudWatch Log Groups
1610 policy = jsonencode (
1711 {
1812 " Version" : " 2012-10-17" ,
1913 " Id" : " key-default-1" ,
20- " Statement" : [
14+ " Statement" : concat ( [
2115 {
2216 " Sid" : " Enable IAM User Permissions" ,
2317 " Effect" : " Allow" ,
@@ -26,10 +20,8 @@ resource "aws_kms_key_policy" "aft" {
2620 },
2721 " Action" : " kms:*" ,
2822 " Resource" : " *"
29- },
30- # Reference: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html
31- {
32- " Sid" : " Allow CloudWatch Logs access" ,
23+ }], var. cloudwatch_log_group_enable_cmk_encryption ? [{
24+ " Sid" : " Allow CloudWatch Logs access" , # Reference: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html
3325 " Effect" : " Allow" ,
3426 " Principal" : {
3527 " Service" : " logs.${ data . aws_region . aft-management . name } .amazonaws.com"
@@ -48,8 +40,12 @@ resource "aws_kms_key_policy" "aft" {
4840 " kms:EncryptionContext:aws:logs:arn" : " arn:${ data . aws_partition . current . partition } :logs:${ data . aws_region . aft-management . name } :${ data . aws_caller_identity . aft-management . account_id } :*"
4941 }
5042 }
51- }
52- ]
53- }
54- )
43+ }] : [])
44+ })
45+ }
46+
47+
48+ resource "aws_kms_alias" "aft" {
49+ name = " alias/aft"
50+ target_key_id = aws_kms_key. aft . key_id
5551}
0 commit comments