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

rds: cannot adjust security groups of existing RDS #30285

Closed
adam-nielsen opened this issue May 21, 2024 · 4 comments
Closed

rds: cannot adjust security groups of existing RDS #30285

adam-nielsen opened this issue May 21, 2024 · 4 comments
Labels
@aws-cdk/aws-rds Related to Amazon Relational Database bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@adam-nielsen
Copy link

Describe the bug

If you create an RDS in your stack, you can adjust the security groups (e.g. allowing an EC2 to connect) which is fine.

However if you import an existing RDS into your stack, you can't adjust the security groups, so there appears to be no way to grant anything in a stack access to an existing RDS.

Expected Behavior

I expected the call to rds.connections.allowFrom() to work the same whether the RDS instance was created in the current stack or imported from another stack with rds.DatabaseCluster.fromDatabaseClusterAttributes().

Current Behavior

If the database already exists outside of the current stack, the allowFrom()/allowTo() calls produce no errors, but the Cfn output does not contain any mention of the security group changes.

Reproduction Steps

import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';

import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as rds from 'aws-cdk-lib/aws-rds';

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

    const vpc = ec2.Vpc.fromLookup(this, 'vpc', {
      vpcId: 'vpc-12345',
    });

    const client = new ec2.Instance(this, 'test', {
      vpc: vpc,
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
      machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
    });

    // If we create a new RDS, then the security groups work fine:
    /*
    const db = new rds.DatabaseCluster(this, 'Database', {
      engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_01_0 }),
      writer: rds.ClusterInstance.provisioned('writer', {
        instanceType: ec2.InstanceType.of(ec2.InstanceClass.R6G, ec2.InstanceSize.XLARGE4),
      }),
      vpc,
    });
    */

    // Produces:
    //    DatabaseSecurityGroupfromteststacktestInstanceSecurityGroup26634811330682648EE3:
    //      Type: AWS::EC2::SecurityGroupIngress
    //      Properties:
    //        Description: EC2 access to RDS
    //        FromPort: 3306
    //        ...

    // But if we get an existing RDS instead, then the security groups do not
    // get mentioned at all in the `cdk synth` output:
    const db = rds.DatabaseCluster.fromDatabaseClusterAttributes(this, 'existing-rds', {
      clusterIdentifier: 'existing-rds', // RDS cluster name
    });

    // Neither of these options work if the RDS already exists outside this stack.
    client.connections.allowTo(db, ec2.Port.MYSQL_AURORA, 'EC2 access to RDS');
    db.connections.allowFrom(client, ec2.Port.MYSQL_AURORA, 'RDS access from EC2');
  }
}

If you comment out the lower const db and uncomment the upper one, you can switch between importing an RDS and creating a new RDS, to see the difference.

Running this with cdk synth is enough to check the Cfn output to see if the security groups are mentioned or not.

Possible Solution

No response

Additional Information/Context

We have a shared RDS used by multiple independent applications, so when we deploy an application we want the resources (EC2/ECS/Lambda/etc.) to be able to access the RDS, without having to use a blanket rule that permits access to the whole VPC.

CDK CLI Version

2.142.1 (build ed4e152)

Framework Version

No response

Node.js Version

v18.18.2

OS

Arch Linux

Language

TypeScript

Language Version

TypeScript (5.3.3)

Other information

No response

@adam-nielsen adam-nielsen added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 21, 2024
@github-actions github-actions bot added the @aws-cdk/aws-rds Related to Amazon Relational Database label May 21, 2024
@pahud pahud self-assigned this May 21, 2024
@pahud pahud added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels May 21, 2024
@pahud pahud removed their assignment May 21, 2024
@pahud
Copy link
Contributor

pahud commented May 21, 2024

If you need to separate the instances and DB in two stacks, consider the code below to have DatabaseStack and InstanceStack:

// create a DatabaseStack that creates a random DatabaseCluster
export class DatabaseStack extends Stack {
  readonly cluster: rds.DatabaseCluster;
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const vpc = ec2.Vpc.fromLookup(this, 'Vpc', { isDefault: true });
    // create a random database cluster
    this.cluster = new rds.DatabaseCluster(this, 'Database', {
      engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_01_0 }),
      writer: rds.ClusterInstance.provisioned('writer', {
        instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MEDIUM),
      }),
      vpc,
    });
  }
}

export interface InstanceStackProps extends StackProps {
  readonly cluster: rds.IDatabaseCluster;
}

// create a InstanceStack that creates a random EC2 Instance
export class InstanceStack extends Stack {
  constructor(scope: Construct, id: string, props: InstanceStackProps) {
    super(scope, id, props);

    const vpc = ec2.Vpc.fromLookup(this, 'Vpc', { isDefault: true });
    // create a random EC2 instance
    const instance = new ec2.Instance(this, 'test', {
      vpc,
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
      machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
    });

    props.cluster.connections.allowFrom(instance.connections, ec2.Port.tcp(3306), 'from ec2 instance to rds')

  }
}

app.ts

const dbstack = new DatabaseStack(app, 'DatabaseStack', { env });

new InstanceStack(app, 'InstanceStack', { 
    env,
    cluster: dbstack.cluster,
});

on npx cdk synth --all

you should see this in your database stack template

   "Type": "AWS::EC2::SecurityGroupIngress",
   "Properties": {
    "Description": "from ec2 instance to rds",
    "FromPort": 3306,
    "GroupId": {
     "Fn::GetAtt": [
      "DatabaseSecurityGroup5C91FDCB",
      "GroupId"
     ]
    },
    "IpProtocol": "tcp",
    "SourceSecurityGroupId": {
     "Fn::ImportValue": "InstanceStack:ExportsOutputFnGetAtttestInstanceSecurityGroup0C312FB5GroupId7D1BFC91"
    },
    "ToPort": 3306
   },

and this in your InstanceStack

   "Type": "AWS::EC2::SecurityGroup",
   "Properties": {
    "GroupDescription": "InstanceStack/test/InstanceSecurityGroup",
    "SecurityGroupEgress": [
     {
      "CidrIp": "0.0.0.0/0",
      "Description": "Allow all outbound traffic by default",
      "IpProtocol": "-1"
     }
    ],

Let me know if it works for you.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 21, 2024
@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 22, 2024
Copy link

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 github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label May 23, 2024
@github-actions github-actions bot added closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels May 28, 2024
@adam-nielsen
Copy link
Author

Hi @pahud,

Many thanks for your reply and apologies for the late response!

You example does work, but it requires the two stacks are in the same CDK project. In our case we have one CDK project for our shared infrastructure, and then six other stacks for each application we deploy to the shared infrastructure. So we're unable to pass the IDatabaseCluster instance between stacks as you have suggested, because the stacks are entirely independent.

If you take your example and put DatabaseStack in one CDK project (then deploy with npx cdk deploy), and cd into the other CDK stack for InstanceStack and do another npx cdk deploy then you will see the problem.

Any ideas how to get your example to work in this situation?

@aws-cdk-automation
Copy link
Collaborator

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

@aws aws locked as resolved and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-rds Related to Amazon Relational Database bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

3 participants