| 1 | import * as AWS from "alchemy/AWS"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | |
| 4 | export const database = Effect.gen(function* () { |
| 5 | const vpc = yield* AWS.EC2.Vpc("Vpc", { cidrBlock: "10.62.0.0/16" }); |
| 6 | const subnetA = yield* AWS.EC2.Subnet("SubnetA", { |
| 7 | vpcId: vpc.vpcId, |
| 8 | cidrBlock: "10.62.0.0/24", |
| 9 | availabilityZone: "us-west-2a", |
| 10 | }); |
| 11 | const subnetB = yield* AWS.EC2.Subnet("SubnetB", { |
| 12 | vpcId: vpc.vpcId, |
| 13 | cidrBlock: "10.62.1.0/24", |
| 14 | availabilityZone: "us-west-2b", |
| 15 | }); |
| 16 | const lambdaSecurityGroup = yield* AWS.EC2.SecurityGroup( |
| 17 | "LambdaSecurityGroup", |
| 18 | { |
| 19 | vpcId: vpc.vpcId, |
| 20 | description: "Aurora example application; no inbound connections", |
| 21 | }, |
| 22 | ); |
| 23 | const dbSecurityGroup = yield* AWS.EC2.SecurityGroup( |
| 24 | "DatabaseSecurityGroup", |
| 25 | { |
| 26 | vpcId: vpc.vpcId, |
| 27 | description: "Postgres from the application security group only", |
| 28 | ingress: [ |
| 29 | { |
| 30 | ipProtocol: "tcp", |
| 31 | fromPort: 5432, |
| 32 | toPort: 5432, |
| 33 | referencedGroupId: lambdaSecurityGroup.groupId, |
| 34 | }, |
| 35 | ], |
| 36 | }, |
| 37 | ); |
| 38 | const subnetIds = [subnetA.subnetId, subnetB.subnetId]; |
| 39 | const aurora = yield* AWS.RDS.Aurora("Database", { |
| 40 | databaseName: "app", |
| 41 | engine: "aurora-postgresql", |
| 42 | engineVersion: "17.5", |
| 43 | subnetIds, |
| 44 | securityGroupIds: [dbSecurityGroup.groupId], |
| 45 | secret: { username: "dbadmin" }, |
| 46 | dataApi: true, |
| 47 | cluster: { |
| 48 | enableIAMDatabaseAuthentication: true, |
| 49 | serverlessV2ScalingConfiguration: { MinCapacity: 0.5, MaxCapacity: 1 }, |
| 50 | }, |
| 51 | instance: { dbInstanceClass: "db.serverless", publiclyAccessible: false }, |
| 52 | }); |
| 53 | return { ...aurora, vpc, subnetIds, lambdaSecurityGroup }; |
| 54 | }); |