-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
134 lines (125 loc) · 3.23 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Create a KMS key
resource "aws_kms_key" "myqsl_kms_key" {
description = "KMS key for RDS encryption"
enable_key_rotation = true
rotation_period_in_days = 100
tags = {
Name = "MySQL KMS Key"
}
}
# Create a KMS key alias
resource "aws_kms_alias" "myqsl_kms_alias" {
name = "alias/${var.kms_key_alias}"
target_key_id = aws_kms_key.myqsl_kms_key.id
}
# Define the key policy
resource "aws_kms_key_policy" "myqsl_kms_policy" {
key_id = aws_kms_key.myqsl_kms_key.id
policy = <<EOF
{
"Version": "2012-10-17",
"Id": "key-default-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "Allow access for Key Administrators",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::${var.account_id}:root"
]
},
"Action": [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
"Resource": "*"
},
{
"Sid": "Allow use of the key",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::${var.account_id}:user/admin"
]
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
},
{
"Sid": "Allow attachment of persistent resources",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::${var.account_id}:user/admin"
]
},
"Action": [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant"
],
"Resource": "*",
"Condition": {
"Bool": {
"kms:GrantIsForAWSResource": "true"
}
}
}
]
}
EOF
}
#Create Database
resource "aws_db_instance" "mysql" {
allocated_storage = var.allocated_storage
engine = "mysql"
engine_version = var.engine_version
instance_class = var.instance_class
db_name = var.db_name
username = var.username
manage_master_user_password = true
parameter_group_name = var.parameter_group_name
skip_final_snapshot = false
final_snapshot_identifier = "final-snapshot-${var.db_name}"
publicly_accessible = false
# Encryption settings
storage_encrypted = true
kms_key_id = aws_kms_key.myqsl_kms_key.arn
# Security groups
vpc_security_group_ids = var.vpc_security_group_ids
# Backup settings
backup_retention_period = 7
backup_window = "07:00-09:00"
# Maintenance settings
maintenance_window = "Mon:00:00-Mon:03:00"
tags = {
Name = "MySQL Database"
}
}