constructor()

in cdk/lib/constructs/database.ts [114:204]


	constructor(scope: GuStack, id: string, props: GuDatabaseProps) {
		const {
			app,
			allowExternalConnection = false,
			devxBackups = false,
			caCertificateIdentifier = 'rds-ca-rsa2048-g1',
			vpc = GuVpc.fromIdParameter(scope, 'primary-vpc'),
			vpcSubnets = {
				subnets: GuVpc.subnetsFromParameter(scope, {
					type: SubnetType.PRIVATE,
					app,
				}),
			},
			port = 5432,
			engine = DatabaseInstanceEngine.POSTGRES,
		} = props;

		const defaultSecurityGroup = new GuSecurityGroup(
			scope,
			'DefaultSecurityGroup',
			{
				vpc,
				app,
			},
		);

		const defaults: DatabaseInstanceProps = {
			vpc,
			vpcSubnets,
			engine,
			port,
			storageEncrypted: true,
			deletionProtection: true,
			removalPolicy: RemovalPolicy.SNAPSHOT,
			publiclyAccessible: false,
			iamAuthentication: true,
			multiAz: true,
			securityGroups: [defaultSecurityGroup],
		};

		super(scope, id, { ...defaults, ...props });

		this.instanceResourceId = this.cfnResource.attrDbiResourceId;
		this.accessSecurityGroup = defaultSecurityGroup;

		this.cfnResource.caCertificateIdentifier = caCertificateIdentifier;

		this.connections.allowFrom(defaultSecurityGroup, Port.tcp(port));

		this.cfnResource.tags.setTag('devx-backup-enabled', String(devxBackups));

		if (allowExternalConnection) {
			const { stack, stage } = scope;

			new StringParameter(this, 'AccessSecurityGroupParam', {
				parameterName: `/${stage}/${stack}/${app}/database/access-security-group`,
				simpleName: false,
				stringValue: defaultSecurityGroup.securityGroupId,
				tier: ParameterTier.STANDARD,
				dataType: ParameterDataType.TEXT,
			});
			new StringParameter(this, 'EndpointAddressParam', {
				parameterName: `/${stage}/${stack}/${app}/database/endpoint-address`,
				simpleName: false,
				stringValue: this.dbInstanceEndpointAddress,
				tier: ParameterTier.STANDARD,
				dataType: ParameterDataType.TEXT,
			});
			new StringParameter(this, 'UsernameParam', {
				parameterName: `/${stage}/${stack}/${app}/database/username`,
				simpleName: false,
				stringValue: props.credentials?.username ?? 'postgres',
				tier: ParameterTier.STANDARD,
				dataType: ParameterDataType.TEXT,
			});
			new StringParameter(this, 'PortParam', {
				parameterName: `/${stage}/${stack}/${app}/database/port`,
				simpleName: false,
				stringValue: this.dbInstanceEndpointPort,
				tier: ParameterTier.STANDARD,
				dataType: ParameterDataType.TEXT,
			});
			new StringParameter(this, 'DatabaseNameParam', {
				parameterName: `/${stage}/${stack}/${app}/database/database-name`,
				simpleName: false,
				stringValue: props.databaseName ?? 'postgres',
				tier: ParameterTier.STANDARD,
				dataType: ParameterDataType.TEXT,
			});
		}
	}