1- using System . Collections . Immutable ;
2- using System . Linq ;
31using Pulumi ;
2+ using Pulumi . Aws . Kms ;
43using Pulumi . Aws . Rds ;
4+ using Pulumi . Aws . Rds . Inputs ;
55using Pulumi . Random ;
66using VirtualFinland . UsersAPI . Deployment . Common . Models ;
77using Instance = Pulumi . Aws . Rds . Instance ;
@@ -15,7 +15,96 @@ public class PostgresDatabase
1515{
1616 public PostgresDatabase ( Config config , StackSetup stackSetup , VpcSetup vpcSetup )
1717 {
18- var dbSubNetGroup = new Pulumi . Aws . Rds . SubnetGroup ( $ "{ stackSetup . ProjectName } -dbsubnets-{ stackSetup . Environment } ", new ( )
18+ if ( stackSetup . IsProductionEnvironment )
19+ {
20+ SetupProductionPostgresDatabase ( config , stackSetup , vpcSetup ) ;
21+ }
22+ else
23+ {
24+ SetupDevelopmentPostgresDatabase ( config , stackSetup , vpcSetup ) ;
25+ }
26+
27+ if ( config . GetBoolean ( "useRdsProxy" ) == true )
28+ {
29+ var rdsProxy = new RDSProxy ( config , stackSetup , this , vpcSetup ) ;
30+ DatabaseConnectionString = rdsProxy . DatabaseConnectionString ; // Override the connection string with one from the proxy
31+ }
32+ }
33+
34+ /// <summary>
35+ /// Setup AWS Aurora RDS Serverless V2 for postgresql
36+ /// </summary>
37+ public void SetupProductionPostgresDatabase ( Config config , StackSetup stackSetup , VpcSetup vpcSetup )
38+ {
39+ var dbSubNetGroup = new SubnetGroup ( stackSetup . CreateResourceName ( "database-subnets" ) , new ( )
40+ {
41+ SubnetIds = vpcSetup . PrivateSubnetIds ,
42+ } ) ;
43+
44+ var password = new RandomPassword ( stackSetup . CreateResourceName ( "database-password" ) , new ( )
45+ {
46+ Length = 16 ,
47+ Special = false ,
48+ OverrideSpecial = "_%@" ,
49+ } ) ;
50+
51+ // Encryption key (KMS)
52+ var encryptionKey = new Key ( stackSetup . CreateResourceName ( "database-encryption-key" ) , new ( )
53+ {
54+ Description = "Encryption key for the database" ,
55+ Tags = stackSetup . Tags ,
56+ DeletionWindowInDays = 90 , // On deletion, the key will be retained for 30 days before being deleted permanently
57+ } ) ;
58+
59+ // AWS Aurora RDS Serverless V2 for postgresql
60+ var auroraCluster = new Cluster ( stackSetup . CreateResourceName ( "database-cluster" ) , new ClusterArgs ( )
61+ {
62+ Engine = "aurora-postgresql" ,
63+ EngineVersion = "13.6" ,
64+ EngineMode = "provisioned" , // serverless v2
65+ Serverlessv2ScalingConfiguration = new ClusterServerlessv2ScalingConfigurationArgs
66+ {
67+ MaxCapacity = 4 ,
68+ MinCapacity = 0.5 ,
69+ } ,
70+
71+ DatabaseName = config . Require ( "dbName" ) ,
72+ MasterUsername = config . Require ( "dbAdmin" ) ,
73+ MasterPassword = password . Result ,
74+
75+ SkipFinalSnapshot = false ,
76+ DeletionProtection = true ,
77+ StorageEncrypted = true ,
78+ KmsKeyId = encryptionKey . Arn ,
79+
80+ DbSubnetGroupName = dbSubNetGroup . Name ,
81+ Tags = stackSetup . Tags ,
82+ } ) ;
83+
84+ new ClusterInstance ( stackSetup . CreateResourceName ( "database-instance" ) , new ( )
85+ {
86+ ClusterIdentifier = auroraCluster . ClusterIdentifier ,
87+ InstanceClass = "db.serverless" ,
88+ Engine = "aurora-postgresql" ,
89+ EngineVersion = auroraCluster . EngineVersion ,
90+ Tags = stackSetup . Tags ,
91+ } ) ;
92+
93+ var DbName = config . Require ( "dbName" ) ;
94+ var DbUsername = config . Require ( "dbAdmin" ) ;
95+ var DbHostName = auroraCluster . Endpoint ;
96+ var DbPassword = password . Result ;
97+
98+ DatabaseConnectionString = Output . Format ( $ "Host={ DbHostName } ;Database={ DbName } ;Username={ DbUsername } ;Password={ DbPassword } ") ;
99+ DBIdentifier = auroraCluster . ClusterIdentifier ;
100+ }
101+
102+ /// <summary>
103+ /// Setup AWS RDS for postgresql
104+ /// </summary>
105+ public void SetupDevelopmentPostgresDatabase ( Config config , StackSetup stackSetup , VpcSetup vpcSetup )
106+ {
107+ var dbSubNetGroup = new SubnetGroup ( $ "{ stackSetup . ProjectName } -dbsubnets-{ stackSetup . Environment } ", new ( )
19108 {
20109 SubnetIds = vpcSetup . PrivateSubnetIds ,
21110 } ) ;
@@ -27,7 +116,7 @@ public PostgresDatabase(Config config, StackSetup stackSetup, VpcSetup vpcSetup)
27116 OverrideSpecial = "_%@" ,
28117 } ) ;
29118
30- var rdsPostGreInstance = new Instance ( $ " { stackSetup . ProjectName } - postgres-db- { stackSetup . Environment } " , new InstanceArgs ( )
119+ var rdsPostGreInstance = new Instance ( stackSetup . CreateResourceName ( " postgres-db" ) , new InstanceArgs ( )
31120 {
32121 Engine = "postgres" ,
33122 InstanceClass = "db.t3.micro" ,
@@ -46,11 +135,11 @@ public PostgresDatabase(Config config, StackSetup stackSetup, VpcSetup vpcSetup)
46135 var DbName = config . Require ( "dbName" ) ;
47136 var DbUsername = config . Require ( "dbAdmin" ) ;
48137 var DbHostName = rdsPostGreInstance . Endpoint ;
49- DBIdentifier = rdsPostGreInstance . Identifier ;
50138 var DbPassword = password . Result ;
51- DbConnectionString = Output . Format ( $ "Host={ DbHostName } ;Database={ DbName } ;Username={ DbUsername } ;Password={ DbPassword } ") ;
139+ DatabaseConnectionString = Output . Format ( $ "Host={ DbHostName } ;Database={ DbName } ;Username={ DbUsername } ;Password={ DbPassword } ") ;
140+ DBIdentifier = rdsPostGreInstance . Identifier ;
52141 }
53142
54- public Output < string > DbConnectionString = default ! ;
55143 public Output < string > DBIdentifier = default ! ;
144+ public Output < string > DatabaseConnectionString = default ! ;
56145}
0 commit comments