Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions rds-terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# ==========================================================================
# Main Terraform Script to create the RDS instance based on passed values#
# ==========================================================================

terraform {

required_providers {
aws = {
source = "hashicorp/aws" # Official AWS provider from HashiCorp
version = "~> 5.0"
}
}
required_version = ">= 1.0"
}

# =============================================================================
# AWS PROVIDER CONFIGURATION
# =============================================================================
provider "aws" {
region = var.aws_region # region we specify in variables
}


# =============================================================================
# RDS POSTGRESQL DATABASE INSTANCE
# =============================================================================

resource "aws_db_instance" "postgres_db" {

# -------------------------------------------------------------------------
# BASIC DATABASE CONFIGURATION
# -------------------------------------------------------------------------
identifier = "${var.environment}-postgres-db"
engine = "postgres"
instance_class = var.db_instance_class

# -------------------------------------------------------------------------
# STORAGE CONFIGURATION
# -------------------------------------------------------------------------
allocated_storage = var.allocated_storage
storage_type = "gp2" # General Purpose SSD (cheapest option)
storage_encrypted = true # Encrypt data at rest (FREE security feature)

# -------------------------------------------------------------------------
# DATABASE ACCESS CREDENTIALS
# -------------------------------------------------------------------------
db_name = var.database_name # Name of the database inside PostgreSQL
username = var.db_username # Admin username for database

password = var.db_password

# -------------------------------------------------------------------------
# NETWORK SECURITY CONFIGURATION
# -------------------------------------------------------------------------
vpc_security_group_ids = [var.vpc_security_group_id]
publicly_accessible = false # NO public internet access


# -------------------------------------------------------------------------
# BACKUP CONFIGURATION
# -------------------------------------------------------------------------
backup_retention_period = var.backup_retention_period # How many days to keep backups

# -------------------------------------------------------------------------
# CLEANUP CONFIGURATION
# -------------------------------------------------------------------------
skip_final_snapshot = var.skip_final_snapshot # When deleting DB:
# true = Delete immediately
# false = Take final backup before deleting (costs extra)

# -------------------------------------------------------------------------
# OPTIONAL: RESOURCE TAGS FOR ORGANIZATION
# -------------------------------------------------------------------------
# These are just labels - they don't cost anything but help organize resources
tags = {
Name = "${var.environment}-postgres-db"
Environment = var.environment
}
}

46 changes: 46 additions & 0 deletions rds-terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@


# =============================================================================
# DATABASE CONNECTION INFORMATION
# =============================================================================

output "db_endpoint" {
description = "Database server address/hostname for connections"
value = aws_db_instance.postgres_db.endpoint

}

output "db_port" {
description = "Database port number"
value = aws_db_instance.postgres_db.port

}

output "db_name" {
description = "Database name to connect to"
value = aws_db_instance.postgres_db.db_name
}

output "db_username" {
description = "Database admin username"
value = aws_db_instance.postgres_db.username
sensitive = true
}


# =============================================================================
# AWS RESOURCE INFORMATION
# =============================================================================

output "db_instance_id" {
description = "AWS RDS instance identifier"
value = aws_db_instance.postgres_db.id

}

output "db_security_group_id" {
description = "Security group ID attached to the database"
value = var.vpc_security_group_id

}

12 changes: 12 additions & 0 deletions rds-terraform/pulumi/Pulumi.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
config:
# Required Configuration
rds-deployment:aws_region: "eu-west-1"
rds-deployment:environment: "dev"
rds-deployment:db_instance_class: "db.t3.micro"
rds-deployment:vpc_security_group_id: "" # Replace with your SG ID

# Provide Database Credentials
rds-deployment:database_name: ""
rds-deployment:db_username: ""
rds-deployment:db_password:
secure: ""
3 changes: 3 additions & 0 deletions rds-terraform/pulumi/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: rds-qa-infra
runtime: python
description: Pulumi project for deploying the QA PostgreSQL RDS instance in eu-west-1
28 changes: 28 additions & 0 deletions rds-terraform/pulumi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# RDS PostgreSQL Deployment - DEV Environment

Deploy AWS RDS PostgreSQL database for development using Pulumi.

## Quick Start

### 1. Install Requirements
```bash
pip install pulumi pulumi-aws
```
### 2. Edit Configuration

Open `Pulumi.dev.yaml` and replace:
- `` with your actual AWS Security Group ID

### 3. Deploy
```bash
# Preview what will be created
pulumi preview

# Create the database
pulumi up
```

## 4. Delete Everything
```bash
pulumi destroy
```
44 changes: 44 additions & 0 deletions rds-terraform/pulumi/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Pulumi script to deploy RDS PostgreSQL instance
"""

import pulumi
import pulumi_aws as aws

# Read configuration values
config = pulumi.Config()
aws_region = config.require("aws_region")
environment = config.require("environment")
db_instance_class = config.require("db_instance_class")
vpc_security_group_id = config.require("vpc_security_group_id")

# Optional configs
database_name = config.get("database_name")
db_username = config.get("db_username")
db_password = config.require("db_password") # Must provide password for now

# Create RDS instance
rds_instance = aws.rds.Instance(
"postgres-db",
identifier=f"{environment}-postgres-db",
engine="postgres",
instance_class=db_instance_class,
allocated_storage=20,
storage_type="gp2",
storage_encrypted=True,
db_name=database_name,
username=db_username,
password=db_password,
vpc_security_group_ids=[vpc_security_group_id],
publicly_accessible=False,
skip_final_snapshot=True,
backup_retention_period=0,
tags={
"Name": f"{environment}-postgres-db",
"Environment": environment,
}
)

# Export basic outputs
pulumi.export("db_endpoint", rds_instance.endpoint)
pulumi.export("db_name", rds_instance.db_name)
19 changes: 19 additions & 0 deletions rds-terraform/qa.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# =============================================================================
# tfvars to pass parameterized inputs tot he terraform script
# =============================================================================
# 1. We can create similar tfvars for different instances, this way the main terraform deployment script stays untouched and we can deploy the instance across different regions with custom parameters
# ===============================================================================

aws_region = "eu-west-1" # Placeholder for EU region, will update accordingly
environment = "qa" # environment
db_instance_class = "db.t3.micro"
vpc_security_group_id = "" # saayam vpc group id

# Optional customizations
database_name = "saayam_qa_db" # Placeholder Database name will update accordingly
db_username = "qa_admin" # Username
db_password = "" # need to discuss on appropriate password passing approach
allocated_storage = 20
backup_retention_period = 0
skip_final_snapshot = true

Loading