Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
argoyle committed Oct 17, 2021
0 parents commit 755daa3
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 opzkit

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# terraform-aws-rds-mysql

49 changes: 49 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
resource "aws_db_subnet_group" "default" {
name = "${var.identifier}-db-subnet-group"
subnet_ids = var.subnet_ids

tags = {
Name = "${var.identifier} DB subnet group"
}
}

data "aws_security_group" "security_groups" {
for_each = toset(var.security_group_names)
name = each.value
}

resource "aws_security_group" "allow_postgres" {
vpc_id = var.vpc_id
name = "allow-postgresql-${var.identifier}"

ingress {
from_port = 5432
protocol = "tcp"
to_port = 5432
security_groups = [for i, g in data.aws_security_group.security_groups : g.id]
}

egress {
from_port = 0
protocol = "-1"
to_port = 0
security_groups = [for i, g in data.aws_security_group.security_groups : g.id]
}
}

resource "aws_db_instance" "default" {
instance_class = var.instance_type
engine_version = var.postgresql_version
max_allocated_storage = 60
skip_final_snapshot = var.skip_final_snapshot
identifier = var.identifier
name = var.db_name
username = var.master_username
password = local.password
maintenance_window = "mon:02:00-mon:03:30"
backup_window = "03:30-05:00"
backup_retention_period = 14
allow_major_version_upgrade = true
apply_immediately = var.apply_immediately
db_subnet_group_name = aws_db_subnet_group.default.name
}
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "database_secret_name" {
value = aws_secretsmanager_secret.rds_secret.name
}
35 changes: 35 additions & 0 deletions secret.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
resource "aws_secretsmanager_secret" "rds_secret" {
name = "rds/postgres/${var.identifier}"
}

resource "aws_secretsmanager_secret_version" "rds_secret_value" {
secret_id = aws_secretsmanager_secret.rds_secret.id
secret_string = jsonencode(local.secret_value)
}

locals {
secret_value = {
DB_USERNAME = var.master_username
DB_PASSWORD = local.password
DB_NAME = aws_db_instance.default.identifier
DB_PORT = aws_db_instance.default.port
DB_HOST = aws_db_instance.default.endpoint
}
password = random_password.password.result
}

resource "random_password" "password" {
length = 32
special = false
lower = true
upper = true
number = true
override_special = ""
min_special = 0
min_lower = 5
min_upper = 5
min_numeric = 5
lifecycle {
ignore_changes = all
}
}
57 changes: 57 additions & 0 deletions vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
variable "identifier" {
type = string
description = "Instance identifier"
}

variable "db_name" {
type = string
description = "Initial database name"
}

variable "instance_type" {
type = string
description = "Instance type"
default = "db.t3.small"
}

variable "postgresql_version" {
type = string
description = "The postgresql version to use"
default = "13.3"
}

variable "master_username" {
type = string
description = "Username for master user"
}

variable "availability_zone" {
type = string
description = "The availability zone where the instance shall be created"
}

variable "vpc_id" {
type = string
}

variable "subnet_ids" {
type = list(string)
description = "List of subnet ids where cluster should be located"
}

variable "security_group_names" {
type = list(string)
description = "List of security group names that should have access to the DB cluster"
}

variable "skip_final_snapshot" {
type = bool
default = false
description = "Store final snapshot or not when destroying database"
}

variable "apply_immediately" {
type = bool
default = false
description = "Apply changes immediately or wait for next maintenance window"
}

0 comments on commit 755daa3

Please sign in to comment.