aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

aws_rds: Fails to create DB due to error "DBInstance ady9tx26mcix2t not found. (Service: AmazonRDS; Status Code: 404; Error Code: DBInstanceNotFound"

Open matsaune opened this issue 4 years ago • 17 comments

Describe the bug

I am trying to create a Postgres DB using CDK. When I do this the create fails with the errorMsg: DBInstance ady9tx26mcix2t not found. (Service: AmazonRDS; Status Code: 404; Error Code: DBInstanceNotFound.

It seems CDK is not waiting for AWS to initialise the DB properly before some lookup.

Expected Behavior

I would expect DB to get created.

Current Behavior

CDK starts to provision DB and I can see that the DB is created in the AWS console. This is the CDK output log:

This is the CDK output:

ArchiveService2Stack | 0/7 | 7:14:11 AM | CREATE_IN_PROGRESS   | AWS::CloudFormation::Stack                  | ArchiveService2Stack User Initiated
ArchiveService2Stack | 0/7 | 7:14:15 AM | CREATE_IN_PROGRESS   | AWS::RDS::DBSubnetGroup                     | db-instance/SubnetGroup/Default (dbinstanceSubnetGroupE4F5C66F) 
ArchiveService2Stack | 0/7 | 7:14:15 AM | CREATE_IN_PROGRESS   | AWS::CDK::Metadata                          | CDKMetadata/Default (CDKMetadata) 
ArchiveService2Stack | 0/7 | 7:14:15 AM | CREATE_IN_PROGRESS   | AWS::EC2::SecurityGroup                     | db-instance/SecurityGroup (dbinstanceSecurityGroupF70613D6) 
ArchiveService2Stack | 0/7 | 7:14:16 AM | CREATE_IN_PROGRESS   | AWS::SecretsManager::Secret                 | db-instance/Secret (ArchiveService2StackdbinstanceSecret4305B9BC3fdaad7efa858a3daf9490cf0a702aeb) 
ArchiveService2Stack | 0/7 | 7:14:16 AM | CREATE_IN_PROGRESS   | AWS::RDS::DBSubnetGroup                     | db-instance/SubnetGroup/Default (dbinstanceSubnetGroupE4F5C66F) Resource creation Initiated
ArchiveService2Stack | 1/7 | 7:14:17 AM | CREATE_COMPLETE      | AWS::RDS::DBSubnetGroup                     | db-instance/SubnetGroup/Default (dbinstanceSubnetGroupE4F5C66F) 
ArchiveService2Stack | 1/7 | 7:14:17 AM | CREATE_IN_PROGRESS   | AWS::CDK::Metadata                          | CDKMetadata/Default (CDKMetadata) Resource creation Initiated
ArchiveService2Stack | 1/7 | 7:14:17 AM | CREATE_IN_PROGRESS   | AWS::SecretsManager::Secret                 | db-instance/Secret (ArchiveService2StackdbinstanceSecret4305B9BC3fdaad7efa858a3daf9490cf0a702aeb) Resource creation Initiated
ArchiveService2Stack | 2/7 | 7:14:17 AM | CREATE_COMPLETE      | AWS::CDK::Metadata                          | CDKMetadata/Default (CDKMetadata) 
ArchiveService2Stack | 3/7 | 7:14:17 AM | CREATE_COMPLETE      | AWS::SecretsManager::Secret                 | db-instance/Secret (ArchiveService2StackdbinstanceSecret4305B9BC3fdaad7efa858a3daf9490cf0a702aeb) 
ArchiveService2Stack | 3/7 | 7:14:20 AM | CREATE_IN_PROGRESS   | AWS::EC2::SecurityGroup                     | db-instance/SecurityGroup (dbinstanceSecurityGroupF70613D6) Resource creation Initiated
ArchiveService2Stack | 4/7 | 7:14:21 AM | CREATE_COMPLETE      | AWS::EC2::SecurityGroup                     | db-instance/SecurityGroup (dbinstanceSecurityGroupF70613D6) 
ArchiveService2Stack | 4/7 | 7:14:23 AM | CREATE_IN_PROGRESS   | AWS::RDS::DBInstance                        | db-instance (dbinstance310A317F) 
ArchiveService2Stack | 4/7 | 7:14:25 AM | CREATE_IN_PROGRESS   | AWS::RDS::DBInstance                        | db-instance (dbinstance310A317F) Resource creation Initiated
4/7 Currently in progress: ArchiveService2Stack, dbinstance310A317F
ArchiveService2Stack | 4/7 | 7:15:09 AM | CREATE_FAILED        | AWS::RDS::DBInstance                        | db-instance (dbinstance310A317F) DBInstance ady9tx26mcix2t not found. (Service: AmazonRDS; Status Code: 404; Error Code: DBInstanceNotFound; Request ID: 737ad075-c085-43aa-911a-1bff4c6b9c3c; Proxy: null)
ArchiveService2Stack | 4/7 | 7:15:10 AM | ROLLBACK_IN_PROGRESS | AWS::CloudFormation::Stack                  | ArchiveService2Stack The following resource(s) failed to create: [dbinstance310A317F]. Rollback requested by user.
Stack ArchiveService2Stack has an ongoing operation in progress and is not stable (ROLLBACK_IN_PROGRESS (The following resource(s) failed to create: [dbinstance310A317F]. Rollback requested by user.))
Stack ArchiveService2Stack has an ongoing operation in progress and is not stable (ROLLBACK_IN_PROGRESS)
ArchiveService2Stack | 4/7 | 7:15:20 AM | DELETE_IN_PROGRESS   | AWS::RDS::DBInstance                        | db-instance (dbinstance310A317F) 
ArchiveService2Stack | 4/7 | 7:15:21 AM | DELETE_FAILED        | AWS::RDS::DBInstance                        | db-instance (dbinstance310A317F) DBInstance ady9tx26mcix2t was not found during DescribeDBInstances
ArchiveService2Stack | 4/7 | 7:15:21 AM | DELETE_IN_PROGRESS   | AWS::RDS::DBSubnetGroup                     | db-instance/SubnetGroup/Default (dbinstanceSubnetGroupE4F5C66F) 

This is the log from CloudFormation console (same error as above):

DBInstance ady9tx26mcix2t not found. (Service: AmazonRDS; Status Code: 404; Error Code: DBInstanceNotFound; Request ID: 737ad075-c085-43aa-911a-1bff4c6b9c3c; Proxy: null)

Reproduction Steps

import {Stack, StackProps, aws_rds as rds, aws_ec2 as ec2, aws_iam as iam, aws_ecs as ecs, Duration, RemovalPolicy,} from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class CdkStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);


    const vpc = ec2.Vpc.fromLookup(this, 'CoreVPC', { vpcName: 'SOME_VPC_NAME' });
    
    const dbInstance = new rds.DatabaseInstance(this, 'db-instance', {
      vpc,
      vpcSubnets: {
        subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
      },
      engine: rds.DatabaseInstanceEngine.postgres({
        version: rds.PostgresEngineVersion.VER_14,
      }),
      instanceType: ec2.InstanceType.of(
          ec2.InstanceClass.BURSTABLE3,
          ec2.InstanceSize.SMALL,
      ),
      credentials: rds.Credentials.fromGeneratedSecret('postgres'),
      multiAz: false,
      allocatedStorage: 20,
      maxAllocatedStorage: 25,
      allowMajorVersionUpgrade: false,
      autoMinorVersionUpgrade: true,
      backupRetention: Duration.days(0),
      deleteAutomatedBackups: true,
      removalPolicy: RemovalPolicy.DESTROY,
      deletionProtection: false,
      databaseName: 'archive',
      publiclyAccessible: false,
    });

  }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.20.0 (build 738ef49)

