1. Answers
  2. How To Connect Cloud Run To Cloud SQL With Private IP?

How to Connect Cloud Run to Cloud SQL With Private IP?

To connect Cloud Run to Cloud SQL with a private IP using Pulumi in TypeScript, we will follow these steps:

  1. Introduction: Provide an overview of the solution and the key services involved.
  2. Step-by-Step Explanation:
    • Step 1: Set up a VPC and subnets.
    • Step 2: Create a Cloud SQL instance with a private IP.
    • Step 3: Deploy a Cloud Run service and connect it to the Cloud SQL instance.
  3. Key Points: Highlight important considerations and configurations.
  4. Conclusion: Summarize the solution and its benefits.

Search terms:

  • “Google Cloud VPC Pulumi”
  • “Google Cloud SQL Pulumi”
  • “Google Cloud Run Pulumi”
  • “Google Cloud SQL private IP”
  • “Google Cloud Run connect to Cloud SQL”

Full Code Example

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

// Create a VPC network
const vpcNetwork = new gcp.compute.Network("vpcNetwork", {
    autoCreateSubnetworks: false,
});

// Create a subnetwork
const subnetwork = new gcp.compute.Subnetwork("subnetwork", {
    network: vpcNetwork.id,
    ipCidrRange: "10.0.0.0/16",
    region: "us-central1",
});

// Create a Cloud SQL instance with private IP
const sqlInstance = new gcp.sql.DatabaseInstance("sqlInstance", {
    region: "us-central1",
    databaseVersion: "POSTGRES_13",
    settings: {
        tier: "db-f1-micro",
        ipConfiguration: {
            ipv4Enabled: false,
            privateNetwork: vpcNetwork.id,
        },
    },
});

// Create a Cloud Run service
const cloudRunService = new gcp.cloudrunv2.Service("cloudRunService", {
    location: "us-central1",
    template: {
        containers: [{
            image: "gcr.io/cloudrun/hello",
            envs: [{
                name: "DB_CONNECTION_NAME",
                value: pulumi.interpolate`${sqlInstance.connectionName}`,
            }],
        }],
        vpcAccess: {
            connector: subnetwork.id,
        },
    },
});

export const vpcNetworkUrl = vpcNetwork.selfLink;
export const subnetworkUrl = subnetwork.selfLink;
export const sqlInstanceUrl = sqlInstance.selfLink;
export const cloudRunServiceUrl = cloudRunService.uri;

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up