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

feat(rds): make VPC optional for serverless Clusters #17413

Merged

Conversation

CorentinDoue
Copy link
Contributor

@CorentinDoue CorentinDoue commented Nov 9, 2021

Fixes #17401


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Nov 9, 2021

@github-actions github-actions bot added the @aws-cdk/aws-rds Related to Amazon Relational Database label Nov 9, 2021
Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @CorentinDoue! In general looks great, just a few minor comments, mainly on the tests.

packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
@@ -442,7 +454,7 @@ export class ServerlessCluster extends ServerlessClusterBase {
databaseName: props.defaultDatabaseName,
dbClusterIdentifier: clusterIdentifier,
dbClusterParameterGroupName: clusterParameterGroupConfig?.parameterGroupName,
dbSubnetGroupName: this.subnetGroup.subnetGroupName,
dbSubnetGroupName: this.subnetGroup?.subnetGroupName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually legal to pass dbSubnetGroupName without vpcSecurityGroupIds? Seems kind of weird...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea, the CFN doc treats them independently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
});
});

test('can\'t create a Serverless cluster without vpc but with imported vpc subnets', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('can\'t create a Serverless cluster without vpc but with imported vpc subnets', () => {
test("can't create a Serverless cluster without VPC but with imported VPC subnets", () => {

Please use the same casing in the other test descriptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem this could be handled by prettier to avoid you loosing your time ;)

packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
@skinny85
Copy link
Contributor

skinny85 commented Nov 9, 2021

BTW, a unit test is currently failing:

@aws-cdk/aws-rds: FAIL test/serverless-cluster.test.js (14.717 s)
@aws-cdk/aws-rds:   � serverless cluster › can create a Serverless cluster without vpc but with imported subnet group
@aws-cdk/aws-rds:     None of 1 resources matches resource 'AWS::RDS::DBCluster' with {
@aws-cdk/aws-rds:       "$objectLike": {
@aws-cdk/aws-rds:         "Engine": "aurora-postgresql",
@aws-cdk/aws-rds:         "DBClusterParameterGroupName": "default.aurora-postgresql10",
@aws-cdk/aws-rds:         "EngineMode": "serverless",
@aws-cdk/aws-rds:         "DBSubnetGroupName": {
@aws-cdk/aws-rds:           "Ref": "SubnetGroup12345"
@aws-cdk/aws-rds:         },
@aws-cdk/aws-rds:         "MasterUsername": {
@aws-cdk/aws-rds:           "Fn::Join": [
@aws-cdk/aws-rds:             "",
@aws-cdk/aws-rds:             [
@aws-cdk/aws-rds:               "{{resolve:secretsmanager:",
@aws-cdk/aws-rds:               {
@aws-cdk/aws-rds:                 "Ref": "DatabaseSecret3B817195"
@aws-cdk/aws-rds:               },
@aws-cdk/aws-rds:               ":SecretString:username::}}"
@aws-cdk/aws-rds:             ]
@aws-cdk/aws-rds:           ]
@aws-cdk/aws-rds:         },
@aws-cdk/aws-rds:         "MasterUserPassword": {
@aws-cdk/aws-rds:           "Fn::Join": [
@aws-cdk/aws-rds:             "",
@aws-cdk/aws-rds:             [
@aws-cdk/aws-rds:               "{{resolve:secretsmanager:",
@aws-cdk/aws-rds:               {
@aws-cdk/aws-rds:                 "Ref": "DatabaseSecret3B817195"
@aws-cdk/aws-rds:               },
@aws-cdk/aws-rds:               ":SecretString:password::}}"
@aws-cdk/aws-rds:             ]
@aws-cdk/aws-rds:           ]
@aws-cdk/aws-rds:         },
@aws-cdk/aws-rds:         "VpcSecurityGroupIds": [
@aws-cdk/aws-rds:           "SecurityGroupId12345"
@aws-cdk/aws-rds:         ]
@aws-cdk/aws-rds:       }
@aws-cdk/aws-rds:     }.
@aws-cdk/aws-rds:     - Field DBSubnetGroupName mismatch: Object type mismatch, Field VpcSecurityGroupIds missing in:
@aws-cdk/aws-rds:         {
@aws-cdk/aws-rds:           "Type": "AWS::RDS::DBCluster",
@aws-cdk/aws-rds:           "Properties": {
@aws-cdk/aws-rds:             "Engine": "aurora-postgresql",
@aws-cdk/aws-rds:             "DBClusterParameterGroupName": "default.aurora-postgresql10",
@aws-cdk/aws-rds:             "DBSubnetGroupName": "SubnetGroupId12345",
@aws-cdk/aws-rds:             "EngineMode": "serverless",
@aws-cdk/aws-rds:             "MasterUsername": {
@aws-cdk/aws-rds:               "Fn::Join": [
@aws-cdk/aws-rds:                 "",
@aws-cdk/aws-rds:                 [
@aws-cdk/aws-rds:                   "{{resolve:secretsmanager:",
@aws-cdk/aws-rds:                   {
@aws-cdk/aws-rds:                     "Ref": "DatabaseSecret3B817195"
@aws-cdk/aws-rds:                   },
@aws-cdk/aws-rds:                   ":SecretString:username::}}"
@aws-cdk/aws-rds:                 ]
@aws-cdk/aws-rds:               ]
@aws-cdk/aws-rds:             },
@aws-cdk/aws-rds:             "MasterUserPassword": {
@aws-cdk/aws-rds:               "Fn::Join": [
@aws-cdk/aws-rds:                 "",
@aws-cdk/aws-rds:                 [
@aws-cdk/aws-rds:                   "{{resolve:secretsmanager:",
@aws-cdk/aws-rds:                   {
@aws-cdk/aws-rds:                     "Ref": "DatabaseSecret3B817195"
@aws-cdk/aws-rds:                   },
@aws-cdk/aws-rds:                   ":SecretString:password::}}"
@aws-cdk/aws-rds:                 ]
@aws-cdk/aws-rds:               ]
@aws-cdk/aws-rds:             },
@aws-cdk/aws-rds:             "StorageEncrypted": true
@aws-cdk/aws-rds:           },
@aws-cdk/aws-rds:           "UpdateReplacePolicy": "Snapshot",
@aws-cdk/aws-rds:           "DeletionPolicy": "Snapshot"
@aws-cdk/aws-rds:         }
@aws-cdk/aws-rds:       983 |
@aws-cdk/aws-rds:       984 |     // THEN
@aws-cdk/aws-rds:     > 985 |     expect(stack).toHaveResource('AWS::RDS::DBCluster', {
@aws-cdk/aws-rds:           |                   ^
@aws-cdk/aws-rds:       986 |       Engine: 'aurora-postgresql',
@aws-cdk/aws-rds:       987 |       DBClusterParameterGroupName: 'default.aurora-postgresql10',
@aws-cdk/aws-rds:       988 |       EngineMode: 'serverless',
@aws-cdk/aws-rds:       at Object.<anonymous> (test/serverless-cluster.test.ts:985:19)

@CorentinDoue
Copy link
Contributor Author

BTW, a unit test is currently failing

I was using yarn test locally. I didn't realize it only test the js files. It's a little misleading ;)

@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch from ce03d19 to 45687af Compare November 10, 2021 09:30
@mergify mergify bot dismissed skinny85’s stale review November 10, 2021 09:30

Pull request has been modified.

@CorentinDoue
Copy link
Contributor Author

@skinny85 You can check the changes in the fixup commit. I will fixup it in one commit when the PR will be ready to merge

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @CorentinDoue! I've commented on the simple trick below that allows you to assert the absence of a value in the template.

packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
@skinny85 skinny85 changed the title feat(aws-rds): make serverless-cluster vpc optional feat(rds): make VPC optional for serverless Clusters Nov 10, 2021
@skinny85
Copy link
Contributor

@CorentinDoue what's the status here? Are you still planning to work on this, or should someone take this over?

@skinny85 skinny85 removed their assignment Dec 15, 2021
@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch from 45687af to 66e9b55 Compare December 15, 2021 08:23
@CorentinDoue
Copy link
Contributor Author

@skinny85 sorry I forgot about this PR. It should be ok now.

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, looks really good @CorentinDoue! I have a few minor last-minute comments, if you'll humor me 😉.

Comment on lines 602 to 614
**Note**: Using the Data API, you can interact with a ServerlessCluster without using its VPC. Therefore, the parameter "vpc" is optional.
The cluster will be created in a VPC, but you will know nothing about it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Is this implying that when enableDataApi is false, a VPC is required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the two notions are separated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we polish this wording then? Because that's what it's implying right now (at least that's how I read it).

@@ -106,11 +106,13 @@ export interface ServerlessClusterProps {

/**
* The VPC that this Aurora Serverless cluster has been created in.
*
* @default - No VPC related construct will be created:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @default - No VPC related construct will be created:
* @default - no VPC will be used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An RDS cluster can't be outside a VPC. So if no VPC is provided the cluster will be created in a VPC, with subnets and security groups but we will not manage them at all. That's what I expect when I use the Data API, I don't want to handle network configuration.
Here an example of Aurora Cluster created without VPC (directly with CfnDBCluster)
Screenshot from 2022-01-11 10-19-44
The created cluster is
aurora-redacted
You can see there is a VPC, 3 subnets, and a security group but I never provided them and I will never interact with them.

So I propose you the folowing description:

Suggested change
* @default - No VPC related construct will be created:
* @default - The cluster will be created in the default VPC

That's what I understand of the Cloud Formation doc
Screenshot from 2022-01-11 10-33-16
https://docs.aws.amazon.com/fr_fr/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbcluster.html#aws-resource-rds-dbcluster-properties

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation. I agree with your suggestion 🙂.

packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
vpcSubnets: props.vpcSubnets,
removalPolicy: props.removalPolicy === RemovalPolicy.RETAIN ? props.removalPolicy : undefined,
});
let securityGroups: ec2.ISecurityGroup[] | undefined = props.securityGroups;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here, if props.vpc is undefined, but props.securityGroups is not, we will actually set these Security Groups in the Cluster. Is that even correct? Should we check this, and error out, similarly like we do if props. vpcSubnets is set without props.vpc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's possible to pass some securityGroups linked to the default VPC without passing it explicitly. But I agree it's better to force the VPC to be provided to use them. I add an error.

packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch from eff911d to 7da2955 Compare January 11, 2022 10:14
@mergify mergify bot dismissed skinny85’s stale review January 11, 2022 10:15

Pull request has been modified.

@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch from 7da2955 to 50813de Compare January 11, 2022 10:15
@CorentinDoue
Copy link
Contributor Author

@skinny85 I rebased and treated the last comments :)

@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch from 50813de to 86c0802 Compare January 11, 2022 12:47
Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @CorentinDoue! A few small documentation tweaks, and we're good to go.

@@ -613,4 +610,7 @@ cluster.grantDataApiAccess(fn);

**Note**: To invoke the Data API, the resource will need to read the secret associated with the cluster.

**Note**: Using the Data API, you can interact with a ServerlessCluster without using its VPC. Therefore, the parameter "vpc" is optional.
The cluster will be created in a VPC, but you will know nothing about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, let's leave this example as-is, and add a new sub-section, with the appropriate header level, talking about VPC being optional.

If optional VPC and enableDataApi are indeed separate, let's separate them in our documentation as well.

packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
@skinny85
Copy link
Contributor

After #18366 is merged, updating from master should fix the build (it's a problem during a release).

@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch 2 times, most recently from 77d67b3 to 6868d6d Compare February 18, 2022 17:58
@CorentinDoue
Copy link
Contributor Author

@skinny85 I found some time to work on this PR. It should be ok now.

@skinny85
Copy link
Contributor

Looks like our backwards compatibility checker doesn't like some of the changes:

err  - PROP @aws-cdk/aws-rds.ServerlessCluster.securityGroups: type Optional<Array<@aws-cdk/aws-ec2.ISecurityGroup>> (formerly Array<@aws-cdk/aws-ec2.ISecurityGroup>): output type is now optional [changed-type:@aws-cdk/aws-rds.ServerlessCluster.securityGroups]
err  - PROP @aws-cdk/aws-rds.ServerlessClusterFromSnapshot.securityGroups: type Optional<Array<@aws-cdk/aws-ec2.ISecurityGroup>> (formerly Array<@aws-cdk/aws-ec2.ISecurityGroup>): output type is now optional [changed-type:@aws-cdk/aws-rds.ServerlessClusterFromSnapshot.securityGroups]

I guess since they are protected, we can't change them to be optional.

Would you mind making these required again? Simply returning an empty list should be good enough.

@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch from 6868d6d to c8cb3f7 Compare February 21, 2022 12:51
@CorentinDoue
Copy link
Contributor Author

Looks like our backwards compatibility checker doesn't like some of the changes:

err  - PROP @aws-cdk/aws-rds.ServerlessCluster.securityGroups: type Optional<Array<@aws-cdk/aws-ec2.ISecurityGroup>> (formerly Array<@aws-cdk/aws-ec2.ISecurityGroup>): output type is now optional [changed-type:@aws-cdk/aws-rds.ServerlessCluster.securityGroups]
err  - PROP @aws-cdk/aws-rds.ServerlessClusterFromSnapshot.securityGroups: type Optional<Array<@aws-cdk/aws-ec2.ISecurityGroup>> (formerly Array<@aws-cdk/aws-ec2.ISecurityGroup>): output type is now optional [changed-type:@aws-cdk/aws-rds.ServerlessClusterFromSnapshot.securityGroups]

I guess since they are protected, we can't change them to be optional.

Would you mind making these required again? Simply returning an empty list should be good enough.

@skinny85 fixed :)

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @CorentinDoue! A few tiny last comments, and we'll get this merged in!

const cluster = new rds.ServerlessCluster(this, 'AnotherCluster', {
engine: rds.DatabaseClusterEngine.AURORA_MYSQL,
vpc,
enableDataApi: true, // Optional - will be automatically set if you call grantDataApiAccess()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert these changes. It's enough to mention that vpc is optional below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example shows the minimal configuration to enable the data API. As the vpc is now optional, I think it shouldn't be in the example

packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/lib/serverless-cluster.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-rds/test/serverless-cluster.test.ts Outdated Show resolved Hide resolved
@CorentinDoue CorentinDoue force-pushed the feat/make-serverless-cluster-vpc-optional branch from c8cb3f7 to 8215643 Compare February 22, 2022 08:32
@CorentinDoue
Copy link
Contributor Author

@skinny85 it should be ok

skinny85
skinny85 previously approved these changes Feb 22, 2022
Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @CorentinDoue!

@mergify mergify bot dismissed skinny85’s stale review February 22, 2022 19:28

Pull request has been modified.

@mergify
Copy link
Contributor

mergify bot commented Feb 22, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: bc8ec49
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 4f7818d into aws:master Feb 22, 2022
@mergify
Copy link
Contributor

mergify bot commented Feb 22, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-rds Related to Amazon Relational Database
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-rds): vpc should be optional in ServerlessCluster
3 participants