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

Want to use the aurora serverless V2 engine on the aws via cdk. #2466

Open
vikramjeet32 opened this issue Jan 30, 2025 · 1 comment
Open
Labels
pending-response Issue is pending response from author pending-triage Incoming issues that need categorization question Question or confusion about some aspect of the product

Comments

@vikramjeet32
Copy link

Environment information

import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';

interface AuroraPostgresClusterStackProps extends cdk.StackProps {
  vpcId: string;
  securityGroupId: string;
  existingParameterGroupName: string;
  secretName: string;
}

export class MyAuroraProjectStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: AuroraPostgresClusterStackProps) {
    super(scope, id, props);

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

    const dbSecurityGroup = new ec2.SecurityGroup(this, 'DatabaseSecurityGroup', {
      vpc,
      description: 'Allow public access to Aurora cluster',
      allowAllOutbound: true
    });

    dbSecurityGroup.addIngressRule(
      ec2.Peer.anyIpv4(),
      ec2.Port.tcp(5432),
      'Allow public access to PostgreSQL'
    );

    const existingParameterGroup = rds.ParameterGroup.fromParameterGroupName(
      this,
      'ExistingParameterGroup',
      props.existingParameterGroupName
    );

    const queryEditorRole = new iam.Role(this, 'QueryEditorRole', {
      assumedBy: new iam.ServicePrincipal('rds.amazonaws.com'),
      description: 'IAM role for RDS Query Editor',
    });

    queryEditorRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonRDSDataFullAccess'));

    const cluster = new rds.DatabaseCluster(this, 'AuroraPostgresCluster', {
      engine: rds.DatabaseClusterEngine.auroraPostgres({ version: rds.AuroraPostgresEngineVersion.VER_13_11 }),
      // credentials: rds.Credentials.fromGeneratedSecret('clusteradmin'),
      credentials: rds.Credentials.fromGeneratedSecret('clusteradmin', {
        secretName: props.secretName, // Provide your desired name here
      }),
      instanceProps: {
        instanceType: ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
        vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
        vpc: vpc,
        securityGroups: [dbSecurityGroup],
        publiclyAccessible: true,
      },
      instances: 1, // Reduce to 1 instance to minimize costs
      serverlessV2MinCapacity: 0.5,
      serverlessV2MaxCapacity: 1,
      parameterGroup: existingParameterGroup,
      defaultDatabaseName: 'DatabaseNa',
      deletionProtection: false,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      enableDataApi: true,
    });

    cluster.grantDataApiAccess(queryEditorRole);

    new cdk.CfnOutput(this, 'ClusterEndpoint', {
      value: cluster.clusterEndpoint.socketAddress,
      description: 'Aurora Cluster Endpoint (Writer)',
    });

    new cdk.CfnOutput(this, 'ClusterReadEndpoint', {
      value: cluster.clusterReadEndpoint.socketAddress,
      description: 'Aurora Cluster Read Endpoint (Reader)',
    });

    new cdk.CfnOutput(this, 'DBCredentialsSecret', {
      value: cluster.secret?.secretName || 'No secret created',
      description: 'DB Credentials Secret Name',
    });

    new cdk.CfnOutput(this, 'ParameterGroupName', {
      value: props.existingParameterGroupName,
      description: 'Existing Parameter Group Name',
    });

    new cdk.CfnOutput(this, 'QueryEditorRoleArn', {
      value: queryEditorRole.roleArn,
      description: 'ARN of IAM Role for Query Editor',
    });
  }
}

Describe the bug

I'm using the postgress serverless database in sandbox and dev environment. We only have test users, with no accessive workload. However, my relational database charges have exponetially increased.

How can I able to correct this code for creation of the serverless database, because this code didn't worked as expected.

I want to change this to the serverless V2, with the
mini capacity: 0.5
max capacity: 1

Or I want sample template to create Serverless V2 postgres database via cdk. So, that I can pick option fron that which I need.

Reproduction steps

NA

@vikramjeet32 vikramjeet32 added the pending-triage Incoming issues that need categorization label Jan 30, 2025
@ykethan
Copy link
Member

ykethan commented Jan 30, 2025

Hey @vikramjeet32, thank you for reaching out. From the information provided, I believe this question may be better suited to be posted on the AWS CDK repository but i was able to find similar issues on the repository: aws/aws-cdk#20126

@ykethan ykethan added question Question or confusion about some aspect of the product pending-response Issue is pending response from author labels Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-response Issue is pending response from author pending-triage Incoming issues that need categorization question Question or confusion about some aspect of the product
Projects
None yet
Development

No branches or pull requests

2 participants