$npx -y skills add VeerMuchandi/rad-skills --skill cloudsql_setupProvisions a PostgreSQL Cloud SQL instance, database, database user, and registers connection details in Secret Manager using Terraform.
| 1 | # Cloud SQL Setup Skill |
| 2 | |
| 3 | This skill allows you to easily provision a PostgreSQL database on Google Cloud Platform using Terraform. It automatically enables required APIs, configures a secure database instance, creates a database and a user with a secure auto-generated password, and registers all connection parameters in Secret Manager for use by ADK agents. |
| 4 | |
| 5 | ## Prerequisites |
| 6 | - **Terraform Version**: `v1.5+` (Recommended `v1.14+`) |
| 7 | - **GCP Project**: Active Google Cloud Project with billing enabled. |
| 8 | - **Permissions**: |
| 9 | - `roles/sqladmin.admin` (to manage Cloud SQL) |
| 10 | - `roles/secretmanager.admin` (to manage secrets) |
| 11 | - `roles/serviceusage.serviceUsageConsumer` (to enable services) |
| 12 | |
| 13 | ## Skill Directory Structure |
| 14 | ```text |
| 15 | cloudsql_setup/ |
| 16 | ├── SKILL.md # This instructions file |
| 17 | ├── resources/ |
| 18 | │ └── templates/ |
| 19 | │ ├── main.tf # Core Terraform resources |
| 20 | │ ├── variables.tf # Customizable variables |
| 21 | │ └── outputs.tf # Secret IDs and Connection Name outputs |
| 22 | └── scripts/ |
| 23 | └── deploy_db.py # Helper Python script to orchestrate deployment |
| 24 | ``` |
| 25 | |
| 26 | ## 1. How to Use this Skill (Step-by-Step) |
| 27 | |
| 28 | ### Step 1: Initialize the Deployment Workspace |
| 29 | Create a dedicated deployment directory outside your agent code package to prevent Terraform state/binaries from being packaged with the agent. |
| 30 | |
| 31 | ```bash |
| 32 | mkdir -p deploy_db |
| 33 | ``` |
| 34 | |
| 35 | ### Step 2: Copy the Templates |
| 36 | Copy the templates from this skill to your new `deploy_db` folder. |
| 37 | *(Note: Replace `<path_to_skill>` with the path of this skill folder, e.g. `skills/cloudsql_setup` or `~/.gemini/config/skills/cloudsql_setup`)* |
| 38 | |
| 39 | ```bash |
| 40 | cp -r <path_to_skill>/resources/templates/* deploy_db/ |
| 41 | ``` |
| 42 | |
| 43 | ### Step 3: Configure Deployment (`deploy_db/terraform.tfvars`) |
| 44 | Create a file named `deploy_db/terraform.tfvars` and set the following parameters: |
| 45 | |
| 46 | ```hcl |
| 47 | project_id = "your-gcp-project-id" |
| 48 | region = "us-central1" |
| 49 | instance_name_prefix = "my-adk-db" |
| 50 | database_flavor = "postgres" # or "mysql" |
| 51 | database_version = "POSTGRES_15" # or "MYSQL_8_0" |
| 52 | database_name = "my_agent_db" |
| 53 | db_username = "db_user" |
| 54 | secret_prefix = "my_agent_" # secrets will be created with prefix: my_agent_ |
| 55 | deletion_protection = false # Set to true for production database |
| 56 | ``` |
| 57 | |
| 58 | ### Step 4: Run Terraform |
| 59 | Navigate to the `deploy_db` directory, initialize, and deploy the resources: |
| 60 | |
| 61 | ```bash |
| 62 | cd deploy_db |
| 63 | terraform init |
| 64 | terraform apply -auto-approve |
| 65 | ``` |
| 66 | |
| 67 | ### Step 5: Capture Output |
| 68 | Upon successful completion, Terraform outputs the connection details and Secret Manager secret IDs: |
| 69 | ```hcl |
| 70 | connection_name = "your-project:us-central1:my-adk-db-xxxx" |
| 71 | secret_ids = { |
| 72 | db_connection_name = "my_agent_db_connection_name" |
| 73 | db_name = "my_agent_db_name" |
| 74 | db_password = "my_agent_db_password" |
| 75 | db_user = "my_agent_db_user" |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ### Option B: Using the Orchestration Helper Script (Recommended) |
| 80 | You can automate the deployment, secrets creation, and database schema initialization in a single step using the helper Python script. |
| 81 | |
| 82 | 1. **Create Schema File** (e.g. `schema.sql`): |
| 83 | ```sql |
| 84 | CREATE TABLE IF NOT EXISTS items ( |
| 85 | id SERIAL PRIMARY KEY, |
| 86 | name VARCHAR(100) NOT NULL, |
| 87 | description TEXT |
| 88 | ); |
| 89 | ``` |
| 90 | |
| 91 | 2. **Run the Script**: |
| 92 | ```bash |
| 93 | python3 <path_to_skill>/scripts/deploy_db.py \ |
| 94 | --project "your-gcp-project-id" \ |
| 95 | --sql-file "schema.sql" |
| 96 | ``` |
| 97 | |
| 98 | This script will run the Terraform deployment and then connect to the Cloud SQL instance over secure IAM-auth using the Python connector to execute the statements in your SQL file. |
| 99 | |
| 100 | --- |
| 101 | |
| 102 | ## 2. Connecting ADK Agent to Cloud SQL |
| 103 | To use the newly created database in your ADK agent: |
| 104 | |
| 105 | 1. **Add Dependencies**: Include these in your agent's `requirements.txt`: |
| 106 | ```text |
| 107 | cloud-sql-python-connector[pg8000] |
| 108 | SQLAlchemy |
| 109 | google-cloud-secret-manager |
| 110 | ``` |
| 111 | 2. **Access Secrets in Agent**: Refer to the `secret_ids` generated. Set environment variables on the deployed agent containing the secret IDs, and use the Cloud SQL Python Connector library to access the database. |
| 112 | |
| 113 | --- |
| 114 | |
| 115 | ## 3. Troubleshooting & Learnings |
| 116 | |
| 117 | ### 3.1. PostgreSQL User Deletion (`cannot be dropped because some objects depend on it`) |
| 118 | - **The Issue**: When running `--destroy` (or `terraform destroy`), if you populated the database (created tables, sequences, etc.) using that user, PostgreSQL will block dropping the user because of these dependent objects. |
| 119 | - **The Workaround**: Simply re-run the destroy command. Since the database itself was already deleted during the first run, the user's dependencies are resolved and the second run will complete cleanly. |