-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patheks-cdk-js-stack.ts
275 lines (261 loc) · 13.1 KB
/
eks-cdk-js-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
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
import * as fs from 'fs';
import * as path from 'path';
import * as cdk from 'aws-cdk-lib';
import { CfnParameter, CustomResource, Duration, CfnJson } from 'aws-cdk-lib';
import { Vpc, IVpc, InstanceType, Port, BlockDeviceVolume, EbsDeviceVolumeType, Instance, MachineImage, AmazonLinuxGeneration, SecurityGroup, Peer } from 'aws-cdk-lib/aws-ec2';
import { AwsAuth, Cluster, EndpointAccess, KubernetesVersion, KubernetesManifest, CfnAddon } from 'aws-cdk-lib/aws-eks';
import { PolicyStatement, Effect, Role, ManagedPolicy, ServicePrincipal, OpenIdConnectPrincipal } from 'aws-cdk-lib/aws-iam';
import { Key } from 'aws-cdk-lib/aws-kms';
import { Runtime, SingletonFunction, Code } from 'aws-cdk-lib/aws-lambda';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Provider } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import * as yaml from 'js-yaml';
import { eksVpc, addEndpoint } from '../lib/vpc-stack';
interface ekstackprops extends cdk.StackProps {
}
export class Ekstack extends cdk.Stack {
public readonly cluster: Cluster
public readonly awsauth: AwsAuth
constructor(scope: Construct, id: string, props: ekstackprops) {
super(scope, id, props);
// Clusters can only be upgraded and cannot be downgraded. Nodegroups are updated separately, refer to nodeAMIVersion parameter in README.md
// https://docs.aws.amazon.com/eks/latest/userguide/update-cluster.html
const k8sversion = new CfnParameter(this, 'k8sVersion', {
type: 'String',
description: 'K8s Version',
default: '1.21',
});
// https://github.com/aws/aws-cdk/issues/4159
// https://aws.github.io/aws-eks-best-practices/security/docs/detective/#enable-audit-logs
const eksLoggingOpts = new CfnParameter(this, 'eksLoggingOpts', {
type: 'CommaDelimitedList',
default: 'api,audit,authenticator,controllerManager,scheduler',
description: 'EKS Logging values,leave empty to disable, options https://docs.aws.amazon.com/eks/latest/APIReference/API_LogSetup.html#AmazonEKS-Type-LogSetup-types,',
});
const vpc = this.getOrCreateVpc(this);
// Locked Down Bastion Host Security Group to only allow outbound access to port 443.
const bastionHostLinuxSecurityGroup = new SecurityGroup(this, 'bastionHostSecurityGroup', {
allowAllOutbound: false,
securityGroupName: this.getOrCreateEksName(this) + '-bastionSecurityGroup',
vpc: vpc,
});
// Recommended to use connections to manage ingress/egress for security groups
bastionHostLinuxSecurityGroup.connections.allowTo(Peer.anyIpv4(), Port.tcp(443), 'Outbound to 443 only');
// Create Custom IAM Role and Policies for Bastion Host
// https://docs.aws.amazon.com/eks/latest/userguide/security_iam_id-based-policy-examples.html#policy_example3
const bastionHostPolicy = new ManagedPolicy(this, 'bastionHostManagedPolicy');
bastionHostPolicy.addStatements(new PolicyStatement({
resources: ['*'],
actions: [
'eks:DescribeNodegroup',
'eks:ListNodegroups',
'eks:DescribeCluster',
'eks:ListClusters',
'eks:AccessKubernetesApi',
'eks:ListUpdates',
'eks:ListFargateProfiles',
],
effect: Effect.ALLOW,
sid: 'EKSReadonly',
}));
const bastionHostRole = new Role(this, 'bastionHostRole', {
roleName: this.getOrCreateEksName(this) + '-bastion-host',
assumedBy: new ServicePrincipal('ec2.amazonaws.com'),
managedPolicies: [
// SSM Manager Permissions
ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore'),
// Read only EKS Permissions
bastionHostPolicy,
],
});
// Create Bastion Host, connect using Session Manager
const bastionHostLinux = new Instance(this, 'BastionEKSHost', {
// Defaults to private subnets https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.Instance.html#vpcsubnets
vpc: vpc,
instanceName: this.getOrCreateEksName(this) + '-EKSBastionHost',
instanceType: new InstanceType('t3.small'),
// Always use Latest Amazon Linux 2 instance, if new AMI is released will replace instance to keep it patched
// If replaced with specific AMI, ensure SSM Agent is installed and running
machineImage: MachineImage.latestAmazonLinux({
generation: AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
securityGroup: bastionHostLinuxSecurityGroup,
role: bastionHostRole,
// Ensure Bastion host EBS volume is encrypted
blockDevices: [{
deviceName: '/dev/xvda',
volume: BlockDeviceVolume.ebs(30, {
volumeType: EbsDeviceVolumeType.GP3,
encrypted: true,
}),
}],
});
// Need KMS Key for EKS Envelope Encryption, if deleted, KMS will wait default (30 days) time before removal.
const clusterKmsKey = new Key(this, 'ekskmskey', {
enableKeyRotation: true,
alias: cdk.Fn.join('', ['alias/', 'eks/', this.getOrCreateEksName(this)]),
});
this.cluster = new Cluster(this, 'EKSCluster', {
version: KubernetesVersion.of(k8sversion.valueAsString),
defaultCapacity: 0,
// https://aws.github.io/aws-eks-best-practices/security/docs/iam/#make-the-eks-cluster-endpoint-private
endpointAccess: EndpointAccess.PRIVATE,
vpc: vpc,
secretsEncryptionKey: clusterKmsKey,
mastersRole: bastionHostLinux.role,
clusterName: this.getOrCreateEksName(this),
// Ensure EKS helper lambadas are in private subnets
placeClusterHandlerInVpc: true,
});
// Allow BastionHost security group access to EKS Control Plane
bastionHostLinux.connections.allowTo(this.cluster, Port.tcp(443), 'Allow between BastionHost and EKS ');
// Install kubectl version similar to EKS k8s version
bastionHostLinux.userData.addCommands(
`VERSION=$(aws --region ${this.region} eks describe-cluster --name ${this.cluster.clusterName} --query 'cluster.version' --output text)`,
'echo \'K8s version is $VERSION\'',
'curl -LO https://dl.k8s.io/release/v$VERSION.0/bin/linux/amd64/kubectl',
'install -o root -g root -m 0755 kubectl /bin/kubectl',
);
this.awsauth = new AwsAuth(this, 'EKS_AWSAUTH', {
cluster: this.cluster,
});
// deploy Custom k8s RBAC group to provide EKS Web Console read only permissions https://docs.aws.amazon.com/eks/latest/userguide/add-user-role.html
// https://aws.github.io/aws-eks-best-practices/security/docs/iam.html#employ-least-privileged-access-when-creating-rolebindings-and-clusterrolebindings
const manifestConsoleViewGroup = yaml.loadAll(fs.readFileSync('manifests/consoleViewOnlyGroup.yaml', 'utf-8'));
const manifestConsoleViewGroupDeploy = new KubernetesManifest(this, 'eks-group-view-only', {
cluster: this.cluster,
manifest: manifestConsoleViewGroup,
});
this.awsauth.node.addDependency(manifestConsoleViewGroupDeploy);
this.awsauth.addMastersRole(bastionHostLinux.role, `${bastionHostLinux.role.roleArn}/{{SessionName}}`);
// Patch aws-node daemonset to use IRSA via EKS Addons, do before nodes are created
// https://aws.github.io/aws-eks-best-practices/security/docs/iam/#update-the-aws-node-daemonset-to-use-irsa
const awsNodeconditionsPolicy = new CfnJson(this, 'awsVpcCniconditionPolicy', {
value: {
[`${this.cluster.openIdConnectProvider.openIdConnectProviderIssuer}:aud`]: 'sts.amazonaws.com',
[`${this.cluster.openIdConnectProvider.openIdConnectProviderIssuer}:sub`]: 'system:serviceaccount:kube-system:aws-node',
},
});
const awsNodePrincipal = new OpenIdConnectPrincipal(this.cluster.openIdConnectProvider).withConditions({
StringEquals: awsNodeconditionsPolicy,
});
const awsVpcCniRole = new Role(this, 'awsVpcCniRole', {
assumedBy: awsNodePrincipal,
});
awsVpcCniRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonEKS_CNI_Policy'));
(() => new CfnAddon(this, 'vpc-cni', {
addonName: 'vpc-cni',
resolveConflicts: 'OVERWRITE',
serviceAccountRoleArn: awsVpcCniRole.roleArn,
clusterName: this.cluster.clusterName,
addonVersion: this.node.tryGetContext('eks-addon-vpc-cni-version'),
}))();
(() => new CfnAddon(this, 'kube-proxy', {
addonName: 'kube-proxy',
resolveConflicts: 'OVERWRITE',
clusterName: this.cluster.clusterName,
addonVersion: this.node.tryGetContext('eks-addon-kube-proxy-version'),
}))();
(() => new CfnAddon(this, 'core-dns', {
addonName: 'coredns',
resolveConflicts: 'OVERWRITE',
clusterName: this.cluster.clusterName,
addonVersion: this.node.tryGetContext('eks-addon-coredns-version'),
}))();
// Add existing IAM Role to Custom Group
// https://aws.github.io/aws-eks-best-practices/security/docs/iam/#use-iam-roles-when-multiple-users-need-identical-access-to-the-cluster
// this.awsauth.addRoleMapping(Role.fromRoleArn(this, 'Role_Admin', `arn:aws:iam::${this.account}:role/Admin`), {
// groups: ['eks-console-dashboard-full-access-group'],
// username: `arn:aws:iam::${this.account}:role/Admin/{{SessionName}}`,
// });
// Enable EKS Cluster Logging using Custom Resources
// https://github.com/aws/aws-cdk/issues/4159
// https://aws.github.io/aws-eks-best-practices/security/docs/detective/#enable-audit-logs
const eksLoggingCustomLambdaPolicy = new PolicyStatement(
{
resources: [
this.cluster.clusterArn,
`${this.cluster.clusterArn}/update-config`,
],
actions: [
'eks:UpdateClusterConfig',
'eks:DescribeCluster',
],
});
// Lambda function to update EKS Cluster with logging options see custom-resources/eksloggingOnEvent.py for code
const eksLoggingCustomLambda = new SingletonFunction(this, 'eksLoggingLambdaFunctiononEvent', {
uuid: this.stackName + 'eksLoggingLambdaFunctiononEvent',
functionName: this.stackName + 'eksLoggingLambdaFunctiononEvent',
runtime: Runtime.PYTHON_3_8,
code: Code.fromAsset(path.join(__dirname, 'custom-resources')),
handler: 'eksloggingOnEvent.on_event',
initialPolicy: [eksLoggingCustomLambdaPolicy],
timeout: Duration.seconds(300),
});
// Lambda function to check if EKS Cluster logging options are updated see custom-resources/eksloggingOnEvent.py for code
const eksLoggingCustomLambdaisComplete = new SingletonFunction(this, 'eksLoggingLambdaFunctionisComplete', {
uuid: this.stackName + 'eksLoggingLambdaFunctionisComplete',
functionName: this.stackName + 'eksLoggingLambdaFunctionisComplete',
runtime: Runtime.PYTHON_3_8,
code: Code.fromAsset(path.join(__dirname, 'custom-resources')),
handler: 'eksloggingOnEvent.is_complete',
initialPolicy: [eksLoggingCustomLambdaPolicy],
timeout: Duration.seconds(300),
});
// Custom Resource to update/check EKS logging using lambdas
const eksLoggingProvider = new Provider(this, 'eksLoggingProvider', {
onEventHandler: eksLoggingCustomLambda,
isCompleteHandler: eksLoggingCustomLambdaisComplete,
logRetention: RetentionDays.ONE_WEEK,
totalTimeout: Duration.minutes(10),
queryInterval: Duration.seconds(300),
});
// Invoke Customer Resource with logging options from parameters
(() => new CustomResource(this, 'eksLoggingCustomResource', {
serviceToken: eksLoggingProvider.serviceToken,
properties: {
eksCluster: this.getOrCreateEksName(this),
loggingOpts: eksLoggingOpts.valueAsList,
},
}))();
}
// Create nodegroup IAM role in same stack as eks cluster to ensure there is not a circular dependency
public createNodegroupRole(id: string): Role {
const role = new Role(this, id, {
assumedBy: new ServicePrincipal('ec2.amazonaws.com'),
});
role.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonEKSWorkerNodePolicy'));
role.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonEC2ContainerRegistryReadOnly'));
this.awsauth.addRoleMapping(role, {
username: 'system:node:{{EC2PrivateDNSName}}',
groups: [
'system:bootstrappers',
'system:nodes',
],
});
return role;
}
private getOrCreateVpc(scope: Construct): IVpc {
// use an existing vpc or create a new one using cdk context
const stack = cdk.Stack.of(scope);
// Able to choose default vpc but will error if EKS Cluster endpointAccess is set to be Private, need private subnets
if (stack.node.tryGetContext('use_default_vpc') === '1') {
return Vpc.fromLookup(stack, 'EKSNetworking', { isDefault: true });
}
if (stack.node.tryGetContext('use_vpc_id') !== undefined) {
return Vpc.fromLookup(stack, 'EKSNetworking', { vpcId: stack.node.tryGetContext('use_vpc_id') });
}
const vpc = new Vpc(stack, stack.stackName + '-EKSNetworking', eksVpc);
addEndpoint(stack, vpc);
return vpc;
}
private getOrCreateEksName(scope: Construct): string {
// use an existing vpc or create a new one using cdk context
const stack = cdk.Stack.of(scope);
if (stack.node.tryGetContext('cluster_name') !== undefined) {
return stack.node.tryGetContext('cluster_name');
}
return 'myekscluster';
}
}