dalawh
dalawh

Reputation: 924

How to get the most recent shared AWS RDS snapshot by id?

I have 2 databases on AWS RDS, one for stage and one for production across 2 accounts. I am trying to copy the data in production to stage every x days. My plan was to make a copy of the most recent automatic backup snapshot in production and share it to the stage account before creating the database in stage using the shared snapshot from production. Everything was going right until I ran into what I believe is a bug but it could easily be that I made a mistake.

When I tried to get the most recent, shared, snapshot with an id of abcd in Terraform with data "aws_db_snapshot", I got no results.

data "aws_db_snapshot" "latest_prod_snapshot" {
  db_instance_identifier = "abcd"
  snapshot_type          = "shared"
  include_shared         = "true"
  most_recent            = "true"
}

Then I decided to try out AWS CLI. When I run this...

aws rds describe-db-snapshots --snapshot-type shared --include-shared

... I get this...

{
    "DBSnapshots": [
        {
            "MasterUsername": "root", 
            "LicenseModel": "general-public-license", 
            "InstanceCreateTime": "2018-01-13T00:00:00.000Z", 
            "Engine": "mysql", 
            "VpcId": "vpc-0000000000000000", 
            "SourceRegion": "us-east-1", 
            "AllocatedStorage": 20, 
            "Status": "available", 
            "PercentProgress": 100, 
            "SourceDBSnapshotIdentifier": "arn:aws:rds:us-east-1:000000000000:snapshot:rds:abcd-2020-01-13-00-00", 
            "DBSnapshotIdentifier": "arn:aws:rds:us-east-1:000000000000:snapshot:rds:abcd-2020-01-13-00-00", 
            "DBSnapshotArn": "arn:aws:rds:us-east-1:000000000000:snapshot:rds:abcd-2020-01-13-00-00", 
            "EngineVersion": "5.6.41", 
            "ProcessorFeatures": [], 
            "OptionGroupName": "default:mysql-5-6", 
            "SnapshotCreateTime": "2020-01-13T00:00:00.000Z", 
            "AvailabilityZone": "us-east-1b", 
            "StorageType": "gp2", 
            "Encrypted": false, 
            "IAMDatabaseAuthenticationEnabled": false, 
            "DbiResourceId": "db-AAAAAAAAAAAAAAAAAAAAAAAAA", 
            "SnapshotType": "shared", 
            "Port": 3306, 
            "DBInstanceIdentifier": "abcd"
        }
    ]
}

... which is what I expected. Looking at the response, I would expect the db instance id to be abcd but when I run this...

aws rds describe-db-snapshots --snapshot-type shared --include-shared --db-instance-identifier abcd

... or this...

aws rds describe-db-snapshots --snapshot-type shared --include-shared --filters Name=db-instance-id,Values=abcd

... I get this...

{
    "DBSnapshots": []
}

... which is not what I would have expected. Is this a bug or am I doing something wrong? I looked through their documentation but I may have missed something.

Upvotes: 5

Views: 2197

Answers (2)

yurez
yurez

Reputation: 3222

Per AWS Support, DBInstanceIdentifier is used to display results of only the snapshots owned by calling customer, and therefore does not return results of the shared snapshots. In order to filter and display only the shared snapshots using the DBInstanceIdentifier, --query may be used to filter by the DBInstanceIdentifier value:

aws rds describe-db-snapshots --snapshot-type shared --include-shared --query "DBSnapshots[?DBInstanceIdentifier=='abcd']" 

Upvotes: 0

edmamerto
edmamerto

Reputation: 8165

While we wait for AWS to fix this. Here are some things you can do as workaround:

  1. In the stage account copy the shared snapshot and use the copy instead of the shared.
  2. In Terraform, rollback to an older provider version.

2.27 works for me

provider "aws" {
  version                 = "2.27.0"
}

Upvotes: 1

Related Questions