DEV Community

Moya Richards for AWS Community Builders

Posted on • Updated on

The dreaded AWS error : Cannot find version xx.x for postgres

When creating an AWS stack containing a postgres database table, you might received this error:

Error

Cannot find version 12.3 for postgres (Service: AmazonRDS; Status Code: 400; Error Code: InvalidParameterCombination


Cannot find upgrade path from 14.2 to 12.3. (Service: AmazonRDS; Status Code: 400; Error Code: InvalidParameterCombination

Erroneous code

const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_12_3 });
Enter fullscreen mode Exit fullscreen mode

Now this error code : InvalidParameterCombination, and the the message: Cannot find version 12.3 for postgres is not very explicit and doesn't explain what the real issue is.

What the error really means is that version 12.3 for postgres is depreciated and any reference to that version, or any version you are having an issue with, should be removed from your code and replaced with a supported version.

The real issue

AWS requires that you specify the version number for the database when it is being created. However, AWS doesn't keep all versions of that database around indefinitely.

AWS depreciates certain versions, so at some point in the future you will have to change the code to use a supported version of that database.


How to fix the error?

Specify a supported version of the database

Fixed code: version 1

| Simple code|

const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_14_2 });
Enter fullscreen mode Exit fullscreen mode

Fixed code: version 2

Use version 2 of the fix, if you don't like to hardcore values in your code that are expected to change.

| Superior code fix|


/**
* Store values in environment variables
* @see https://www.npmjs.com/package/dotenv
*/
const postgresFullVersion = process.env.POSTGRESFULLVERSION ?? "14.2";
const postgresMajorVersion = process.env.POSTGRESMAJORVERSION ?? "14";

/**
* Generate the postgres engine version from the values in the environment variable
*/

const engine = DatabaseInstanceEngine.postgres({
    version: PostgresEngineVersion.of(
      postgresFullVersion,
      postgresMajorVersion,
    ),
  });
Enter fullscreen mode Exit fullscreen mode

How to know which versions of the database is Depreciated and supported

  • Visit the AWS website

https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html

Depreciated versions of postgres database
Image: Depreciated 12.x versions of postgres

After running the command, scroll all the way to the end of the list to see the most recent version of the database supported by AWS

aws rds describe-db-engine-versions --engine postgres --output table 
Enter fullscreen mode Exit fullscreen mode
AWS CLI view of current version of postgres
Image: AWS CLI view of current version of postgres

AWS stack creation code (contains error)

import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Vpc, InstanceType, InstanceSize, InstanceClass } from 'aws-cdk-lib/aws-ec2';
import { DatabaseInstance, DatabaseInstanceEngine, PostgresEngineVersion } from 'aws-cdk-lib/aws-rds';


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

    const vpc = new Vpc(this, 'TestVpc', {
      cidr: '10.192.0.0/16',
      maxAzs: 2,
      natGateways: 1,
      enableDnsHostnames: true,
      enableDnsSupport: true
    });

    //Erroneous Line 
    //--------------------------
    const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_12_3 });
    //--------------------------

    const dbInstance = new DatabaseInstance(this, 'Test_Postgres_CDK', {
      engine,
      vpc,
      iamAuthentication: true,
      instanceType: InstanceType.of(
        InstanceClass.BURSTABLE3,
        InstanceSize.MICRO
      ),
      databaseName: 'test_postgres'
    });

    new CfnOutput(this, 'dbEndpoint', {
      value: dbInstance.instanceEndpoint.hostname,
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

AWS stack creation code (fixed code)

import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Vpc, InstanceType, InstanceSize, InstanceClass } from 'aws-cdk-lib/aws-ec2';
import { DatabaseInstance, DatabaseInstanceEngine, PostgresEngineVersion } from 'aws-cdk-lib/aws-rds';


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

    const vpc = new Vpc(this, 'TestVpc', {
      cidr: '10.192.0.0/16',
      maxAzs: 2,
      natGateways: 1,
      enableDnsHostnames: true,
      enableDnsSupport: true
    });

    //--- Erroneous Line 
    //const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_12_3 });


    //simple fix : version 1
    //const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_14_2 });

    //--------------------------
    //--- start : superior code fix , version 2
    const postgresFullVersion = process.env.POSTGRESMAJORVERSION ?? "14.2";
    const postgresMajorVersion = process.env.POSTGRESENGINEVERSION ?? "14";


    const engine = DatabaseInstanceEngine.postgres({
        version: PostgresEngineVersion.of(
          postgresFullVersion,
          postgresMajorVersion,
        ),
      });
    //--- end : superior code fix
    //--------------------------
    const dbInstance = new DatabaseInstance(this, 'Test_Postgres_CDK', {
      engine,
      allowMajorVersionUpgrade: true,
      vpc,
      iamAuthentication: true,
      instanceType: InstanceType.of(
        InstanceClass.BURSTABLE3,
        InstanceSize.MICRO
      ),
      databaseName: 'test_postgres'
    });

    new CfnOutput(this, 'dbEndpoint', {
      value: dbInstance.instanceEndpoint.hostname,
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
moyarich profile image
Moya Richards • Edited

I am using typescript and serverless stack in another project.

This is a sample of the subtle changes I made to the code

code changes