1
1
provider "aws" {
2
- region = " us-east-1 "
2
+ region = local . region
3
3
}
4
4
5
5
locals {
6
+ name = " complete-mssql"
7
+ region = " eu-west-1"
6
8
tags = {
7
9
Owner = " user"
8
10
Environment = " dev"
9
11
}
10
12
}
11
13
12
- # #############################################################
13
- # Data sources to get VPC, subnets and security group details
14
- # #############################################################
15
- data "aws_vpc" "default" {
16
- default = true
17
- }
14
+ # ###############################################################################
15
+ # Supporting Resources
16
+ # ###############################################################################
17
+
18
+ module "vpc" {
19
+ source = " terraform-aws-modules/vpc/aws"
20
+ version = " ~> 2"
21
+
22
+ name = local. name
23
+ cidr = " 10.99.0.0/18"
24
+
25
+ azs = [" ${ local . region } a" , " ${ local . region } b" , " ${ local . region } c" ]
26
+ public_subnets = [" 10.99.0.0/24" , " 10.99.1.0/24" , " 10.99.2.0/24" ]
27
+ private_subnets = [" 10.99.3.0/24" , " 10.99.4.0/24" , " 10.99.5.0/24" ]
28
+ database_subnets = [" 10.99.7.0/24" , " 10.99.8.0/24" , " 10.99.9.0/24" ]
18
29
19
- data "aws_subnet_ids" "all" {
20
- vpc_id = data. aws_vpc . default . id
30
+ create_database_subnet_group = true
31
+
32
+ tags = local. tags
21
33
}
22
34
23
- data "aws_security_group" "default" {
24
- vpc_id = data. aws_vpc . default . id
25
- name = " default"
35
+ module "security_group" {
36
+ source = " terraform-aws-modules/security-group/aws"
37
+ version = " ~> 3"
38
+
39
+ name = local. name
40
+ description = " Complete SqlServer example security group"
41
+ vpc_id = module. vpc . vpc_id
42
+
43
+ # ingress
44
+ ingress_with_cidr_blocks = [
45
+ {
46
+ from_port = 1433
47
+ to_port = 1433
48
+ protocol = " tcp"
49
+ description = " SqlServer access from within VPC"
50
+ cidr_blocks = module.vpc.vpc_cidr_block
51
+ },
52
+ ]
53
+
54
+ tags = local. tags
26
55
}
27
56
28
- # ####################################
57
+ # ###############################################################################
29
58
# IAM Role for Windows Authentication
30
- # ####################################
59
+ # ###############################################################################
31
60
32
61
data "aws_iam_policy_document" "rds_assume_role" {
33
62
statement {
@@ -58,9 +87,9 @@ resource "aws_iam_role_policy_attachment" "rds_directory_services" {
58
87
policy_arn = " arn:aws:iam::aws:policy/service-role/AmazonRDSDirectoryServiceAccess"
59
88
}
60
89
61
- # #########################################
90
+ # ###############################################################################
62
91
# AWS Directory Service (Acitve Directory)
63
- # #########################################
92
+ # ###############################################################################
64
93
65
94
resource "aws_directory_service_directory" "demo" {
66
95
name = " corp.demo.com"
@@ -69,63 +98,61 @@ resource "aws_directory_service_directory" "demo" {
69
98
type = " MicrosoftAD"
70
99
71
100
vpc_settings {
72
- vpc_id = data . aws_vpc . default . id
101
+ vpc_id = module . vpc . vpc_id
73
102
# Only 2 subnets, must be in different AZs
74
- subnet_ids = slice (tolist (data . aws_subnet_ids . all . ids ), 0 , 2 )
103
+ subnet_ids = slice (tolist (module . vpc . database_subnets ), 0 , 2 )
75
104
}
76
105
77
106
tags = local. tags
78
107
}
79
108
80
- # ####
81
- # DB
82
- # ####
109
+ # ###############################################################################
110
+ # RDS Module
111
+ # ###############################################################################
83
112
84
113
module "db" {
85
114
source = " ../../"
86
115
87
- identifier = " demodb "
116
+ identifier = local . name
88
117
89
- engine = " sqlserver-ex"
90
- engine_version = " 14 .00.1000.169 .v1"
91
- instance_class = " db.t2.medium "
92
- allocated_storage = 20
93
- storage_encrypted = false
118
+ engine = " sqlserver-ex"
119
+ engine_version = " 15 .00.4073.23 .v1"
120
+ family = " sqlserver-ex-15.0 " # DB parameter group
121
+ major_engine_version = " 15.00 " # DB option group
122
+ instance_class = " db.t3.large "
94
123
95
- name = null # "demodb"
96
- username = " demouser"
124
+ allocated_storage = 20
125
+ max_allocated_storage = 100
126
+ storage_encrypted = false
127
+
128
+ name = null
129
+ username = " complete_mssql"
97
130
password = " YourPwdShouldBeLongAndSecure!"
98
- port = " 1433"
131
+ port = 1433
99
132
100
133
domain = aws_directory_service_directory. demo . id
101
134
domain_iam_role_name = aws_iam_role. rds_ad_auth . name
102
135
103
- vpc_security_group_ids = [data . aws_security_group . default . id ]
104
-
105
- maintenance_window = " Mon:00:00-Mon:03:00"
106
- backup_window = " 03:00-06:00"
136
+ multi_az = false
137
+ subnet_ids = module. vpc . database_subnets
138
+ vpc_security_group_ids = [module . security_group . this_security_group_id ]
107
139
108
- # disable backups to create DB faster
109
- backup_retention_period = 0
110
-
111
- tags = local. tags
140
+ maintenance_window = " Mon:00:00-Mon:03:00"
141
+ backup_window = " 03:00-06:00"
142
+ enabled_cloudwatch_logs_exports = [" error" ]
112
143
113
- # DB subnet group
114
- subnet_ids = data. aws_subnet_ids . all . ids
144
+ backup_retention_period = 0
145
+ final_snapshot_identifier = local. name
146
+ deletion_protection = false
115
147
116
- # Snapshot name upon DB deletion
117
- final_snapshot_identifier = " demodb"
148
+ performance_insights_enabled = true
149
+ performance_insights_retention_period = 7
150
+ create_monitoring_role = true
118
151
152
+ options = []
119
153
create_db_parameter_group = false
120
154
license_model = " license-included"
155
+ timezone = " GMT Standard Time"
121
156
122
- timezone = " Central Standard Time"
123
-
124
- # Database Deletion Protection
125
- deletion_protection = false
126
-
127
- # DB options
128
- major_engine_version = " 14.00"
129
-
130
- options = []
157
+ tags = local. tags
131
158
}
0 commit comments