Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Cannot create Global Aurora (MySQL) cluster based upon snapshot #29187

Closed
rosstimson opened this issue Jan 31, 2023 · 6 comments · Fixed by #30158
Closed

[Bug]: Cannot create Global Aurora (MySQL) cluster based upon snapshot #29187

rosstimson opened this issue Jan 31, 2023 · 6 comments · Fixed by #30158
Labels
bug Addresses a defect in current functionality. service/rds Issues and PRs that pertain to the rds service.
Milestone

Comments

@rosstimson
Copy link

rosstimson commented Jan 31, 2023

Terraform Core Version

1.3.7

AWS Provider Version

4.52.0

Affected Resource(s)

  • aws_db_cluster_snapshot (data)
  • aws_rds_global_cluster
  • aws_rds_cluster
  • aws_rds_cluster_instance

Expected Behavior

A MySQL Global Aurora cluster should be created with a primary cluster and writer instance based upon a snapshot, it should look something like this:

Screenshot 2023-01-31 at 16-37-35 RDS Management Console

Note that this correct looking cluster was created using the exact same code just with the snapshot_identifier = data.aws_db_cluster_snapshot.sre_5136_to_be_upgraded.id part commented out.

Actual Behavior

A MySQL Global Aurora cluster is created but the primary cluster and instance are not added to it, they are created separately as a standalone regional cluster.

Screenshot 2023-01-31 at 14-07-43 RDS Management Console

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

# Create Initial Cluster to be Copied Next
# -----------------------------------------------------------------------------

resource "aws_rds_global_cluster" "sre_5136" {
  global_cluster_identifier = "sre-5136-global-test"
  engine                    = "aurora"
  engine_version            = "5.6.mysql_aurora.1.22.4"
  storage_encrypted         = true
}

resource "aws_rds_cluster" "sre_5136_primary" {
  engine                    = aws_rds_global_cluster.sre_5136.engine
  engine_version            = aws_rds_global_cluster.sre_5136.engine_version
  cluster_identifier        = "sre-5136-test-primary-cluster"
  master_username           = "example_user"
  master_password           = "password"
  database_name             = "example_db"
  global_cluster_identifier = aws_rds_global_cluster.sre_5136.id
  db_subnet_group_name      = "default"
}

resource "aws_rds_cluster_instance" "sre_5136_primary" {
  engine               = aws_rds_global_cluster.sre_5136.engine
  engine_version       = aws_rds_global_cluster.sre_5136.engine_version
  identifier           = "sre-5136-test-primary-cluster-instance"
  cluster_identifier   = aws_rds_cluster.sre_5136_primary.id
  instance_class       = "db.r4.large"
  db_subnet_group_name = "default"
}


# Try Creating a New Upgraded Cluster Based on Snapshot From Above Cluster
# -----------------------------------------------------------------------------

# Grab the most recent snapshot from the RDS cluster above.
data "aws_db_cluster_snapshot" "sre_5136_to_be_upgraded" {
  db_cluster_identifier = aws_rds_cluster.sre_5136_primary.id
  most_recent           = true
}

# Upgrade to the latest engine version supported by the snapshot.
resource "aws_rds_global_cluster" "sre_5136_upgraded" {
  global_cluster_identifier = "sre-5136-global-test-upgraded"
  engine                    = "aurora"
  engine_version            = "5.6.mysql_aurora.1.22.4"
  storage_encrypted         = true
}

# Use the snapshot of the above database to create a new dev database.
resource "aws_rds_cluster" "sre_5136_upgraded" {
  engine                    = aws_rds_global_cluster.sre_5136_upgraded.engine
  engine_version            = aws_rds_global_cluster.sre_5136_upgraded.engine_version
  cluster_identifier        = "sre-5136-upgraded-cluster"
  
  # Commented out as not needed when using a snapshot but needing when testing creation not based on a snapshot.
  # master_username           = "example_user"
  # master_password           = "password"
  # database_name             = "example_db"
  
  global_cluster_identifier = aws_rds_global_cluster.sre_5136_upgraded.id
  db_subnet_group_name      = "default"

  snapshot_identifier = data.aws_db_cluster_snapshot.sre_5136_to_be_upgraded.id

  lifecycle {
    ignore_changes = [snapshot_identifier]
  }
}

resource "aws_rds_cluster_instance" "sre_5136_upgraded" {
  engine               = aws_rds_global_cluster.sre_5136_upgraded.engine
  engine_version       = aws_rds_global_cluster.sre_5136_upgraded.engine_version
  identifier           = "sre-5136-upgraded-test-primary-cluster-instance"
  cluster_identifier   = aws_rds_cluster.sre_5136_upgraded.id
  instance_class       = "db.r5.large"
  db_subnet_group_name = "default"
}

Steps to Reproduce

  1. Create the first Global Aurora and snapshot it and create a manual snaphshot for testing.
  2. Add the second Global Aurora and terraform apply.

I was then double checking what should happen by creating the second cluster from scratch by uncommenting the master_username, master_password, and database_name and commenting out this section:

  snapshot_identifier = data.aws_db_cluster_snapshot.sre_5136_to_be_upgraded.id

  lifecycle {
    ignore_changes = [snapshot_identifier]
  }

The Global cluster is built correctly then.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

Would you like to implement a fix?

None

