Skip to content

Commit 8436f61

Browse files
committed
added aws_db_instance & aws_db_parameter_group logic
1 parent 40be0ba commit 8436f61

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

.gitignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# our main tfvars file
2+
backend.tf
3+
4+
## Taken from https://github.com/github/gitignore/blob/main/Terraform.gitignore
5+
6+
# Local .terraform directories
7+
**/.terraform/*
8+
9+
# .tfstate files
10+
*.tfstate
11+
*.tfstate.*
12+
13+
# Crash log files
14+
crash.log
15+
crash.*.log
16+
17+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
18+
# password, private keys, and other secrets. These should not be part of version
19+
# control as they are data points which are potentially sensitive and subject
20+
# to change depending on the environment.
21+
*.tfvars
22+
*.tfvars.json
23+
24+
# Ignore override files as they are usually used to override resources locally and so
25+
# are not checked in
26+
override.tf
27+
override.tf.json
28+
*_override.tf
29+
*_override.tf.json
30+
31+
# Include override files you do wish to add to version control using negated pattern
32+
# !example_override.tf
33+
34+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
35+
# example: *tfplan*
36+
37+
# Ignore CLI configuration files
38+
.terraformrc
39+
terraform.rc
40+
41+
**/.terraform
42+
**/.terraform.lock.hcl
43+
**/terraform.tfstate
44+
**/terraform.tfstate.backup
45+
.DS_Store

main.tf

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ provider "aws" {
22
region = var.region
33
}
44

5-
data "terrafom_remote_state" "aws-networking" {
5+
data "terraform_remote_state" "aws-networking" {
66
backend = "remote"
77

88
config = {
@@ -13,25 +13,44 @@ data "terrafom_remote_state" "aws-networking" {
1313
}
1414
}
1515

16-
resource "random_password" "master" {
16+
resource "random_password" "db_password" {
1717
length = 16
1818
special = true
1919
}
2020

21-
resource "aws_rds_cluster" "aurora_pg" {
22-
cluster_identifier = "aurora-psql-${var.db_name}"
23-
engine = "aurora-postgresql"
24-
master_username = var.db_admin
25-
master_password = random_password.master.result
26-
database_name = var.db_name
27-
db_subnet_group_name = data.terrafom_remote_state.aws-networking.outputs.db_subnet_group_name
28-
skip_final_snapshot = true
21+
resource "random_string" "random_str" {
22+
length = 6
23+
numeric = false
24+
special = false
25+
upper = false
2926
}
3027

31-
resource "aws_rds_cluster_instance" "aurora_pg_instance" {
32-
count = 1
33-
identifier = "aurora-psql-${var.db_name}-instance"
34-
cluster_identifier = aws_rds_cluster.aurora_pg.id
35-
instance_class = var.instance_class
36-
engine = aws_rds_cluster.aurora_pg.engine
28+
resource "aws_db_parameter_group" "db_param_group" {
29+
name = "${var.db_name}-${random_string.random_str.id}"
30+
family = "postgres16"
31+
32+
parameter {
33+
name = "log_connections"
34+
value = "1"
35+
}
36+
37+
lifecycle {
38+
create_before_destroy = true
39+
}
40+
}
41+
42+
resource "aws_db_instance" "db" {
43+
identifier = "${var.db_name}-${random_string.random_str.id}"
44+
instance_class = "db.t3.micro"
45+
allocated_storage = 5
46+
apply_immediately = true
47+
engine = "postgres"
48+
engine_version = "16.6"
49+
username = var.db_admin
50+
password = resource.random_password.db_password.result
51+
db_subnet_group_name = data.terraform_remote_state.aws-networking.outputs.db_subnet_group_name
52+
vpc_security_group_ids = [data.terraform_remote_state.aws-networking.outputs.psql_security_group_id.id]
53+
parameter_group_name = aws_db_parameter_group.db_param_group.name
54+
publicly_accessible = true
55+
skip_final_snapshot = true
3756
}

variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ variable "instance_class" {
2626
description = "Instance Size"
2727
type = string
2828
validation {
29-
condition = contains(["db.r6g.large", "db.r6g.xlarge", "db.r6g.2xlarge"], var.instance_class)
30-
error_message = "Supported sizes: db.r6g.large, db.r6g.xlarge, db.r6g.2xlarge"
29+
condition = contains(["db.t3.micro"], var.instance_class)
30+
error_message = "Supported sizes: db.t3.micro"
3131
}
3232
}

0 commit comments

Comments
 (0)