-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
309 lines (279 loc) · 8.2 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import * as pulumi from "@pulumi/pulumi";
import * as awsx from "@pulumi/awsx";
import * as eks from "@pulumi/eks";
import * as k8s from "@pulumi/kubernetes";
import * as aws from "@pulumi/aws";
//import * as mysql from "mysql"; // Used for DB test if implemented
// Read the ownerTag from environment variables
const ownerTag = process.env.OWNER_TAG;
// Read the webAppImage from environment variables
const webAppImage = process.env.WEB_APP_IMAGE;
if (!ownerTag) {
throw new Error("OWNER_TAG environment variable is not set.");
}
if (!webAppImage) {
throw new Error("WEB_APP_IMAGE environment variable is not set.");
}
// Create a VPC with public and private subnets
const vpc = new awsx.ec2.Vpc(`${ownerTag}-vpc`, {
tags: {
"Owner": ownerTag
},
});
// Define the role mappings for the EKS cluster
// Opened up to all authenticated accounts for simplicity
// As to not worry about role-based authentication
// although that is best-practice.
const roleMappings: eks.RoleMapping[] = [{
roleArn: "*",
username: "*",
groups: ["system:masters"],
}];
// Create an Amazon EKS cluster with default AMI selection behavior
const cluster = new eks.Cluster(`${ownerTag}-eks-cluster`, {
vpcId: vpc.vpcId,
subnetIds: vpc.publicSubnetIds,
instanceType: "t3.medium",
desiredCapacity: 2,
minSize: 1,
maxSize: 2,
tags: {
"Owner": ownerTag
},
roleMappings: roleMappings,
});
const redisPort = 6397
const redisService = new k8s.core.v1.Service("redis-service", {
metadata: {
namespace: "default"
},
spec: {
type: "ClusterIP", // Only accessible inside the cluster. Redis doesnt need outside access.
ports: [{
port: redisPort,
targetPort: "redis", // Named port matches named port in deployment
}],
selector: {
app: "redis"
},
},
}, {
provider: cluster.provider,
customTimeouts: {
create: "30s"
}
});
const redisUrl = redisService.metadata.apply(metadata => `redis://${metadata.name}.${metadata.namespace}.svc.cluster.local:${redisPort}`)
// Create a Redis deployment and service
const redisDeployment = new k8s.apps.v1.Deployment("redis-deployment", {
metadata: {
namespace: "default"
},
spec: {
replicas: 1,
selector: {
matchLabels: {
app: "redis"
},
},
template: {
metadata: {
labels: {
app: "redis"
},
},
spec: {
containers: [{
name: "redis",
image: "redis",
ports: [{
name: "redis", // Named port matches named port in service
containerPort: 6379
}],
env: [{
name: "REDIS_URL",
value: redisUrl,
}, {
name: "REDIS_HOST",
value: "0.0.0.0"
}],
command: ["redis-server"],
args: ["--bind", "0.0.0.0"],
}],
},
},
},
}, {
provider: cluster.provider,
customTimeouts: {
create: "30s"
}
});
// Create a web application deployment and service
const webAppDeployment = new k8s.apps.v1.Deployment("webapp-deployment", {
metadata: {
namespace: "default"
},
spec: {
replicas: 1,
selector: {
matchLabels: {
app: "webapp"
},
},
template: {
metadata: {
labels: {
app: "webapp"
},
},
spec: {
containers: [{
name: "webapp",
image: `${webAppImage}`,
ports: [{
containerPort: 4567
}],
env: [{
name: "REDIS_URL",
value: redisUrl
}],
}],
},
},
},
}, {
provider: cluster.provider,
customTimeouts: {
create: "30s"
}
});
const webAppService = new k8s.core.v1.Service("webapp-service", {
metadata: {
namespace: "default"
},
spec: {
type: "LoadBalancer", // Accessible via AWS ELB URL on the internet
ports: [{
port: 80,
targetPort: 4567
}],
selector: {
app: "webapp"
},
},
}, {
provider: cluster.provider,
customTimeouts: {
create: "30s"
}
});
const cfg = new pulumi.Config();
// Create the RDS database instance using the createRDS function
export function createRDS() {
// Cant have uppercase letters in RDS and subnet names.
const rdsName = `${ownerTag!.toLowerCase()}-rds-instance`.toLowerCase().replace(/[^a-z0-9-]+/g, "");
// Create a subnet group for the RDS instance
const dbSubnetGroup = new aws.rds.SubnetGroup(`${ownerTag}-rds-subnet-group`, {
name: rdsName,
subnetIds: vpc.privateSubnetIds,
tags: {
"Owner": ownerTag!
},
});
// Get the CIDR blocks of the public subnets
const publicSubnetCidrBlocks = pulumi.output(vpc.publicSubnetIds).apply(subnetIds =>
subnetIds.map(subnetId =>
aws.ec2.getSubnet({
id: subnetId
}).then(subnet => subnet.cidrBlock ?? "")
)
);
// Create a security group for the RDS instance that allows inbound traffic from the public subnet
// And denies from everywhere else
const dbSecurityGroup = new aws.ec2.SecurityGroup(`${ownerTag}-db-security-group`, {
vpcId: vpc.vpcId,
ingress: [{ //Absence of any other rules means all inbound traffic is denied
protocol: "tcp",
fromPort: 3306,
toPort: 3306,
// Allow inbound traffic only from the public subnet CIDR block
cidrBlocks: publicSubnetCidrBlocks,
}],
tags: {
"Owner": ownerTag!
},
});
// TODO generate and store password as a secret for RDS
// const rdsPassword = new random.RandomPassword("rdsPassword", {
// length: 16, // Adjust the length as needed
// special: true, // Include special characters in the password
// });
// // Store the generated password in the Pulumi configuration
// cfg.requireSecret("rdspassword", rdsPassword.result);
// Create the RDS database instance
const rdsInstance = new aws.rds.Instance(`${ownerTag}-rds-instance`, {
allocatedStorage: 20,
engine: "mysql",
engineVersion: "8.0",
instanceClass: "db.t3.micro",
dbName: `${ownerTag}RDS`, // dbName can only be alpah numeric characters
identifier: rdsName,
username: "admin",
password: cfg.requireSecret("rdspassword"), // TODO generate and store this without human intervention
skipFinalSnapshot: true,
vpcSecurityGroupIds: [dbSecurityGroup.id],
dbSubnetGroupName: dbSubnetGroup.name, // Use the subnet group created above
tags: {
"Owner": ownerTag!
},
});
// Define the Lambda IAM role
const lambdaRole = new aws.iam.Role("lambdaRole", {
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: {
Service: "lambda.amazonaws.com",
},
}],
}),
});
// // TODO Lambda checks for db connection from internet and fails if it is
// new aws.iam.RolePolicyAttachment("lambdaRolePolicyAttachment", {
// role: lambdaRole,
// policyArn: aws.iam.ManagedPolicy.AWSLambdaBasicExecutionRole,
// });
// rdsInstance.endpoint.apply(endpoint => {
// rdsInstance.password.apply(password => {
// // Lambda function that attempts to connect to the RDS instance
// const lambdaFunction = new aws.lambda.CallbackFunction("checkDbConnection", {
// role: lambdaRole,
// callback: async () => {
// const connection = mysql.createConnection({
// host: endpoint,
// user: "admin",
// password: password,
// });
// connection.connect((err) => {
// if (err) {
// console.error("Error connecting to the database: ", err);
// return;
// }
// // If connection is successful, throw an error to fail the Pulumi deployment
// throw new Error("Database connection was successful, which is not expected.");
// });
// connection.end();
// },
// });
// });
// })
return rdsInstance;
}
// Create the RDS database instance using the createRDS function
const rdsInstance = createRDS();
// Export the Kubernetes cluster and the web application URL
export const kubeconfigOutput = pulumi.secret(cluster.kubeconfig);
export const webAppUrl = pulumi.interpolate `http://${webAppService.status.loadBalancer.ingress[0].hostname}`;