-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
94 lines (75 loc) · 2.58 KB
/
main.ts
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
import { Construct } from "constructs";
import { App, TerraformStack, TerraformVariable } from "cdktf";
import express from "express";
import short from "short-uuid";
import fs from 'fs';
import archiver from 'archiver';
import { S3Bucket } from "@cdktf/provider-aws/lib/s3-bucket";
import { AwsProvider } from "@cdktf/provider-aws/lib/provider";
const PORT = process.env.PORT || 3000;
const server = express();
server.use(express.json());
export interface StackRequest {
name: string;
buckets: string[];
region: string;
}
class MyStack extends TerraformStack {
constructor(scope: Construct, id: string, req: StackRequest) {
super(scope, id);
const region = new TerraformVariable(this, "region", {
type: "string",
description: "The AWS region to deploy the stack to",
});
// configure the aws provider
new AwsProvider(this, "aws", {
region: region.stringValue,
});
req.buckets.forEach((bucketName) => {
// deploy an aws s3 bucket
new S3Bucket(this, bucketName, {
bucket: bucketName,
});
});
// define resources here
console.log("deploying buckets", req.buckets);
}
}
function zipDirectory(sourceDir: string, outPath: string): Promise<string> {
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(outPath);
const archive = archiver('zip', { zlib: { level: 9 } });
output.on('close', () => resolve(outPath));
archive.on('error', (err) => reject(err));
archive.pipe(output);
archive.directory(sourceDir, false);
archive.finalize();
});
}
server.post("/generate", async (req, res) => {
const deploymentSpec = req.body; // Assuming body structure is validated
console.log('Deploying', deploymentSpec);
const uniqueId = short.generate();
const outputDir = `./${uniqueId}`;
// Generate Terraform configuration
const app = new App({
outdir: outputDir,
});
new MyStack(app, deploymentSpec.name, deploymentSpec);
app.synth();
// Zip the generated Terraform configuration
const zipPath = await zipDirectory(`${outputDir}/stacks/${deploymentSpec.name}`, `${outputDir}.zip`);
// Send the zip file as a response
res.download(zipPath, async (err) => {
if (err) {
console.error('Error sending the zip file:', err);
res.status(500).send('Error generating Terraform configuration');
}
// cleanup generated output and zip file
await fs.promises.rmdir(outputDir, { recursive: true });
await fs.promises.rm(zipPath);
});
});
server.listen(PORT, () => {
console.log("Server is running on port", PORT);
});