1. Answers
  2. Creating a GCP SQL User with Pulumi

How do I create a GCP SQL user with Pulumi?

In this example, we will create a SQL user in Google Cloud SQL using Pulumi. We will define a Cloud SQL instance and then create a user for that instance. This involves specifying the instance details and the user properties such as username and password.

Key Points:

  • We will use the @pulumi/gcp package to interact with Google Cloud resources.
  • We will define a Cloud SQL instance.
  • We will create a SQL user for the defined instance.
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

// Create a Cloud SQL instance
const instance = new gcp.sql.DatabaseInstance("instance", {
    databaseVersion: "MYSQL_8_0",
    settings: {
        tier: "db-f1-micro",
    },
});

// Create a SQL user for the instance
const user = new gcp.sql.User("user", {
    instance: instance.name,
    name: "myuser",
    password: "mypassword",  // Ensure this is securely managed in a real scenario
});

export const instanceConnectionName = instance.connectionName;
export const userName = user.name;
export const userPassword = user.password;  // Do not export sensitive information in a real scenario

In this program:

  1. We import the necessary Pulumi and GCP packages.
  2. We create a Cloud SQL instance named instance with MySQL version 8.0 and a small instance tier.
  3. We create a SQL user named myuser with a password for the created instance.
  4. We export the connection name of the instance and the username for reference.

This setup will provision a Cloud SQL instance and create a user for that instance, which can be used to connect to the database. Ensure that sensitive information such as passwords is handled securely in a production environment.

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