Framework Version

No response

Node.js Version

v16.4.2

OS

macOS 12.3.1 (21E258)

Language

Typescript

Language Version

3.9.10

Other information

No response

matsaune avatar May 04 '22 05:05 matsaune

Hi @matsaune,

thanks for opening the issue.

Can you check whether this also happens for a new VPC? Can you substitute the Vpc.fromLookup() with new Vpc(), and see if you get the same error?

Thanks, Adam

skinny85 avatar May 04 '22 18:05 skinny85

Yes, I will try this and provide you with feedback on this.

matsaune avatar May 05 '22 05:05 matsaune

@skinny85 Hi, just tried this on my private account where I created a new VPC with no properties. Lots of stuff getting created so took a bit longer. Anyways in this usecase the postgres DB is created successfully. Mats

matsaune avatar May 05 '22 07:05 matsaune

Hi @matsaune,

thanks for the info, and for making that discovery! That means the problem most likely lies somewhere in the interaction between the Database, and the VPC you got from Vpc.fromLookup().

Can you explain what that VPC is? How did you create it - was it with CDK, or in a different way?

Thanks, Adam

skinny85 avatar May 05 '22 17:05 skinny85

@skinny85 The VPC in question is a corporate provided VPC initialised by a cloudformation template. We are running our applications in a corporate context and we have some limits in what we are allowed to do and so on. Much of the basic "infrastructure" (vpc, roles etc) is provided and not allowed to change.

If you have some concrete issue/area you want me to look into I can do this for you.

Is there some way to tell cdk to wait a bit?

Mats

matsaune avatar May 06 '22 12:05 matsaune

Is there some way to tell cdk to wait a bit?

Can you clarify? Wait on what? And why do you think waiting would help with this problem?

skinny85 avatar May 06 '22 17:05 skinny85

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar May 08 '22 20:05 github-actions[bot]

