Search code examples
google-cloud-platformgoogle-cloud-sqlgoogle-deployment-manager

Google Deployment Manager - database creation fails


I'm trying to create a CloudSQL instance and two databases using Google Deployment Manager. I can't get a reliable first-time deployment where both databases create successfully. Instead, each time I run it, one (or both!) fail with the status "FAILED_PRECONDITION", error message "Bad Request" and no further explanation as to which precondition has failed or how to fix it. Has anyone else come across this before or have any clues on how I can find the issue?

The properties {{ SQL_NAME }} etc. are all defined at the top of my jinja template, but I've ommitted them for clarity.

resources:
- name: {{ SQL_NAME }}
  type: sqladmin.v1beta4.instance
  properties:
    backendType: SECOND_GEN
    instanceType: CLOUD_SQL_INSTANCE
    region: {{ properties["region"] }}
    databaseVersion: {{ properties["dbType"] }}
    settings:
      tier: db-n1-standard-1
      dataDiskSizeGb: 10
      dataDiskType: PD_SSD
      storageAutoResize: true
      replicationType: SYNCHRONOUS
      locationPreference:
        zone: {{ properties['zone']}}
      ipConfiguration:
        privateNetwork: {{ properties["network"] }}

- name: {{ DB_NAME }}
  type: sqladmin.v1beta4.database
  properties:
    name: db1
    instance: $(ref.{{ SQL_NAME }}.name)
    charset: utf8
    collation: utf8_general_ci
  metadata:
    dependsOn:
    - {{ SQL_NAME }}

- name: {{ DB2_NAME }}
  type: sqladmin.v1beta4.database
  properties:
    name: db2
    instance: $(ref.{{ SQL_NAME }}.name)
    charset: utf8
  metadata:
    dependsOn:
    - {{ SQL_NAME }}

- name: {{ USER_NAME }}
  type: sqladmin.v1beta4.user
  properties:
    name: dbroot
    host:  "%"
    instance: $(ref.{{ SQL_NAME }}.name)
    password: {{ properties['password'] }}
  metadata:
    dependsOn:
    - {{ SQL_NAME }}

Solution

  • So, I found the answer. It turns out Google is even less helpful with their error message than I thought when I came across the issue. What it seems to be (I still have no concrete evidence that this was the precondition, but the below seems to solve it) is that you can't create two databases at the same time on the same CloudSQL instance, and Deployment Manager tries to because they're both only dependant on the CloudSQL instance itself. I solved the issue by adding dependencies on each successive resource:

    - name: {{ DB_NAME }}
      type: sqladmin.v1beta4.database
      properties:
        name: db1
        instance: $(ref.{{ SQL_NAME }}.name)
        charset: utf8
        collation: utf8_general_ci
      metadata:
        dependsOn:
        - {{ SQL_NAME }}
    
    - name: {{ DB2_NAME }}
      type: sqladmin.v1beta4.database
      properties:
        name: db2
        instance: $(ref.{{ SQL_NAME }}.name)
        charset: utf8
      metadata:
        dependsOn:
        - {{ SQL_NAME }}
        - {{ DB_NAME }}
    
    - name: {{ USER_NAME }}
      type: sqladmin.v1beta4.user
      properties:
        name: dbroot
        host:  "%"
        instance: $(ref.{{ SQL_NAME }}.name)
        password: {{ properties['password'] }}
      metadata:
        dependsOn:
        - {{ SQL_NAME }}
        - {{ DB2_NAME }}