-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.ts
146 lines (130 loc) · 3.87 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { AwsProvider } from "@cdktf/provider-aws/lib/provider";
import { App, TerraformStack, RemoteBackend } from "cdktf";
import { Construct } from "constructs";
import { Frontend } from "./frontend";
import { Posts } from "./posts";
import { LocalProvider } from "@cdktf/provider-local/lib/provider";
interface EnvironmentOptions {
environment: "development" | "production";
user?: string;
}
const app = new App();
interface FrontendStackOptions extends EnvironmentOptions {
apiEndpoint: string;
}
export class FrontendStack extends TerraformStack {
constructor(
scope: Construct,
name: string,
public options: FrontendStackOptions
) {
super(scope, name);
new AwsProvider(this, "aws", { region: "eu-central-1" });
new LocalProvider(this, "local");
new Frontend(this, "frontend", {
environment: options.environment,
apiEndpoint: options.apiEndpoint,
});
}
}
export class PostsStack extends TerraformStack {
public posts: Posts;
constructor(
scope: Construct,
name: string,
public options: EnvironmentOptions
) {
super(scope, name);
new AwsProvider(this, "aws", { region: "eu-central-1" });
this.posts = new Posts(this, "posts", {
environment: options.environment,
userSuffix: options.user,
});
}
}
interface PreviewStackOptions {
previewBuildIdentifier: string;
}
class PreviewStack extends TerraformStack {
constructor(scope: Construct, name: string, options: PreviewStackOptions) {
super(scope, name);
// TODO: how to make sure that posts resources are applied before frontend is? (FE depends on API Endpoint of posts api)
const posts = new Posts(this, "posts", {
environment: options.previewBuildIdentifier,
});
new Frontend(this, "frontend", {
environment: options.previewBuildIdentifier,
apiEndpoint: posts.apiEndpoint,
});
}
}
const USE_REMOTE_BACKEND = process.env.USE_REMOTE_BACKEND === "true";
if (process.env.PREVIEW_BUILD_IDENTIFIER) {
if (
["development", "production"].includes(process.env.PREVIEW_BUILD_IDENTIFIER)
) {
throw new Error(
`environment variable PREVIEW_BUILD_IDENTIFIER may not be set to development or production but it was set to ${process.env.PREVIEW_BUILD_IDENTIFIER}`
);
}
// PR Preview - a single Terraform Cloud workspace will host the state for this
new PreviewStack(app, "preview", {
previewBuildIdentifier: process.env.PREVIEW_BUILD_IDENTIFIER,
});
} else {
// Use seperate stacks to minimize blast radius -> TODO: is this a smart idea at all?
// dev
const postsDev = new PostsStack(app, "posts-dev", {
environment: "development",
user: process.env.CDKTF_USER,
});
if (USE_REMOTE_BACKEND) {
new RemoteBackend(postsDev, {
organization: "cdktf-team",
workspaces: {
name: "cdktf-e2e-serverless-posts-dev",
},
});
}
const frontendDev = new FrontendStack(app, "frontend-dev", {
environment: "development",
apiEndpoint: postsDev.posts.apiEndpoint,
});
if (USE_REMOTE_BACKEND) {
new RemoteBackend(frontendDev, {
organization: "cdktf-team",
workspaces: {
name: "cdktf-e2e-serverless-frontend-dev",
},
});
}
// prod
const postsProd = new PostsStack(app, "posts-prod", {
environment: "production",
});
if (USE_REMOTE_BACKEND) {
new RemoteBackend(postsProd, {
organization: "cdktf-team",
workspaces: {
name: "cdktf-e2e-serverless-posts-prod",
},
});
}
const frontendProd = new FrontendStack(app, "frontend-prod", {
environment: "production",
apiEndpoint: postsProd.posts.apiEndpoint,
});
if (USE_REMOTE_BACKEND) {
new RemoteBackend(frontendProd, {
organization: "cdktf-team",
workspaces: {
name: "cdktf-e2e-serverless-frontend-prod",
},
});
}
}
app.synth();