@rosstimson rosstimson added bug Addresses a defect in current functionality. needs-triage Waiting for first response or review from a maintainer. labels Jan 31, 2023
@github-actions
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added the service/rds Issues and PRs that pertain to the rds service. label Jan 31, 2023
@justinretzolk justinretzolk removed the needs-triage Waiting for first response or review from a maintainer. label Jan 31, 2023
@ewbankkit
Copy link
Contributor

User Guide for Aurora.

@jar-b
Copy link
Member

jar-b commented Mar 21, 2023

As the AWS documentation linked above indicates, it is not currently possible to restore a database from snapshot AND join to an existing global cluster in a single operation. You can however use a cluster restored from snapshot as the starting point for a new global cluster.

You can restore a snapshot of an Aurora DB cluster or from an Amazon RDS DB instance to use as the starting point for your Aurora global database. You restore the snapshot and create a new Aurora provisioned DB cluster at the same time. You then add another AWS Region to the restored DB cluster, thus turning it into an Aurora global database. Any Aurora DB cluster that you create using a snapshot in this way becomes the primary cluster of your Aurora global database


Here is a modified version of the original configuration which inverts the dependency flow so the global cluster is sourced from the cluster restored from snapshot:

Show/Hide Configuration
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {}

# Create Initial Cluster to be Copied Next
# -----------------------------------------------------------------------------
resource "aws_rds_global_cluster" "example" {
  global_cluster_identifier = "jb-test-global-cluster"
  engine                    = "aurora"
  engine_version            = "5.6.mysql_aurora.1.22.4"
  storage_encrypted         = true
}

resource "aws_rds_cluster" "example_primary" {
  engine                    = aws_rds_global_cluster.example.engine
  engine_version            = aws_rds_global_cluster.example.engine_version
  cluster_identifier        = "jb-test-primary-cluster"
  master_username           = "example_user"
  master_password           = "password"
  database_name             = "example_db"
  global_cluster_identifier = aws_rds_global_cluster.example.id
  db_subnet_group_name      = "default"

  skip_final_snapshot = true
}

resource "aws_rds_cluster_instance" "example_primary" {
  engine               = aws_rds_global_cluster.example.engine
  engine_version       = aws_rds_global_cluster.example.engine_version
  identifier           = "jb-test-primary-cluster-instance"
  cluster_identifier   = aws_rds_cluster.example_primary.id
  instance_class       = "db.r4.large"
  db_subnet_group_name = "default"
}

resource "aws_db_cluster_snapshot" "example_primary" {
  db_cluster_identifier          = aws_rds_cluster.example_primary.id
  db_cluster_snapshot_identifier = "jb-test-primary-cluster-snapshot"
}

# Try Creating a New Upgraded Cluster Based on Snapshot From Above Cluster
# -----------------------------------------------------------------------------

# Grab the most recent snapshot from the RDS cluster above.
data "aws_db_cluster_snapshot" "example_to_be_upgraded" {
  depends_on = [aws_db_cluster_snapshot.example_primary]

  db_cluster_identifier = aws_rds_cluster.example_primary.id
  most_recent           = true
}

# Use the snapshot of the above database to create a new dev database.
resource "aws_rds_cluster" "example_upgraded_primary" {
  # Because the global cluster is sourced from this cluster, the initial 
  # engine and engine_version values are defined here and automatically
  # inherited by the global cluster.
  engine             = "aurora"
  engine_version     = "5.6.mysql_aurora.1.22.4"
  cluster_identifier = "jb-test-upgraded-primary-cluster"

  db_subnet_group_name = "default"
  snapshot_identifier  = data.aws_db_cluster_snapshot.example_to_be_upgraded.id

  lifecycle {
    ignore_changes = [
      snapshot_identifier,
      # NOTE: Using this DB Cluster to create a Global Cluster, the
      # global_cluster_identifier attribute will become populated and
      # Terraform will begin showing it as a difference. Do not configure:
      # global_cluster_identifier = aws_rds_global_cluster.example.id
      # as it creates a circular reference. Use ignore_changes instead.
      global_cluster_identifier,
    ]
  }

  skip_final_snapshot = true
}

# Global cluster is now sourced from the existing cluster restored from snapshot
resource "aws_rds_global_cluster" "example_upgraded" {
  global_cluster_identifier    = "jb-test-upgraded-global-cluster"
  source_db_cluster_identifier = aws_rds_cluster.example_upgraded_primary.arn
  force_destroy                = true
}

resource "aws_rds_cluster_instance" "example_upgraded_primary" {
  engine               = aws_rds_cluster.example_upgraded_primary.engine
  engine_version       = aws_rds_cluster.example_upgraded_primary.engine_version
  identifier           = "jb-test-upgraded-primary-cluster-instance"
  cluster_identifier   = aws_rds_cluster.example_upgraded_primary.id
  instance_class       = "db.r5.large"
  db_subnet_group_name = "default"
}

This results in the intended relationship between resources. See the aws_rds_global_cluster documentation for additional background.

image

@jar-b
Copy link
Member

jar-b commented Mar 21, 2023

To avoid misleading outcomes, #30158 will also add a plan time check to prevent snapshot_identifier and global_cluster_identifier from being set in the same configuration.

@github-actions github-actions bot added this to the v4.60.0 milestone Mar 22, 2023
@github-actions
Copy link

This functionality has been released in v4.60.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/rds Issues and PRs that pertain to the rds service.
Projects
None yet
4 participants