aws-cdk-local icon indicating copy to clipboard operation
aws-cdk-local copied to clipboard

DatabaseCluster.clusterEndpoint.hostname does not respect LOCALSTACK_HOSTNAME

Open Garethp opened this issue 3 years ago • 1 comments

When deploying my RDS stack I have it put it's hostname into an SSM Parameter to be used by Lambda functions later. This doesn't seem to work when configuring LOCALSTACK_HOSTNAME, HOSTNAME and HOSTNAME_EXTERNAL.

Given the following docker-compose.yaml and cdk files

version: "3.5"
services:
  localstack:
    container_name: "localstack"
    image: localstack/localstack:latest
    network_mode: bridge
    ports:
      - "80:80"
      - "443:443"
      - "4566:4566"
      - "4510-4559:4510-4559"  # external services port range
      - "127.0.0.1:53:53"                # DNS config (only required for Pro)
      - "127.0.0.1:53:53/udp"            # DNS config (only required for Pro)
      - "4571:4571"
    environment:
      - SERVICES=sqs, lambda, apigateway, s3, sts, ssm, cloudformation, ecr, kms, iam, rds
      - LOCALSTACK_HOSTNAME=172.26.0.1
      - HOSTNAME=172.26.0.1
      - HOSTNAME_EXTERNAL=172.26.0.1
      - DOCKER_HOST=unix:///var/run/docker.sock
      - DEFAULT_REGION=eu-west-2
      - TEST_AWS_ACCOUNT_ID=000000000000
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
import { App, Stack } from "aws-cdk-lib";
import { Construct } from "constructs";
import { StringParameter } from "aws-cdk-lib/aws-ssm";
import {
  AuroraPostgresEngineVersion,
  DatabaseCluster,
  DatabaseClusterEngine,
} from "aws-cdk-lib/aws-rds";
import { Vpc } from "aws-cdk-lib/aws-ec2";

class TestStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    new DbConstruct(this, "consumer");
  }
}

class DbConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    const cluster = new DatabaseCluster(this, "DatabaseCluster", {
      engine: DatabaseClusterEngine.auroraPostgres({
        version: AuroraPostgresEngineVersion.VER_13_4,
      }),
      defaultDatabaseName: "database",
      storageEncrypted: true,
      instanceProps: {
        vpc: new Vpc(this, "db-vpc"),
      },
    });

    new StringParameter(this, `DatabaseEndpoint`, {
      parameterName: "databaseEndpoint",
      stringValue: cluster.clusterEndpoint.hostname,
    });
  }
}

const app = new App();

new TestStack(app, "stack");

I get the following results when querying by console

$ awslocal ssm get-parameter --name databaseEndpoint
{
    "Parameter": {
        "Name": "databaseEndpoint",
        "Type": "String",
        "Value": "localhost",
        "Version": 1,
        "LastModifiedDate": "2022-08-03T15:36:53.410000+01:00",
        "ARN": "arn:aws:ssm:eu-west-2:000000000000:parameter/databaseEndpoint",
        "DataType": "text"
    }
}

$ awslocal rds describe-db-clusters
{
    "DBClusters": [
        {
            "AllocatedStorage": 1,
            "DatabaseName": "database",
            "DBClusterIdentifier": "dbc-63393c9f",
            "DBClusterParameterGroup": "default.aurora-postgresql13",
            "DBSubnetGroup": "consumerDatabaseClusterSubnets68C9D7B3-baf5d77a",
            "Status": "error",
            "Endpoint": "172.26.0.1:4511",
            "MultiAZ": false,
            "Engine": "aurora-postgresql",
            "EngineVersion": "13.4",
            "Port": 4511,
            "MasterUsername": "postgres",
            "StorageEncrypted": false,
            "DBClusterArn": "arn:aws:rds:eu-west-2:000000000000:cluster:dbc-63393c9f",
            "IAMDatabaseAuthenticationEnabled": false,
            "CopyTagsToSnapshot": true
        }
    ]
}

Of note here is that the endpoint from describe-db-clusters is 172.26.0.1:4511 whereas the value that gets stored in ssm is localhost

Garethp avatar Aug 03 '22 14:08 Garethp

Hi! We just wanted to follow up to see whether your issue has been resolved. Were you able to get it working with the latest version of LocalStack? We would appreciate your feedback!

lakkeger avatar Nov 14 '23 07:11 lakkeger