import { GracefulError } from "@webiny/project";
import { type IProjectConfigModel } from "@webiny/project/abstractions/models/index.js";
import { getVpcConfigFromExtension } from "~/pulumi/apps/extensions/getVpcConfigFromExtension.js";
import { isVpcEnabled } from "~/pulumi/apps/extensions/isVpcEnabled.js";
const CONFIG_FILE = "webiny.config.tsx";
export interface AssertPostgresNetworkParams {
projectConfig: IProjectConfigModel;
isProduction: boolean;
/** The component that needs the network, as the messages name it: ``. */
component: string;
}
/**
* Stops `deploy core` when an Aurora cluster has no network to go into. Shared by
* `` and ``:
*
* - no VPC: the database sits in private subnets, and the functions reach it, and Secrets Manager,
* from inside the VPC. Production environments get a VPC by default, so this only asks for
* `` elsewhere. Postgres never turns the VPC on by itself, because the NAT
* gateway costs money.
* - an existing VPC without `postgresVpcConfig`: there's no subnet or security group to put the
* database in.
*/
export const assertPostgresNetwork = (params: AssertPostgresNetworkParams): void => {
const { projectConfig, isProduction, component } = params;
const vpcConfig = getVpcConfigFromExtension(projectConfig);
if (!isVpcEnabled(vpcConfig, isProduction)) {
throw GracefulError.from(
new Error("Cannot deploy Postgres without a VPC."),
[
"The database of %s runs in the VPC's private subnets, and the API functions reach it, and Secrets Manager, from inside the VPC.",
"",
"Add %s to your %s config file. The VPC adds a NAT gateway, which has its own cost.",
"Production environments use a VPC by default, unless %s turns it off."
].join("\n"),
component,
"",
CONFIG_FILE,
""
);
}
if (typeof vpcConfig === "object" && vpcConfig.useExistingVpc) {
const postgresVpcConfig = vpcConfig.useExistingVpc.postgresVpcConfig;
if (!postgresVpcConfig?.subnetIds?.length) {
throw GracefulError.from(
new Error(
"Cannot deploy Postgres into an existing VPC without `postgresVpcConfig`."
),
[
"With %s, Webiny doesn't know which subnets and security groups the database should use.",
"",
"Add %s to %s in your %s config file.",
"The security groups must accept port 5432 from the Lambda functions' security groups."
].join("\n"),
"useExistingVpc",
"postgresVpcConfig: { subnetIds, securityGroupIds }",
"",
CONFIG_FILE
);
}
}
};