Skip to content

AWS CDK Create Aurora-V2-Serverless Postgres (lowest cost)

0

As of Jan 2025, confirming that ServerlessCluster is NOT what you use for "Aurora-V2-Serverless" (in my case Postgres). You must use DatabaseCluster (FYI only, DBCluster if written in CloudFormation).

Below is the CDK that was quite successful.

I used PostgreSQL v16.4 as it is the ONLY veesion that is supported by AWS for Aurora-Limitless, which tell me that this is the version that will be MOST support for the LONGEST time.

2 Answers
0

You are correct that ServerlessCluster is not used for Aurora Serverless v2. However, your statement about using PostgreSQL v16.4 for Aurora Unlimited (which I assume you mean Aurora Limitless Database) is not accurate for Aurora Serverless v2.

For creating an Aurora Serverless v2 PostgreSQL cluster using AWS CDK, you indeed use the DatabaseCluster construct. Here's an example of how you can create a low-cost Aurora Serverless v2 PostgreSQL cluster:

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

// Assume you have a VPC defined
const vpc = new ec2.Vpc(this, 'MyVPC');

const cluster = new rds.DatabaseCluster(this, 'AuroraServerlessV2Cluster', {
  engine: rds.DatabaseClusterEngine.auroraPostgres({
    version: rds.AuroraPostgresEngineVersion.VER_15_2, // Use a version that supports Serverless v2
  }),
  instanceProps: {
    instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MEDIUM),
    vpc: vpc,
  },
  serverlessV2MinCapacity: 0.5, // Minimum ACU (0.5 is the lowest for PostgreSQL)
  serverlessV2MaxCapacity: 1, // Maximum ACU (adjust based on your needs)
  vpc: vpc,
});

This configuration creates an Aurora Serverless v2 PostgreSQL cluster with the following cost-saving features:

  1. Uses Aurora PostgreSQL 15.2, which supports Serverless v2 (as of January 2024, version 16.4 is not yet available for Aurora Serverless v2).
  2. Sets the minimum capacity to 0.5 ACUs, which is the lowest possible for PostgreSQL.
  3. Sets the maximum capacity to 1 ACU, limiting scaling to control costs (adjust as needed).
  4. Uses a burstable instance type (t3.medium) which is generally more cost-effective for lower-traffic databases.

Remember to adjust the version and capacity settings based on your specific requirements and the latest available options. Also, ensure you're using the most recent CDK version to have access to the latest Aurora versions and features.

Sources
Supported Regions and Aurora DB engines for Aurora Serverless v2 - Amazon Aurora
aws-cdk-lib.aws_rds module · AWS CDK

answered 2 years ago

0

