https://pulumi.com logo
Title
b

brash-hairdresser-60389

01/13/2023, 12:37 AM
Hello there, I have had an issue for months dealing with
gcp.sql.Database
and
gcp.sql.User
(using Postgres) In my pulumi script, I use to create in order: 1. The
gcp.sql.User
2. the
gcp.sql.Database
This works fine until I try to destroy the stack, I have always the issue the User cannot be deleted because there’s a reference (the database). I tried to change the order of creation Database first, then User, but this doesn’t bind the user correctly to the database. I’ve seen there’s the need in such a case to set up the
deletionPolicy: 'ABANDON'
on the Database, but in doing so, the Database is not deleted, and then if I try to recreate the database, it fails because it already exists. It is possible there is no way to manage these GCP resources correctly using Pulumi? I can’t believe I have to delete it manually. Thank you for any answer.
b

billowy-army-68599

01/13/2023, 1:57 AM
can you share your code?
b

brash-hairdresser-60389

01/13/2023, 11:20 PM
The code is very easy:
import * as gcp from '@pulumi/gcp';
import * as pulumi from '@pulumi/pulumi';
import { ManagedDatabaseInfo } from '../schema';

export class AppDatabaseManaged extends pulumi.ComponentResource {
  public readonly uri: pulumi.Output<string>;

  constructor(args: ManagedDatabaseInfo, opts?: pulumi.ResourceOptions) {
    super('sys:app:Database-managed', args.name, args, opts);

    const user = new gcp.sql.User(
      `${args.name}-user`,
      {
        instance: 'app-dev',
        name: args.user.name,
        password: args.user.password,
        project: args.project,
      },
      {
        parent: this,
        customTimeouts: { create: '45s', update: '45s', delete: '45s' },
      },
    );

    const db = new gcp.sql.Database(
      `${args.name}-pgdatabase`,
      {
        instance: 'app-dev',
        name: args.name,
        project: args.project,
      },
      {
        parent: this,
        customTimeouts: { create: '45s', update: '45s', delete: '45s' },
      },
    );

    this.uri = pulumi.output(
    pulumi.interpolate`postgresql://${user.name}:${user.password}@${args.publicIpAddress}:5432/${db.name}`,
    );
  }
}
b

billowy-army-68599

01/13/2023, 11:31 PM
there’s no dependency between the database and user, add a
dependsOn
so that the user gets created after the database: https://www.pulumi.com/docs/intro/concepts/resources/options/dependson/
b

brash-hairdresser-60389

01/13/2023, 11:33 PM
I tried using the depenbdsOn but nothing changed, consider by official doc, seems necessary to use the deletion policy abandon if there are other objects (users i.e.) belonging to the database because the GCP API cannot perform the delete. For me is unbelievable to have su a limitation
b

billowy-army-68599

01/13/2023, 11:40 PM
There’s not much we can do about the Google cloud API limitations
b

brash-hairdresser-60389

01/13/2023, 11:41 PM
So are you confirming that I cannot delete databases and I have to leave it there for manual deletion?
b

billowy-army-68599

01/13/2023, 11:42 PM
I can’t confirm that, it’s a Google cloud limitation. If the dependon didn’t help
b

brash-hairdresser-60389

01/13/2023, 11:47 PM
OK, I just want some official pulumi answer on how to properly create and delete those resources via pulumi.