-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcluster-stack.ts
69 lines (55 loc) · 1.87 KB
/
cluster-stack.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
import * as cdk from '@aws-cdk/core';
import * as iam from '@aws-cdk/aws-iam';
import * as eks from '@aws-cdk/aws-eks';
import * as ec2 from '@aws-cdk/aws-ec2';
import { PhysicalName } from '@aws-cdk/core';
export class ClusterStack extends cdk.Stack {
public readonly cluster: eks.Cluster;
public readonly firstRegionRole: iam.Role;
public readonly secondRegionRole: iam.Role;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const primaryRegion = 'ap-northeast-1';
const clusterAdmin = new iam.Role(this, 'AdminRole', {
assumedBy: new iam.AccountRootPrincipal()
});
const cluster = new eks.Cluster(this, 'demogo-cluster', {
clusterName: 'demo',
version: eks.KubernetesVersion.V1_21,
mastersRole: clusterAdmin,
defaultCapacity: 2
});
cluster.addNodegroupCapacity('spot-ng', {
instanceTypes: [
new ec2.InstanceType('m5.large'),
new ec2.InstanceType('m5a.large')
],
minSize: 2,
capacityType: eks.CapacityType.SPOT
})
this.cluster = cluster;
if (cdk.Stack.of(this).region==primaryRegion) {
this.firstRegionRole = createDeployRole(this, `for-1st-region`, cluster);
}
else {
this.secondRegionRole = createDeployRole(this, `for-2nd-region`, cluster);
}
}
}
function createDeployRole(scope: cdk.Construct, id: string, cluster: eks.Cluster): iam.Role {
const role = new iam.Role(scope, id, {
roleName: PhysicalName.GENERATE_IF_NEEDED,
assumedBy: new iam.AccountRootPrincipal()
});
cluster.awsAuth.addMastersRole(role);
return role;
}
export interface EksProps extends cdk.StackProps {
cluster: eks.Cluster
}
export interface CicdProps extends cdk.StackProps {
firstRegionCluster: eks.Cluster,
secondRegionCluster: eks.Cluster,
firstRegionRole: iam.Role,
secondRegionRole: iam.Role
}