-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpersistence.ts
More file actions
114 lines (102 loc) · 3.89 KB
/
Copy pathpersistence.ts
File metadata and controls
114 lines (102 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2025-Present Datadog, Inc.
*/
import * as cdk from "aws-cdk-lib";
import { IVpc } from "aws-cdk-lib/aws-ec2";
import { IStringParameter, StringParameter } from "aws-cdk-lib/aws-ssm";
import { Construct } from "constructs";
import {
ClusterInstance,
DatabaseCluster,
DatabaseSecret,
} from "aws-cdk-lib/aws-rds";
export interface PersistenceProps {
env: string;
account: string;
vpc: IVpc;
}
export class Persistence extends Construct {
databaseEndpoint: IStringParameter;
constructor(scope: Construct, id: string, props: PersistenceProps) {
super(scope, id);
var databaseSecurityGroup = new cdk.aws_ec2.SecurityGroup(
this,
"DatabaseSecurityGroup",
{
vpc: props.vpc,
description: "Security group for Stickerlandia database",
allowAllOutbound: true,
}
);
databaseSecurityGroup.addIngressRule(
cdk.aws_ec2.Peer.ipv4(props.vpc.vpcCidrBlock),
cdk.aws_ec2.Port.tcp(5432),
"Allow Postgres access from within the VPC"
);
var secret = new DatabaseSecret(this, "SharedDBSecret", {
username: "postgres",
excludeCharacters: '"@/\\;=\'',
});
// Use DESTROY for dev environments, RETAIN for production
const removalPolicy = props.env === "dev"
? cdk.RemovalPolicy.DESTROY
: cdk.RemovalPolicy.RETAIN;
var cluster = new DatabaseCluster(this, "SharedDB", {
clusterIdentifier: `stickerlandia-${props.env}-db`,
engine: cdk.aws_rds.DatabaseClusterEngine.auroraPostgres({
version: cdk.aws_rds.AuroraPostgresEngineVersion.VER_17_4,
}),
vpc: props.vpc,
credentials: cdk.aws_rds.Credentials.fromSecret(secret),
serverlessV2MinCapacity: 1,
serverlessV2MaxCapacity: 1,
securityGroups: [databaseSecurityGroup],
removalPolicy: removalPolicy,
deletionProtection: props.env !== "dev",
defaultDatabaseName: "stickerlandia",
writer: cdk.aws_rds.ClusterInstance.serverlessV2(
"StickerlandiaWriterInstance"
),
readers: [
ClusterInstance.serverlessV2("StickerlandiaReaderInstance", {
scaleWithWriter: true,
}),
],
});
var databaseEndpointParam = new StringParameter(
this,
"DatabaseEndpointParam",
{
parameterName: `/stickerlandia/${props.env}/shared/database-endpoint`,
stringValue: cluster.clusterEndpoint.hostname,
description: `The database endpoint for the Stickerlandia ${props.env} environment`,
tier: cdk.aws_ssm.ParameterTier.STANDARD,
}
);
var databaseIdentifierParam = new StringParameter(
this,
"DatabaseIdentifierParam",
{
parameterName: `/stickerlandia/${props.env}/shared/database-identifier`,
stringValue: cluster.clusterIdentifier,
description: `The database ARN for the Stickerlandia ${props.env} environment`,
tier: cdk.aws_ssm.ParameterTier.STANDARD,
}
);
var databaseResourceIdentifierParam = new StringParameter(this, "DatabaseResourceIdentifierParam", {
parameterName: `/stickerlandia/${props.env}/shared/database-resource-identifier`,
stringValue: cluster.clusterResourceIdentifier,
description: `The database ARN for the Stickerlandia ${props.env} environment`,
tier: cdk.aws_ssm.ParameterTier.STANDARD,
});
// Export the secret ARN so microservices can fetch credentials
new StringParameter(this, "DatabaseSecretArnParam", {
parameterName: `/stickerlandia/${props.env}/shared/database-secret-arn`,
stringValue: secret.secretArn,
description: `The Secrets Manager ARN for the Stickerlandia ${props.env} database credentials`,
tier: cdk.aws_ssm.ParameterTier.STANDARD,
});
}
}