-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.tf
89 lines (81 loc) · 4.58 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
##----------------------------------------------------------------------------------
## Labels module callled that will be used for naming and tags.
##----------------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
name = var.name
enabled = var.enabled
repository = var.repository
environment = var.environment
managedby = var.managedby
label_order = var.label_order
attributes = var.attributes
}
####----------------------------------------------------------------------------------
## This terraform resource creates a KMS Customer Master Key (CMK) and its alias.
####----------------------------------------------------------------------------------
resource "aws_kms_key" "default" {
count = var.enabled && var.kms_key_enabled ? 1 : 0
description = var.description
key_usage = var.key_usage
deletion_window_in_days = var.deletion_window_in_days
is_enabled = var.kms_key_enabled
enable_key_rotation = var.enable_key_rotation
customer_master_key_spec = var.customer_master_key_spec
policy = var.policy
multi_region = var.multi_region
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
tags = module.labels.tags
}
####----------------------------------------------------------------------------------
## Create KMS keys in an external key store backed by your cryptographic keys outside of AWS.
####----------------------------------------------------------------------------------
resource "aws_kms_external_key" "external" {
count = var.enabled && var.create_external_enabled ? 1 : 0
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
deletion_window_in_days = var.deletion_window_in_days
description = var.description
enabled = var.create_external_enabled
key_material_base64 = var.key_material_base64
multi_region = var.multi_region
policy = var.policy
valid_to = var.valid_to
tags = module.labels.tags
}
####----------------------------------------------------------------------------------
## Replica Key
####----------------------------------------------------------------------------------
resource "aws_kms_replica_key" "replica" {
count = var.enabled && var.create_replica_enabled ? 1 : 0
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
deletion_window_in_days = var.deletion_window_in_days
description = var.description
primary_key_arn = var.primary_key_arn
enabled = var.create_replica_enabled
policy = var.policy
tags = module.labels.tags
}
####----------------------------------------------------------------------------------
## Replica External Key
####----------------------------------------------------------------------------------
resource "aws_kms_replica_external_key" "replica_external" {
count = var.enabled && var.create_replica_external_enabled ? 1 : 0
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
deletion_window_in_days = var.deletion_window_in_days
description = var.description
enabled = var.create_replica_external_enabled
key_material_base64 = var.key_material_base64
policy = var.policy
primary_key_arn = var.primary_external_key_arn
valid_to = var.valid_to
tags = module.labels.tags
}
##----------------------------------------------------------------------------------
## Provides an alias for a KMS customer master key.
##----------------------------------------------------------------------------------
resource "aws_kms_alias" "default" {
count = var.enabled ? 1 : 0
name = coalesce(var.alias, format("alias/%v", module.labels.id))
target_key_id = try(aws_kms_key.default[0].key_id, aws_kms_external_key.external[0].id, aws_kms_replica_key.replica[0].key_id, aws_kms_replica_external_key.replica_external[0].key_id, null)
}