Skip to content

Commit 7fa2fc3

Browse files
committed
feat: AWS RDS plugins
1 parent d142e3f commit 7fa2fc3

10 files changed

Lines changed: 1064 additions & 0 deletions

File tree

rds-database/icon.svg

Lines changed: 17 additions & 0 deletions
Loading

rds-database/manifest.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: rds-database
2+
display_name: RDS PostgreSQL Database
3+
type: database
4+
description: "Creates a PostgreSQL database on an existing RDS instance with automatic credential generation and service injection"
5+
icon: ./icon.svg
6+
deployment:
7+
terraform: ./module
8+
9+
inputs:
10+
rds_instance_endpoint:
11+
type: string
12+
required: true
13+
description: "Connection endpoint of the RDS instance in format hostname:port (e.g. `mydb.abc123.us-east-1.rds.amazonaws.com:5432`)"
14+
15+
codebuild_project_name:
16+
type: string
17+
required: true
18+
description: "Name of the CodeBuild project for database operations (from RDS instance)"
19+
20+
database_name:
21+
type: string
22+
required: false
23+
description: "Name of the database to create. If not specified, uses stack_id and resource name (e.g. `myapp_db`)"
24+
25+
database_owner:
26+
type: string
27+
required: false
28+
description: "Username for the database owner role. If not specified, a unique role is created (e.g. `myapp_user`)"
29+
30+
outputs:
31+
database_name:
32+
type: string
33+
description: "Name of the created database"
34+
35+
database_owner:
36+
type: string
37+
description: "Username of the database owner"
38+
39+
database_password:
40+
type: string
41+
description: "Password for the database owner (sensitive)"
42+
43+
connection_string:
44+
type: string
45+
description: "PostgreSQL connection string for the database"

rds-database/module/main.tf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Local variables
2+
locals {
3+
database_name = var.database_name != null ? var.database_name : replace("${var.suga.stack_id}_${var.suga.name}", "-", "_")
4+
database_owner = var.database_owner != null ? var.database_owner : replace("${var.suga.stack_id}_${var.suga.name}_user", "-", "_")
5+
6+
# Build PostgreSQL connection string
7+
connection_string = "postgresql://${local.database_owner}:${random_password.db_password.result}@${var.rds_instance_endpoint}/${local.database_name}?sslmode=require"
8+
9+
# Output service export map
10+
service_outputs = {
11+
for name, service in var.suga.services : name => {
12+
env = {
13+
(var.suga.env_var_key) = local.connection_string
14+
}
15+
}
16+
}
17+
}
18+
19+
# Generate a random password for the database owner role
20+
resource "random_password" "db_password" {
21+
length = 32
22+
special = false
23+
}
24+
25+
# Trigger CodeBuild to create the database and role
26+
resource "null_resource" "create_database" {
27+
triggers = {
28+
database_name = local.database_name
29+
database_owner = local.database_owner
30+
rds_endpoint = var.rds_instance_endpoint
31+
codebuild_project = var.codebuild_project_name
32+
}
33+
34+
provisioner "local-exec" {
35+
interpreter = ["bash", "-c"]
36+
command = <<EOF
37+
BUILD_ID=$(aws codebuild start-build \
38+
--project-name ${var.codebuild_project_name} \
39+
--environment-variables-override '${jsonencode([
40+
{
41+
name = "DB_NAME"
42+
value = local.database_name
43+
},
44+
{
45+
name = "DB_ROLE"
46+
value = local.database_owner
47+
},
48+
{
49+
name = "DB_ROLE_PASSWORD"
50+
value = random_password.db_password.result
51+
}
52+
])}' \
53+
--query 'build.id' --output text)
54+
STATUS="IN_PROGRESS"
55+
while [[ $STATUS == "IN_PROGRESS" ]]; do
56+
sleep 5
57+
STATUS=$(aws codebuild batch-get-builds --ids $BUILD_ID --query 'builds[0].buildStatus' --output text)
58+
done
59+
if [[ $STATUS != "SUCCEEDED" ]]; then
60+
echo "Build failed with status $STATUS"
61+
exit 1
62+
fi
63+
EOF
64+
}
65+
}

rds-database/module/outputs.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
output "database_name" {
2+
description = "Name of the created database"
3+
value = local.database_name
4+
}
5+
6+
output "database_owner" {
7+
description = "Username of the database owner"
8+
value = local.database_owner
9+
}
10+
11+
output "database_password" {
12+
description = "Password for the database owner"
13+
value = random_password.db_password.result
14+
sensitive = true
15+
}
16+
17+
output "connection_string" {
18+
description = "PostgreSQL connection string for the database"
19+
value = local.connection_string
20+
sensitive = true
21+
}
22+
23+
output "suga" {
24+
value = {
25+
id = local.database_name
26+
exports = {
27+
# Export known service outputs
28+
services = local.service_outputs
29+
resources = {}
30+
}
31+
}
32+
}

rds-database/module/variables.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
variable "rds_instance_endpoint" {
2+
type = string
3+
description = "Connection endpoint of the RDS instance in format hostname:port"
4+
}
5+
6+
variable "codebuild_project_name" {
7+
type = string
8+
description = "Name of the CodeBuild project for database operations"
9+
}
10+
11+
variable "database_name" {
12+
type = string
13+
description = "Name of the database to create"
14+
default = null
15+
}
16+
17+
variable "database_owner" {
18+
type = string
19+
description = "Username for the database owner role"
20+
default = null
21+
}
22+
23+
variable "suga" {
24+
type = object({
25+
name = string
26+
stack_id = string
27+
env_var_key = string
28+
services = map(object({
29+
actions = list(string)
30+
identities = map(object({
31+
exports = map(string)
32+
}))
33+
}))
34+
})
35+
}

rds-instance/icon.svg

Lines changed: 17 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)