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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_db_instance restore from snapshot intermittent state collision. #14785

Open
scott-kausler opened this issue Aug 21, 2020 · 1 comment
Open
Labels
bug Addresses a defect in current functionality. service/rds Issues and PRs that pertain to the rds service.

Comments

@scott-kausler
Copy link

Community Note

  • Please vote on this issue by adding a 馃憤 reaction to the original issue to help the community and maintainers prioritize this request
  • 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
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform CLI and Terraform AWS Provider Version

Terraform CLI Versionv0.12.20
Terraform AWS Provider Version 2.69.0

Affected Resource(s)

aws_db_instance

Terraform Configuration Files

resource "aws_db_instance" "this" {
...
  snapshot_identifier       = var.snapshot_arn

  parameter_group_name   = var.parameter_group_name
  backup_retention_period   = 7

  # When restoring from snapshot, terraform tries to set the password, but the provider could error out because of this.
  username = var.username
  password =  var.password
}

Debug Output

[2020-08-20T03:42:14.996Z] module.rds.aws_db_instance.this: Still creating... [9m50s elapsed]
[2020-08-20T03:42:21.517Z] module.rds.aws_db_instance.this: Creation complete after 9m58s [id=db-instance]
[2020-08-20T03:42:24.891Z] module.rds.aws_db_instance.this: Still creating... [10m0s elapsed]
[2020-08-20T03:42:34.826Z] module.rds.aws_db_instance.this: Still creating... [10m10s elapsed]
[2020-08-20T03:42:44.714Z] module.rds.aws_db_instance.this: Still creating... [10m20s elapsed]
[2020-08-20T03:42:55.028Z] module.rds.aws_db_instance.this: Still creating... [10m30s elapsed]
[2020-08-20T03:43:04.850Z] module.rds.aws_db_instance.this: Still creating... [10m40s elapsed]
[2020-08-20T03:43:05.775Z] module.rds.aws_db_instance.this: Refreshing state... [id=db-instance]
[2020-08-20T03:43:09.292Z] module.rds.aws_db_instance.this: Modifying... [id=db-instance]
[2020-08-20T03:43:09.292Z]
[2020-08-20T03:43:09.292Z] Error: Error modifying DB Instance db-instance: InvalidDBInstanceState: Database instance is not in available state.
[2020-08-20T03:43:09.292Z] status code: 400, request id: 7d9c0234-e364-49e7-9eb6-e8d7886a8d0e
[2020-08-20T03:43:09.292Z]
[2020-08-20T03:43:09.292Z] on .terraform/modules/rds/main.tf line 1, in resource "aws_db_instance" "this":
[2020-08-20T03:43:09.292Z] 1: resource "aws_db_instance" "this" {
[2020-08-20T03:43:09.292Z]

Expected Behavior

The db instance should have been restored successfully

Actual Behavior

Because the password was set and the backup_retention_period differed from the snapshot, the aws provider attempted to modify both, presumably without the correct wait between modifying.

Steps to Reproduce

  1. terraform apply

Important Factoids

  • When modifying the DB instance, the status may remain "Available" before moving to "Modifying" and then back to "Available" once the operation is complete. The provider should expect that behavior.
  • Presumably, this could be reproduced with any parameter that differs from the snapshot, such as allocated_storage, although it was encountered when only modifying parameter_group_name, backup_retention_period, and password.
@ghost ghost added the service/rds Issues and PRs that pertain to the rds service. label Aug 21, 2020
@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label Aug 21, 2020
@breathingdust breathingdust added bug Addresses a defect in current functionality. service/rds Issues and PRs that pertain to the rds service. and removed service/rds Issues and PRs that pertain to the rds service. needs-triage Waiting for first response or review from a maintainer. labels Sep 16, 2021
@ljluestc
Copy link

ljluestc commented Sep 7, 2023

# Define a data source to fetch snapshot information
data "aws_db_snapshot" "example" {
  snapshot_identifier = var.snapshot_arn
}

# Wait for the DB instance to become available
resource "null_resource" "wait_for_db" {
  provisioner "local-exec" {
    command = "sleep 300"  # Adjust the sleep duration as needed
  }
  depends_on = [aws_db_instance.this]
}

# Create the DB instance
resource "aws_db_instance" "this" {
  // Other DB instance attributes

  # Set the backup retention period based on the snapshot
  backup_retention_period = data.aws_db_snapshot.example.allocated_storage

  # Set the password based on the snapshot
  password = data.aws_db_snapshot.example.encrypted
}

# Optionally, you can add a local-exec provisioner to wait for the DB instance
# to become available before proceeding with other tasks.

use the aws_db_snapshot data source to fetch information about the snapshot, such as the allocated storage and encrypted attributes.
add a null_resource with a local-exec provisioner to wait for a particular duration (e.g., 300 seconds) to allow the DB instance to become fully available before proceeding with other tasks. You can adjust the sleep duration as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Addresses a defect in current functionality. service/rds Issues and PRs that pertain to the rds service.
Projects
None yet
Development

No branches or pull requests

3 participants