UNPKG

limgen

Version:

Infrastructure as Code generator

170 lines (169 loc) 7.13 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostgresRdsCluster = exports.defaultPostgresRdsClusterArgs = void 0; const pulumi = __importStar(require("@pulumi/pulumi")); const aws = __importStar(require("@pulumi/aws")); const random = __importStar(require("@pulumi/random")); const prefixed_1 = require("../utils/prefixed"); const deep_merge_1 = require("../utils/deep-merge"); ; exports.defaultPostgresRdsClusterArgs = { vpc: null, subnetIds: [], ingressType: "public", clusterConfig: { engine: aws.rds.EngineType.AuroraPostgresql, databaseName: "blog", masterUsername: "postgres", masterPassword: "", skipFinalSnapshot: false, }, databaseConfigs: [ { engine: aws.rds.EngineType.AuroraPostgresql, instanceClass: "db.t3.medium", publiclyAccessible: true, } ], }; class PostgresRdsCluster extends pulumi.ComponentResource { constructor(name = 'Database', args = exports.defaultPostgresRdsClusterArgs, opts = {}) { super("limgen:PostgresRdsClusterComponent", name, {}, opts); this._args = args; this.password = this.getPassword(); this.vpc = this.getVpc(); this.subnetGroup = this.getSubnetGroup(); this.securityGroups = this.getSecurityGroups(); this.dbCluster = this.getDbCluster(); this.databases = this.getDatabases(); this.connectionStringSecret = this.getConnectionStringSecret(); this.registerOutputs({ clusterIdentifier: this.dbCluster.clusterIdentifier, databaseEndpoint: this.databases[0].endpoint, databasePort: this.dbCluster.port, connectionStringSecretArn: this.connectionStringSecret.arn, securityGroups: this.securityGroups.map(sg => sg.id), }); } getDbCluster() { return new aws.rds.Cluster("DBCluster", (0, deep_merge_1.deepMerge)({ clusterIdentifier: (0, prefixed_1.prefixed)('cluster'), engine: aws.rds.EngineType.AuroraPostgresql, databaseName: 'postgres', masterUsername: "postgres", dbSubnetGroupName: this.subnetGroup.name, vpcSecurityGroupIds: this.securityGroups.map(sg => sg.id), skipFinalSnapshot: true, }, Object.assign(Object.assign({}, this._args.clusterConfig), { masterPassword: this.getPasswordValue() }))); } getDatabases() { if (this._args.databaseConfigs) { return this._args.databaseConfigs.map((config, index) => { return new aws.rds.ClusterInstance(`DBInstance-${index}`, (0, deep_merge_1.deepMerge)({ dbSubnetGroupName: this.subnetGroup.name, clusterIdentifier: this.dbCluster.id, publiclyAccessible: true, identifier: (0, prefixed_1.prefixed)(`db-instance-${index}`), }, Object.assign(Object.assign({}, config), { engine: aws.rds.EngineType.AuroraPostgresql }))); }); } return [ new aws.rds.ClusterInstance("DBInstance", { dbSubnetGroupName: this.subnetGroup.name, clusterIdentifier: this.dbCluster.id, engine: aws.rds.EngineType.AuroraPostgresql, publiclyAccessible: true, instanceClass: "db.t3.medium", identifier: (0, prefixed_1.prefixed)('db-instance'), }) ]; } getSecurityGroups() { var _a; return (_a = this._args.securityGroups) !== null && _a !== void 0 ? _a : [ new aws.ec2.SecurityGroup("DBIngressSecurityGroup", { vpcId: this.vpc.vpcId, ingress: [ { protocol: "tcp", fromPort: 5432, toPort: 5432, cidrBlocks: ["0.0.0.0/0"], } ], egress: [ { protocol: "tcp", fromPort: 0, toPort: 65535, cidrBlocks: ["0.0.0.0/0"], } ], }) ]; } getSubnetGroup() { var _a; return new aws.rds.SubnetGroup("PublicSubnetGroup", { name: (0, prefixed_1.prefixed)('public-subnet-group'), subnetIds: (_a = this._args.subnetIds) !== null && _a !== void 0 ? _a : this.vpc.publicSubnetIds, }); } getVpc() { if (!this._args.vpc) { throw new Error("VPC is required for RDS cluster"); } return this._args.vpc; } getPassword() { var _a, _b; if ((_a = this._args.clusterConfig) === null || _a === void 0 ? void 0 : _a.masterPassword) { return (_b = this._args.clusterConfig) === null || _b === void 0 ? void 0 : _b.masterPassword; } return new random.RandomPassword("DBPassword", { length: 24, special: false, }); } getConnectionStringSecret() { const connectionString = pulumi.interpolate `postgresql://${this.dbCluster.masterUsername}:${this.getPasswordValue()}@${this.dbCluster.endpoint}:${this.dbCluster.port}/${this.dbCluster.databaseName}`; const stack = pulumi.getStack(); this.connectionStringSecret = new aws.secretsmanager.Secret("ConnectionString", { namePrefix: (0, prefixed_1.prefixed)("connection-string"), }); new aws.secretsmanager.SecretVersion("ConnectionStringVersion", { secretId: this.connectionStringSecret.id, secretString: connectionString, versionStages: ["AWSCURRENT"], }); return this.connectionStringSecret; } getPasswordValue() { var _a; return ((_a = this.password) === null || _a === void 0 ? void 0 : _a.apply) ? this.password : this.password.result; } } exports.PostgresRdsCluster = PostgresRdsCluster;