Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prisma CLI in Redwood ECS console #91

Merged
merged 3 commits into from
Jan 27, 2024
Merged
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
8 changes: 0 additions & 8 deletions api/src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ async function ssmAuthForURL(databaseURL: string): Promise<string> {
return url.toString()
}

function pgpasswordAuthForURL(databaseURL: string): string {
const url = new URL(databaseURL)
url.password = process.env.PGPASSWORD
return url.toString()
}

/*
* Instance of the Prisma Client
*/
Expand All @@ -51,8 +45,6 @@ async function createPrismaClient() {
datasourceUrl = await rdsIAMAuthForURL(process.env.DATABASE_URL)
} else if (process.env.DATABASE_SECRET_SOURCE === 'ssm') {
datasourceUrl = await ssmAuthForURL(process.env.DATABASE_URL)
} else if (process.env.DATABASE_SECRET_SOURCE === 'env_PGPASSWORD') {
datasourceUrl = pgpasswordAuthForURL(process.env.DATABASE_URL)
}

db = new PrismaClient({
Expand Down
43 changes: 25 additions & 18 deletions terraform/ecs_redwood_console.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,14 @@ module "ecs_console_container_definition" {
}

map_environment = {
AWS_REGION = data.aws_region.current.name
AWS_DEFAULT_REGION = data.aws_region.current.name
DATABASE_SECRET_SOURCE = "env_PGPASSWORD"
DATABASE_URL = format(
"postgres://%s@%s:%s/%s?%s",
module.postgres.cluster_master_username,
module.postgres.cluster_endpoint,
module.postgres.cluster_port,
module.postgres.cluster_database_name,
join("&", [
"sslmode=verify",
"sslcert=/home/node/app/api/db/rds-combined-ca-bundle.pem"
])
)
CI = ""
NODE_ENV = "development"
AWS_REGION = data.aws_region.current.name
AWS_DEFAULT_REGION = data.aws_region.current.name
CI = ""
NODE_ENV = "development"
}

map_secrets = {
PGPASSWORD = aws_ssm_parameter.postgres_master_password.arn
DATABASE_URL = aws_ssm_parameter.ecs_console_secret_database_url.arn
}

log_configuration = {
Expand All @@ -53,6 +41,25 @@ module "ecs_console_container_definition" {
}
}

resource "aws_ssm_parameter" "ecs_console_secret_database_url" {
name = "${var.ssm_service_parameters_path_prefix}/postgres/database_url"
description = "Prisma database URL for connecting the Postgres cluster"
type = "SecureString"
key_id = data.aws_kms_key.ssm.arn
value = format(
"postgres://%s:%s@%s:%s/%s?%s",
module.postgres.cluster_master_username,
module.postgres.cluster_master_password,
module.postgres.cluster_endpoint,
module.postgres.cluster_port,
module.postgres.cluster_database_name,
join("&", [
"sslmode=verify-full",
"sslrootcert=${urlencode("/home/node/app/api/db/rds-combined-ca-bundle.pem")}",
])
)
}

resource "aws_iam_role" "ecs_console_execution" {
name_prefix = "${var.namespace}-console-ECSTaskExecution-"
permissions_boundary = local.permissions_boundary_arn
Expand Down Expand Up @@ -80,7 +87,7 @@ data "aws_iam_policy_document" "ecs_console_execution" {
]
resources = [
data.aws_kms_key.ssm.arn,
aws_ssm_parameter.postgres_master_password.arn,
aws_ssm_parameter.ecs_console_secret_database_url.arn,
]
}
statement {
Expand Down
Loading