This document describes how to create and stage a Cloud SQL blue-green
deployment to perform major version upgrades or configuration changes.

## Before you begin

To create and stage a blue-green deployment, verify that you have the required
roles and that your source instance meets the deployment prerequisites.

### Required roles and permissions

To get the permissions that you need to create and stage a blue-green
deployment, ask your administrator to grant you the following
IAM roles on your project:

- Cloud SQL Editor (`roles/cloudsql.editor`)
- Cloud SQL Admin (`roles/cloudsql.admin`)

For custom roles, ensure that you have the following permissions:

- `cloudsql.blueGreenDeployments.create`
- `cloudsql.blueGreenDeployments.get`
- `cloudsql.instances.get`
- `cloudsql.instances.create`
- `cloudsql.instances.update`
- `cloudsql.operations.get`

For more information about IAM roles and permissions in
Cloud SQL, see
[Roles and permissions](https://docs.cloud.google.com/sql/docs/mysql/iam-roles.md.txt).

### Instance prerequisites

Before creating a blue-green deployment, verify that your production (blue)
instance meets the following requirements:

1. **Database engine, edition, and version:** your instance must run Cloud SQL for MySQL version 5.7, 8.0, or 8.4 on Cloud SQL Enterprise edition or Cloud SQL Enterprise Plus edition. Version 8.0.18 isn't supported. For major version upgrades, you can upgrade from version 8.0 to 8.4.
2. **Binary logging and automated backups:** you must enable binary logging and automated backups on the blue instance to establish continuous logical replication to the green environment.
3. **Instance status:** the blue instance must be in a `RUNNING` state with no ongoing operations or conflicting maintenance windows.
4. **High availability (HA) configuration:** Cloud SQL supports both single-zone (`ZONAL`) and high-availability (`REGIONAL`) instances for blue-green deployments. When you create a deployment for an instance configured for high availability, Cloud SQL automatically provisions the green staging instance with high availability. However, instances that use the [legacy high availability
   configuration](https://docs.cloud.google.com/sql/docs/mysql/configure-legacy-ha) (using a legacy failover replica) aren't supported. You must [update the instance to the
   current high availability
   configuration](https://docs.cloud.google.com/sql/docs/mysql/configure-legacy-ha#update-from-legacy) before creating a blue-green deployment.
5. **Unsupported features:** verify that your instance doesn't use any
   features that aren't supported in blue-green deployments, which include
   the following:

   - [Read replicas](https://docs.cloud.google.com/sql/docs/mysql/replication.md.txt#read-replicas) (including cross-region read replicas used for disaster recovery)
   - [Legacy high availability](https://docs.cloud.google.com/sql/docs/mysql/configure-legacy-ha.md.txt)
   - [Outbound connectivity with Private Service Connect](https://docs.cloud.google.com/sql/docs/mysql/configure-private-service-connect.md.txt#enable-psc-outbound)
   - [IAM group authentication](https://docs.cloud.google.com/sql/docs/mysql/iam-authentication.md.txt#iam-group-auth)
6. **Latest maintenance version:** your instance must run the latest
   maintenance version before you create a blue-green deployment. For more
   information, see
   [Self-service maintenance](https://docs.cloud.google.com/sql/docs/mysql/self-service-maintenance.md.txt).

7. **Network architecture:** your instance must use the
   [new network architecture](https://docs.cloud.google.com/sql/docs/mysql/upgrade-cloud-sql-instance-new-network-architecture.md.txt).
   Instances that use the old network architecture aren't supported.

When you trigger deployment creation, Cloud SQL automatically runs a
series of automated prechecks to validate replication compatibility and
flags. For major version upgrades, Cloud SQL also runs the major
version upgrade pre-check API on the source instance to validate upgrade
readiness before proceeding with the workflow. If prechecks fail, deployment
creation stops and returns an error in the operation status.

## Create a blue-green deployment

You can create a blue-green deployment to perform a major version upgrade (by
specifying a target database version), or to stage hardware or configuration
changes at your current database version.

### Console

1. In the Google Cloud console, go to the **Cloud SQL Instances** page.

   [Go to Cloud SQL Instances](https://console.cloud.google.com/sql)
2. To open the **Overview** page of an instance, click the instance name.
3. In the **Configuration** section, click **Create blue green
   deployment**.
4. On the **Create Blue Green Deployment** page, in the **Deployment
   information** section, enter a name for the deployment in the **Deployment name** field. The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
5. In the **Deployment use case** list, select one of the following options:
   - **Option A (Major version upgrade):** to stage and test a major version upgrade on the green instance before switchover, select **Major version upgrade** . In the **Target database version** list, select the target database version for the green target instance (**MySQL 8.4** ). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4.

     When you create a deployment for a major version upgrade,
     Cloud SQL automatically runs the major version upgrade
     precheck as part of the operation before creating the green instance.
     Alternatively, you can run the
     [major version
     upgrade precheck](https://docs.cloud.google.com/sql/docs/mysql/upgrade-major-db-version-inplace#precheck) before creating the deployment to identify any
     upgrade blockers.
   - **Option B (Default):** to create a green staging environment at the same database version as the source instance to test configuration or hardware changes, select **Default**.
6. Click **Create**.

### gcloud

To create a blue-green deployment by using the [gcloud CLI](https://docs.cloud.google.com/sdk/gcloud.md.txt), run the
[`gcloud
sql blue-green-deployments create`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/blue-green-deployments/create) command.

**Option A: Major version upgrade**

To create a deployment for a major version upgrade, specify the
`--target-database-version` flag. When you specify a target
database version, Cloud SQL automatically runs the major version
upgrade precheck as part of the operation before creating the green instance.
Alternatively, you can run the
[major version upgrade
precheck](https://docs.cloud.google.com/sql/docs/mysql/upgrade-major-db-version-inplace#precheck) on your blue instance before creating the deployment to
identify any upgrade blockers:

```
gcloud sql blue-green-deployments create DEPLOYMENT_NAME \
  --source-instance=SOURCE_INSTANCE_ID \
  --target-database-version=TARGET_DATABASE_VERSION \
  --region=REGION \
  --async
```

Replace the following:

- <var translate="no">DEPLOYMENT_NAME</var>: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
- <var translate="no">SOURCE_INSTANCE_ID</var>: the name of your blue source instance.
- <var translate="no">TARGET_DATABASE_VERSION</var>: the target database version for the green target instance (`MYSQL_8_4`). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.
- <var translate="no">REGION</var>: the Google Cloud region of your blue instance.

**Option B: Hardware or configuration changes**

To provision a green environment at the same database version as the blue
instance to test hardware or configuration changes, omit the
`--target-database-version` flag:

```
gcloud sql blue-green-deployments create DEPLOYMENT_NAME \
  --source-instance=SOURCE_INSTANCE_ID \
  --region=REGION \
  --async
```

Creating a blue-green deployment takes several minutes to complete,
especially when performing a major version upgrade. You might see a message
indicating that the operation is taking longer than expected. You can either
ignore this message or run the
[`gcloud sql
operations wait`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/operations/wait) command to dismiss the message and wait for the
operation to complete:

```
gcloud sql operations wait OPERATION_ID
```

Replace <var translate="no">OPERATION_ID</var> with the operation ID returned by the
command or displayed in the message.

### Terraform

To create a blue-green deployment using Terraform, add the
`blue_green_deployment` code block to your primary
[`google_sql_database_instance`
resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance) with the `active_environment` field set to
`"SOURCE"`.

> [!CAUTION]
> **Caution:** Because Terraform authoritatively manages the `blue_green_deployment` code block on the primary `google_sql_database_instance` resource, if you create a blue-green deployment outside of Terraform (for example, by using the Google Cloud console or the [gcloud CLI](https://docs.cloud.google.com/sdk/gcloud.md.txt)) without adding the `blue_green_deployment` code block to your Terraform configuration, running the `terraform apply` command deletes the deployment and its green target instance.

**Option A: Major version upgrade**

To create a deployment for a major version upgrade, keep the top-level
`database_version` field set to your current blue instance
version, and set the `target_database_version` field in the
`blue_green_deployment` code block to the target major version:

```terraform
resource "google_sql_database_instance" "primary" {
  name             = "SOURCE_INSTANCE_ID"
  region           = "REGION"
  database_version = "SOURCE_DATABASE_VERSION"

  settings {
    tier              = "MACHINE_TIER"
    edition           = "EDITION"
    availability_type = "REGIONAL"

    backup_configuration {
      enabled            = true
      binary_log_enabled = true
    }
  }

  blue_green_deployment {
    deployment_name         = "DEPLOYMENT_NAME"
    target_database_version = "TARGET_DATABASE_VERSION"
    active_environment      = "SOURCE"
  }
}
```

Replace the following:

- <var translate="no">SOURCE_INSTANCE_ID</var>: the name of your blue source instance.
- <var translate="no">REGION</var>: the Google Cloud region of your blue instance.
- <var translate="no">SOURCE_DATABASE_VERSION</var>: the current database version of your blue instance (for example, `MYSQL_8_0`). For major version upgrades, the blue instance must run MySQL 8.0 (excluding 8.0.18).
- <var translate="no">MACHINE_TIER</var>: the machine type of your instance (for example, `db-custom-2-7680`).
- <var translate="no">EDITION</var>: the Cloud SQL edition of your instance (`ENTERPRISE` or `ENTERPRISE_PLUS`).
- <var translate="no">DEPLOYMENT_NAME</var>: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
- <var translate="no">TARGET_DATABASE_VERSION</var>: the target database version for the green target instance (`MYSQL_8_4`). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.

**Option B: Hardware or configuration changes**

To provision a green environment at the same database version as the blue
instance to test hardware or configuration changes, omit the
`target_database_version` field in the
`blue_green_deployment` code block:

```terraform
resource "google_sql_database_instance" "primary" {
  name             = "SOURCE_INSTANCE_ID"
  region           = "REGION"
  database_version = "SOURCE_DATABASE_VERSION"

  settings {
    tier              = "MACHINE_TIER"
    edition           = "EDITION"
    availability_type = "REGIONAL"

    backup_configuration {
      enabled            = true
      binary_log_enabled = true
    }
  }

  blue_green_deployment {
    deployment_name    = "DEPLOYMENT_NAME"
    active_environment = "SOURCE"
  }
}
```

To apply your Terraform configuration in a Google Cloud project, complete the steps in the
following sections.

## Prepare Cloud Shell

1. Launch [Cloud Shell](https://shell.cloud.google.com/).
2. Set the default Google Cloud project
   where you want to apply your Terraform configurations.

   You only need to run this command once per project, and you can run it in any directory.

   ```
   export GOOGLE_CLOUD_PROJECT=PROJECT_ID
   ```

   Environment variables are overridden if you set explicit values in the Terraform
   configuration file.

## Prepare the directory

Each Terraform configuration file must have its own directory (also
called a *root module*).

1. In [Cloud Shell](https://shell.cloud.google.com/), create a directory and a new file within that directory. The filename must have the `.tf` extension---for example `main.tf`. In this tutorial, the file is referred to as `main.tf`.

   ```
   mkdir DIRECTORY && cd DIRECTORY && touch main.tf
   ```
2. If you are following a tutorial, you can copy the sample code in each section or step.

   Copy the sample code into the newly created `main.tf`.

   Optionally, copy the code from GitHub. This is recommended
   when the Terraform snippet is part of an end-to-end solution.
3. Review and modify the sample parameters to apply to your environment.
4. Save your changes.
5. Initialize Terraform. You only need to do this once per directory.

   ```
   terraform init
   ```

   Optionally, to use the latest Google provider version, include the `-upgrade`
   option:

   ```
   terraform init -upgrade
   ```

## Apply the changes

1. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:

   ```
   terraform plan
   ```

   Make corrections to the configuration as necessary.
2. Apply the Terraform configuration by running the following command and entering `yes` at the prompt:

   ```
   terraform apply
   ```

   Wait until Terraform displays the "Apply complete!" message.
3. [Open your Google Cloud project](https://console.cloud.google.com/) to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.

> [!NOTE]
> **Note:** Terraform samples typically assume that the required APIs are enabled in your Google Cloud project.

### REST v1

To create a blue-green deployment using the Cloud SQL Admin API, send a POST
request to the `blueGreenDeployments.create` method.

**Option A: Major version upgrade**

To create a deployment for a major version upgrade, specify the
`databaseVersion` field in the `requestedConfig` object.
When you specify a target database version, Cloud SQL automatically
runs the major version upgrade precheck as part of the operation before creating
the green instance. Alternatively, you can run the
[major version upgrade
precheck](https://docs.cloud.google.com/sql/docs/mysql/upgrade-major-db-version-inplace#precheck) on your blue instance before creating the deployment to
identify any upgrade blockers:

```
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID",
  "requestedConfig": {
    "databaseVersion": "TARGET_DATABASE_VERSION"
  }
}
```

Replace the following:

- <var translate="no">PROJECT_ID</var>: the ID of your Google Cloud project.
- <var translate="no">REGION</var>: the Google Cloud region of your blue instance.
- <var translate="no">DEPLOYMENT_NAME</var>: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
- <var translate="no">SOURCE_INSTANCE_ID</var>: the name of your blue source instance.
- <var translate="no">TARGET_DATABASE_VERSION</var>: the target database version for the green target instance (`MYSQL_8_4`). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.

**Option B: Hardware or configuration changes**

To provision a green environment at the same database version as the blue
instance to test hardware or configuration changes, omit the
`requestedConfig` field:

```
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID"
}
```

### REST v1beta4

To create a blue-green deployment using the Cloud SQL Admin API, send a POST
request to the `blueGreenDeployments.create` method.

**Option A: Major version upgrade**

To create a deployment for a major version upgrade, specify the
`databaseVersion` field in the `requestedConfig` object.
When you specify a target database version, Cloud SQL automatically
runs the major version upgrade precheck as part of the operation before creating
the green instance. Alternatively, you can run the
[major version upgrade
precheck](https://docs.cloud.google.com/sql/docs/mysql/upgrade-major-db-version-inplace#precheck) on your blue instance before creating the deployment to
identify any upgrade blockers:

```
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID",
  "requestedConfig": {
    "databaseVersion": "TARGET_DATABASE_VERSION"
  }
}
```

Replace the following:

- <var translate="no">PROJECT_ID</var>: the ID of your Google Cloud project.
- <var translate="no">REGION</var>: the Google Cloud region of your blue instance.
- <var translate="no">DEPLOYMENT_NAME</var>: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
- <var translate="no">SOURCE_INSTANCE_ID</var>: the name of your blue source instance.
- <var translate="no">TARGET_DATABASE_VERSION</var>: the target database version for the green target instance (`MYSQL_8_4`). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.

**Option B: Hardware or configuration changes**

To provision a green environment at the same database version as the blue
instance to test hardware or configuration changes, omit the
`requestedConfig` field:

```
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID"
}
```

## Monitor deployment status

Creation is a long-running operation (LRO). During staging, Cloud SQL
provisions the green instance, upgrades it (if a major version upgrade was
requested), and starts continuous logical replication.

Check the progress and status of your blue-green deployment:

### Console

1. In the Google Cloud console, go to the **Cloud SQL Instances** page.

   [Go to Cloud SQL Instances](https://console.cloud.google.com/sql)
2. To open the **Overview** page of an instance, click the instance name.
3. Locate the **Blue Green Deployment Status** card to view the status of the deployment.
4. To view detailed provisioning tasks and progress, click **Details** to open the **Deployment overview** page.

### gcloud

To check the progress and status of your blue-green deployment by using the
[gcloud CLI](https://docs.cloud.google.com/sdk/gcloud.md.txt), run the
[`gcloud
sql blue-green-deployments describe`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/blue-green-deployments/describe) command:

```
gcloud sql blue-green-deployments describe DEPLOYMENT_NAME \
  --region=REGION
```

Replace the following:

- <var translate="no">DEPLOYMENT_NAME</var>: the name of your blue-green deployment.
- <var translate="no">REGION</var>: the Google Cloud region where the deployment was created.

To wait for the creation operation to complete, run the
[`gcloud sql
operations wait`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/operations/wait) command:

```
gcloud sql operations wait OPERATION_ID
```

Replace <var translate="no">OPERATION_ID</var> with the ID of the creation operation.

### REST v1

To check the progress and status of your blue-green deployment using the
Cloud SQL Admin API, send a GET request to the
`blueGreenDeployments.get` method:

```
GET https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments/DEPLOYMENT_NAME
```

Replace the following:

- <var translate="no">PROJECT_ID</var>: the ID of your Google Cloud project.
- <var translate="no">REGION</var>: the Google Cloud region where the deployment was created.
- <var translate="no">DEPLOYMENT_NAME</var>: the name of your blue-green deployment.

### REST v1beta4

To check the progress and status of your blue-green deployment using the
Cloud SQL Admin API, send a GET request to the
`blueGreenDeployments.get` method:

```
GET https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments/DEPLOYMENT_NAME
```

Replace the following:

- <var translate="no">PROJECT_ID</var>: the ID of your Google Cloud project.
- <var translate="no">REGION</var>: the Google Cloud region where the deployment was created.
- <var translate="no">DEPLOYMENT_NAME</var>: the name of your blue-green deployment.

In the command output or the Google Cloud console, monitor the `state` field to
track the deployment lifecycle:

- **`PROVISIONING`:** the green instance is being created, upgraded (if requested), and connected to continuous logical replication.
- **`SWITCHOVER_READY`:** initial replication has completed and continuous logical replication is active. The deployment is ready for validation, testing, and switchover.
- **`SWITCHOVER_NOT_READY`:** the deployment is provisioned, but switchover can't be initiated (for example, if replication is broken or an issue is reported in the `errorDetail` field).

For more information about all deployment lifecycle phases, see
[Deployment lifecycle and
states](https://docs.cloud.google.com/sql/docs/mysql/about-blue-green-deployments#lifecycle-states).

Wait until the deployment state changes to `SWITCHOVER_READY` before you
proceed to describe the deployment, validate your application workloads, or
start a switchover.

## What's next

- [Describe and list blue-green deployments](https://docs.cloud.google.com/sql/docs/mysql/describe-blue-green-deployment.md.txt) to inspect deployment status and retrieve connection details.
- [Switchover a blue-green deployment](https://docs.cloud.google.com/sql/docs/mysql/switchover-blue-green-deployment.md.txt) to convert the green instance to the active production read and write instance.
- [Delete a blue-green deployment](https://docs.cloud.google.com/sql/docs/mysql/delete-blue-green-deployment.md.txt) to cancel staging and remove the green environment.