Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mwkaufman committed Jun 14, 2020
1 parent a320a3a commit 821edb7
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
30 changes: 30 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
locals {
stack = var.stack != "" ? var.stack : var.environment
}

resource "random_password" "random_db_password" {
length = 30
special = true
override_special = "!#$%&_"
}

resource "random_id" "final_snapshot_id" {
byte_length = 4
}

resource "aws_db_instance" "db" {
identifier = "${local.stack}-${var.app_name}"
snapshot_identifier = var.snapshot_identifier
allocated_storage = var.snapshot_identifier != "" ? null : var.allocated_storage
storage_type = "gp2"
engine = var.snapshot_identifier != "" ? null : "${var.db_engine}"
engine_version = var.snapshot_identifier != "" ? null : "${var.db_engine_version}"
instance_class = "${var.db_instance_class}"
name = var.snapshot_identifier != "" ? null : "${var.db_name}"
username = var.snapshot_identifier != "" ? null : "${var.db_username}"
password = "${aws_ssm_parameter.password.value}"
vpc_security_group_ids = "${var.vpc_security_group_ids}"
db_subnet_group_name = "${var.subnet_group_name}"

final_snapshot_identifier = "A${random_id.final_snapshot_id.hex}"
}
46 changes: 46 additions & 0 deletions ssm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
resource "aws_ssm_parameter" "password" {
name = "/${var.environment}/${var.app_name}/${local.stack}/DB_PASSWORD"
description = "db password"
type = "SecureString"
value = "${random_password.random_db_password.result}"
overwrite = "true"
tags = {
environment = "${var.environment}"
}
}

resource "aws_ssm_parameter" "host" {
name = "/${var.environment}/${var.app_name}/${local.stack}/DB_HOST"
description = "db host"
type = "SecureString"
value = "${aws_db_instance.db.address}"
overwrite = "true"

tags = {
environment = "${var.environment}"
}
}

resource "aws_ssm_parameter" "name" {
name = "/${var.environment}/${var.app_name}/${local.stack}/DB_NAME"
description = "db name"
type = "SecureString"
value = "${aws_db_instance.db.name}"
overwrite = "true"

tags = {
environment = "${var.environment}"
}
}

resource "aws_ssm_parameter" "username" {
name = "/${var.environment}/${var.app_name}/${local.stack}/DB_USERNAME"
description = "db username"
type = "SecureString"
value = "${aws_db_instance.db.username}"
overwrite = "true"

tags = {
environment = "${var.environment}"
}
}
60 changes: 60 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
variable "app_name" {
type = string
description = "Name of the app to create an RDS instance for"
}

variable "environment" {
type = string
description = "Infrastructure environment, e.g. staging or production"
default = "staging"
}

variable "stack" {
type = string
description = "Name to differentiate applications deployed in the same infrastructure environment"
default = ""
}

variable "subnet_group_name" {
type = string
default = "db_subnet_group"
}

variable "allocated_storage" {


}

variable "db_engine" {


}

variable "db_engine_version" {


}

variable "db_instance_class" {


}

variable "db_name" {


}

variable "db_username" {


}

variable "vpc_security_group_ids" {
type = list(string)
}

variable "snapshot_identifier" {
type = string
default = ""
}

0 comments on commit 821edb7

Please sign in to comment.