On other constructs you can specify construct.dependsOn(another.construct). In this case there is no other construct that is created and thus nothing that can be used. But in java it is possible to specify a tread.wait(timeout). Not sure if there are any similar mechanisms in cdk, but is a hack in this case in order to slow process down somewhat. Anyways probably is a shot in the dark.

matsaune avatar May 09 '22 10:05 matsaune

I saw that in your initial code, you used subnetType: ec2.SubnetType.PRIVATE_ISOLATED.

Can you check whether changing this value makes the deployment succeed (with your Vp.fromLookup())? If it does, it would help us narrow down the problem.

skinny85 avatar May 09 '22 21:05 skinny85

Hi, I have tried with the different SubnetTypes now. The synth of the stack fails always if PRIVATE_ISOLATED not is selected:

/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.ts:600 throw new Error(There are no '${subnetType}' subnet groups in this VPC. Available types: ${availableTypes}); ^ Error: There are no 'Public' subnet groups in this VPC. Available types: Isolated at LookedUpVpc.selectSubnetObjectsByType (/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.ts:600:13) at LookedUpVpc.selectSubnetObjects (/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.ts:556:22) at LookedUpVpc.selectSubnets (/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.ts:431:26) at new SubnetGroup (/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-rds/lib/subnet-group.ts:75:37) at new DatabaseInstanceNew (/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-rds/lib/instance.ts:668:46) at new DatabaseInstanceSource (/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-rds/lib/instance.ts:856:5) at new DatabaseInstance (/PROJECT_PATH/cdk/node_modules/aws-cdk-lib/aws-rds/lib/instance.ts:1010:5) at new CdkStack (/PROJECT_PATH/cdk/lib/cdk-stack.ts:57:24) at Object. (/PROJECT_PATH/cdk/bin/cdk.ts:8:1)

This is probably due to this in CDK_CONTEXT_JSON: subnetGroups":[{"name":"Isolated","type":"Isolated","subnets":[SUB_NET_DATA]

Let me know if there is some other info I can contribute

matsaune avatar May 10 '22 11:05 matsaune

Hi, did an attempt in our sandbox env (not as strict) with: vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC, }, This deploys successfully where the only change of code is SubnetType (and vpc retrieved by id not name but I dont suspect that this matters)

matsaune avatar May 10 '22 12:05 matsaune

Hmm. That would suggest that there's something in that VPC that makes it fail when used by the CDK.

Can you try and see what are the differences in the generated template resources for the Database in your sandbox, and in your production environment? Maybe that will reveal some clue about what the problem is.

skinny85 avatar May 10 '22 16:05 skinny85

Hi, did a diff on the generated template, however I do not think this gives us any help:

cdk.out git:(master) ✗ diff ArchiveService2Stack.template.dev.json ArchiveService2Stack.template.sand.json 21,23c21,22 < "subnet-068169adb33", < "subnet-09ad7bb8b2b", < "subnet-07a78331644" --- > "subnet-061d17a927a1", > "subnet-05ba35d7798" 41c40 < "VpcId": "vpc-0ed591005918" --- > "VpcId": "vpc-0085ececa23b" cdk.out git:(master) ✗

The only differences are the ID's so this does not provide any clues

matsaune avatar May 11 '22 08:05 matsaune

OK. Another potential idea: you said that the VPC was created by CloudFormation. Given that, are the Isolated subnets correctly recognized by CDK? More details here.

skinny85 avatar May 11 '22 16:05 skinny85

I do not think that this is the issue. We are using "similar" features in other Stacks ( subnets: coreVpc.selectSubnets({onePerAz: true}).subnetIds, ).

Did try to tweake the subnetSelection a bit but still getting the same error. However looking into the logs a bit further it seems to me that the error is due to some error/misconfiguration in handling the ID of the database.

In stack events I see this:

dbinstance310A317F | CREATE_IN_PROGRESS | Resource creation Initiated

On fail this event is displayed: dbinstance310A317F | CREATE_FAILED | DBInstance ad1xn2jdugi3ugf not found. (Service: AmazonRDS; Status Code: 404; Error Code: DBInstanceNotFound; Request ID: 3b1b6240-a67f-4324-b113-18ab6e66151b; Proxy: null)

So the ID of the DB that is created is dbinstance310A317F but further lookups are using ad1xn2jdugi3ugf.

If this theory is correct there is something with the provisioning of the db in regards to what ID is used when private networks are involved. Attaching screendump of stack console

Screenshot 2022-05-12 at 10 56 43

matsaune avatar May 12 '22 09:05 matsaune

I have figured out what is wrong. The DB is not encrypted and we have company rules stating that db's has to be encrypted (different rules in different accounts apparently). Anyways there is a "CloudCustodian" that deletes the db when an unencrypted db is discovered.

Added this to the cdk construct: storageEncrypted:true

I should have spotted this one earlier. Thanks for you time and effort to solve this issue, even if the error was on my side

matsaune avatar May 12 '22 09:05 matsaune

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar May 12 '22 09:05 github-actions[bot]