This is the lowest-cost I like. You can go for serverless_v2_min_capacity set to 0 (zero), for absolute minimum cost.

        ### Aurora Serverless v2 Cluster for PostgreSQL-v16
        self.db = aws_rds.DatabaseCluster( self,
            ### ??? NOT-Serverless
            ### https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_rds/DatabaseCluster.html
            id="AuroraV2-PGSQL",
            default_database_name = default_database_name,
            cluster_identifier = cluster_identifier,
            engine = aws_rds.DatabaseClusterEngine.aurora_postgres(version=aws_rds.AuroraPostgresEngineVersion.VER_16_4),
            enable_data_api = True, ### Default = False
            auto_minor_version_upgrade = False,
            writer=aws_rds.ClusterInstance.serverless_v2( id="AuroraWriter",
                instance_identifier = f"{cluster_identifier}-writer",
                #__ scale_with_writer =  .. .. applies to READERs only
                auto_minor_version_upgrade = False,
                allow_major_version_upgrade = False,
                publicly_accessible = False,
            ),
             #__ readers = [IClusterInstance] ### A list of instances to create as cluster-reader-instances. Default: - no readers are created. The cluster will have a single writer/reader
            serverless_v2_min_capacity = 1.0,  # DEFAULT is 0.5 (lowest possible)
            serverless_v2_max_capacity = serverless_aurorav2_max_capacity,
            # instances = [0-9]+, ### !!!!! LEGACY DEPRECATED parameter !!!  DEFAULT = 2 (Writer + 1-Reader)
            ### In V1-Aurora, we have to EXPLICITY provide the secret.  Not so for V2!
            ### In V2-Aurora, Default-CDK-construct's behavior is to create a NEW username of 'admin' (or 'postgres' for PostgreSQL) and SecretsManager-generated password
            # credentials=aws_rds.Credentials.from_secret(
            #     secret = self.rds_admin_dbo_secret,
            #     username = "admin_user"
            # ),
           # parameter_group=aws_rds.ParameterGroup.from_parameter_group_name(
            #     self,
            #     "ParameterGroup",
            #     f"default.aurora-postgresql{engine_ver_as_string}"
            # ),
            # network_type=aws_rds.NetworkType.IPV4,
            # enable_local_write_forwarding = True, ## Whether read-replicas can forward write-operations to the writer-nstance. Only be enabled for MySQL 3.04+ or PostgreSQL 16.4+

            backup=aws_rds.BackupProps(retention=Duration.days(30),
            copy_tags_to_snapshot = True,
            deletion_protection = True,
            removal_policy = RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE,
            vpc=vpc
            subnet_group=self.rds_subnet_group,
            security_groups=[self.rds_security_group],
            # storage_type = aws_rds.DBClusterStorageType.AURORA_IOPT1, ### required for LIMITLESS mode
            # storage_type = aws_rds.DBClusterStorageType.AURORA, ### Default: - DBClusterStorageType.AURORA_IOPT1
            # instance_props=aws_rds.InstanceProps( !!!!!!!!!!!!!!!!!!! LEGACY DEPRECATED parameter !!!!!!!!!!!!!!!!!!!!
            #     # parameter_group = aws_rds.ParameterGroup.from_parameter_group_name( self, "ParamGrp", rds_paramgroup_name),
            #     vpc=self.vpc, ### WARNING: RuntimeError: Provide either vpc or instanceProps.vpc, but not both
            #     vpc_subnets = vpc_subnets, ### WARNING: RuntimeError: Provide either vpc or instanceProps.vpc, but not both
            #     security_groups=[self.rds_security_group], ### WARNING: RuntimeError: Provide either vpc or instanceProps.vpc, but not both
            #     allow_major_version_upgrade = False,
            #     publicly_accessible = False,
            #     delete_automated_backups = False,
            #     instance_type=aws_ec2.InstanceType.of( !!!!!!! LEGACY DEPRECATED parameter !!!!!!!!
            #         aws_ec2.InstanceClass.R6GD,
            #         aws_ec2.InstanceSize.XLARGE4,
            #     ),
            # ),

            # cluster_scailability_type=aws_rds.ClusterScailabilityType.STANDARD,
            # enable_performance_insights=True, ### RuntimeError: Performance Insights must be enabled for Aurora Limitless Database.
            # performance_insight_retention=aws_rds.PerformanceInsightRetention.MONTHS_1,
            # enable_cluster_level_enhanced_monitoring=True,  ### RuntimeError: Cluster level enhanced monitoring must be set for Aurora Limitless Database. Please set 'monitoringInterval' and enable 'enableClusterLevelEnhancedMonitoring'.
            # monitoring_interval=Duration.seconds(60),       ### Max 1 minute. RuntimeError: Cluster level enhanced monitoring must be set for Aurora Limitless Database. Please set 'monitoringInterval' and enable 'enableClusterLevelEnhancedMonitoring'.
                                        ### Default: NO enhanced monitoring
            # monitoring_role -- Default: Role is Automatically created -- will be used to manage DB instances monitoring
            # cloudwatch_logs_exports = [str] ###  list of log-types that need to be enabled, for exporting to CW-Logs. Default: - --NO-- log exports
            # cloudwatch_logs_retention = RetentionDays() ### Default: - logs --NEVER-- expire.  # of days log-events are kept in CW-Logs. To remove the retention policy, set the value to Infinity.

            # backtrack_window = .. MySql only.
            # s3_export_buckets = .. MySql only.
            # s3_import_buckets = .. MySql only.
        )

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.