hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.88k stars 455 forks source link

google: Cannot create a SQLDb instance because of a badRequest - not enough logs #3720

Open eduardosanzb opened 2 months ago

eduardosanzb commented 2 months ago

Expected Behavior

SQL DB should be created. I'm using the same configuration as I"d use with HCL.

I redacted the company name from the logs

Actual Behavior

THe debug message from why is the request badly done is not enough to continue. I'm not able to create a SQLdb in GCP

Steps to Reproduce

With the next code, just run cdtfk deploy. The plan went fine.

import { App, TerraformStack } from 'cdktf'
import { Construct } from 'constructs'
import { GoogleProvider } from '@cdktf/provider-google/lib/provider'
import * as path from 'path'
import * as fs from 'fs'
import { SqlDatabaseInstance } from '@cdktf/provider-google/lib/sql-database-instance'
import { DataGoogleProject } from '@cdktf/provider-google/lib/data-google-project'
import { SqlDatabase } from '@cdktf/provider-google/lib/sql-database'
import { password } from './.gen/providers/random'
import { SqlUser } from '@cdktf/provider-google/lib/sql-user'
import { RandomProvider } from './.gen/providers/random/provider'

class MyStack extends TerraformStack {
  constructor(scope: Construct, name: string) {
    super(scope, name)

    const credentialsPath = path.join(process.cwd(), 'google.json')
    const credentials = fs.existsSync(credentialsPath)
      ? fs.readFileSync(credentialsPath).toString()
      : '{}'

    console.log('credentialsPath', credentialsPath)
    console.log('credentials', credentials)
    const region = 'europe-west4'
    const projectId = 'company-development'
    new GoogleProvider(this, 'Google', {
      region,
      project: projectId,
      // credentials, //using the ones from ADC
    })

    new RandomProvider(this, 'random', {})

    const googleProject = new DataGoogleProject(this, 'project', {
      projectId,
    })

    const dbInstance = new SqlDatabaseInstance(this, 'db-instance', {
      project: googleProject.id,
      region,
      name: 'db-instance',
      databaseVersion: 'POSTGRES_13',

      settings: {
        tier: 'db-f1-micro',
        diskSize: 10,
        diskType: 'PD_SSD',
        availabilityType: 'REGIONAL',

        databaseFlags: [
          {
            name: 'autovacuum',
            value: 'on',
          },
          {
            name: 'max_connections',
            value: '100',
          },
          {
            name: 'max_wal_size',
            value: '1GB',
          },
        ],

        deletionProtectionEnabled: false,

        ipConfiguration: {
          ipv4Enabled: true,
          // requireSsl: true,
        },
        maintenanceWindow: {
          day: 1,
          hour: 4,
          updateTrack: 'stable',
        },
        backupConfiguration: {
          enabled: true,
          binaryLogEnabled: true,
          pointInTimeRecoveryEnabled: true,
          startTime: '05:00',
          location: 'EU',
        },
        insightsConfig: {
          queryInsightsEnabled: true,
          queryStringLength: 4500,
          recordApplicationTags: true,
        },
      },

      lifecycle: {
        ignoreChanges: [],
      },
    })

    new SqlDatabase(this, 'name-db', {
      project: googleProject.id,
      instance: dbInstance.name,
      name: 'name-db',
    })

    new SqlUser(this, 'name-db-user', {
      project: googleProject.id,
      instance: dbInstance.name,
      name: 'name-db-user',
      password: new password.Password(this, 'name-db-user-password', {
        length: 16,
      }).result,
      host: '%',
    })

    // lets do the service api
    // 1. service account
    // 2. iam to cloudsql
  }
}

const app = new App()
new MyStack(app, 'name-app-dev')
app.synth()

Versions

language: typescript cdktf-cli: 0.20.8 node: v20.16.0 cdktf: 0.20.8 constructs: 10.3.0 jsii: null terraform: 1.9.5 arch: arm64 os: darwin 23.5.0 providers random@undefined (LOCAL) terraform provider version: 3.6.2 @cdktf/provider-google (PREBUILT) terraform provider version: 5.43.1 prebuilt provider version: 13.32.1 cdktf version: ^0.20.0 @cdktf/provider-kubernetes (PREBUILT) terraform provider version: 2.32.0 prebuilt provider version: 11.7.0 cdktf version: ^0.20.0 cdktf debug 14.17s user 1.98s system 178% cpu 9.070 total

Providers

──────────────────────────┬──────┬─────────┬─────────────────────┬─────────────┐
──          ───            ──     ──        ──────                ───
 Provider    Provider     │ CDKTF│         │ Package Name        │ Package     │
Name        Version               Constraint                      Version

──────────────────────────┼──────┼─────────┼─────────────────────┼─────────────┤
──          ───            ──     ──        ──────                ───
 random      3.6.2        │      │         │                     │             │

──────────────────────────┼──────┼─────────┼─────────────────────┼─────────────┤
──          ───            ──     ──        ──────                ───
 google      5.43.1       │      │         │                     │ 13.32.1     │
                           ^0.20.0          @cdktf/provider-google

──────────────────────────┼──────┼─────────┼─────────────────────┼─────────────┤
──          ───            ──     ──        ──────                ───
 kubernetes  2.32.0       │      │         │ @cdktf/provider-kube│ 11.7.0      │
                           ^0.20.0          netes

──────────────────────────┴──────┴─────────┴─────────────────────┴─────────────┘
──          ───            ──     ──        ──────                ───

Gist

https://gist.github.com/eduardosanzb/a85bba69ef2eb4067ac079a07d8b45db

Possible Solutions

No response

Workarounds

No response

Anything Else?

No response

References

No response

Help Wanted

Community Note