export async function createDatabaseTunnel()

in shared/database/local/databaseTunnel.ts [81:117]


export async function createDatabaseTunnel(defaultSelection?: {
  stage: Stage;
}) {
  const { stage } =
    defaultSelection ||
    (await prompts({
      type: "select",
      name: "stage",
      message: "Stage?",
      choices: [
        { title: "CODE", value: "CODE", selected: true },
        { title: "PROD", value: "PROD" },
      ],
    }));

  const DBProxyName = getDatabaseProxyName(stage);

  const dbProxyResponse = await new RDS(standardAwsConfig).describeDBProxies({
    DBProxyName,
  });

  const { Endpoint } = dbProxyResponse.DBProxies![0]!;
  process.env[ENVIRONMENT_VARIABLE_KEYS.databaseHostname] = Endpoint!;
  console.log(`DB Proxy Hostname: ${Endpoint!}`);

  if (await isThereExistingTunnel(Endpoint!)) {
    console.log(
      `It looks like there is already a suitable SSH tunnel established on localhost:${DATABASE_PORT} 🎉`
    );
  } else {
    const jumpHostInstanceId = await getDatabaseJumpHost(stage);

    await establishTunnelToDBProxy(stage, jumpHostInstanceId, Endpoint!);
  }

  return stage;
}