/
efs-file-system.ts
339 lines (289 loc) · 10.2 KB
/
efs-file-system.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import * as ec2 from '@aws-cdk/aws-ec2';
import * as kms from '@aws-cdk/aws-kms';
import { FeatureFlags, ConcreteDependable, IDependable, IResource, RemovalPolicy, Resource, Size, Tags } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { AccessPoint, AccessPointOptions } from './access-point';
import { CfnFileSystem, CfnMountTarget } from './efs.generated';
/**
* EFS Lifecycle Policy, if a file is not accessed for given days, it will move to EFS Infrequent Access.
*
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-efs-filesystem.html#cfn-elasticfilesystem-filesystem-lifecyclepolicies
*/
export enum LifecyclePolicy {
/**
* After 7 days of not being accessed.
*/
AFTER_7_DAYS = 'AFTER_7_DAYS',
/**
* After 14 days of not being accessed.
*/
AFTER_14_DAYS = 'AFTER_14_DAYS',
/**
* After 30 days of not being accessed.
*/
AFTER_30_DAYS = 'AFTER_30_DAYS',
/**
* After 60 days of not being accessed.
*/
AFTER_60_DAYS = 'AFTER_60_DAYS',
/**
* After 90 days of not being accessed.
*/
AFTER_90_DAYS = 'AFTER_90_DAYS'
}
/**
* EFS Performance mode.
*
* @see https://docs.aws.amazon.com/efs/latest/ug/performance.html#performancemodes
*/
export enum PerformanceMode {
/**
* General Purpose is ideal for latency-sensitive use cases, like web serving
* environments, content management systems, home directories, and general file serving.
* Recommended for the majority of Amazon EFS file systems.
*/
GENERAL_PURPOSE = 'generalPurpose',
/**
* File systems in the Max I/O mode can scale to higher levels of aggregate
* throughput and operations per second. This scaling is done with a tradeoff
* of slightly higher latencies for file metadata operations.
* Highly parallelized applications and workloads, such as big data analysis,
* media processing, and genomics analysis, can benefit from this mode.
*/
MAX_IO = 'maxIO'
}
/**
* EFS Throughput mode.
*
* @see https://docs.aws.amazon.com/efs/latest/ug/performance.html#throughput-modes
*/
export enum ThroughputMode {
/**
* This mode on Amazon EFS scales as the size of the file system in the standard storage class grows.
*/
BURSTING = 'bursting',
/**
* This mode can instantly provision the throughput of the file system (in MiB/s) independent of the amount of data stored.
*/
PROVISIONED = 'provisioned'
}
/**
* Represents an Amazon EFS file system
*/
export interface IFileSystem extends ec2.IConnectable, IResource {
/**
* The ID of the file system, assigned by Amazon EFS.
*
* @attribute
*/
readonly fileSystemId: string;
/**
* Dependable that can be depended upon to ensure the mount targets of the filesystem are ready
*/
readonly mountTargetsAvailable: IDependable;
}
/**
* Properties of EFS FileSystem.
*/
export interface FileSystemProps {
/**
* VPC to launch the file system in.
*/
readonly vpc: ec2.IVpc;
/**
* Security Group to assign to this file system.
*
* @default - creates new security group which allows all outbound traffic
*/
readonly securityGroup?: ec2.ISecurityGroup;
/**
* Which subnets to place the mount target in the VPC.
*
* @default - the Vpc default strategy if not specified
*/
readonly vpcSubnets?: ec2.SubnetSelection;
/**
* Defines if the data at rest in the file system is encrypted or not.
*
* @default - If your application has the '@aws-cdk/aws-efs:defaultEncryptionAtRest' feature flag set, the default is true, otherwise, the default is false.
* @link https://docs.aws.amazon.com/cdk/latest/guide/featureflags.html
*/
readonly encrypted?: boolean;
/**
* The file system's name.
*
* @default - CDK generated name
*/
readonly fileSystemName?: string;
/**
* The KMS key used for encryption. This is required to encrypt the data at rest if @encrypted is set to true.
*
* @default - if 'encrypted' is true, the default key for EFS (/aws/elasticfilesystem) is used
*/
readonly kmsKey?: kms.IKey;
/**
* A policy used by EFS lifecycle management to transition files to the Infrequent Access (IA) storage class.
*
* @default - None. EFS will not transition files to the IA storage class.
*/
readonly lifecyclePolicy?: LifecyclePolicy;
/**
* The performance mode that the file system will operate under.
* An Amazon EFS file system's performance mode can't be changed after the file system has been created.
* Updating this property will replace the file system.
*
* @default PerformanceMode.GENERAL_PURPOSE
*/
readonly performanceMode?: PerformanceMode;
/**
* Enum to mention the throughput mode of the file system.
*
* @default ThroughputMode.BURSTING
*/
readonly throughputMode?: ThroughputMode;
/**
* Provisioned throughput for the file system.
* This is a required property if the throughput mode is set to PROVISIONED.
* Must be at least 1MiB/s.
*
* @default - none, errors out
*/
readonly provisionedThroughputPerSecond?: Size;
/**
* The removal policy to apply to the file system.
*
* @default RemovalPolicy.RETAIN
*/
readonly removalPolicy?: RemovalPolicy;
/**
* Whether to enable automatic backups for the file system.
*
* @default false
*/
readonly enableAutomaticBackups?: boolean;
}
/**
* Properties that describe an existing EFS file system.
*/
export interface FileSystemAttributes {
/**
* The security group of the file system
*/
readonly securityGroup: ec2.ISecurityGroup;
/**
* The File System's ID.
*/
readonly fileSystemId: string;
}
/**
* The Elastic File System implementation of IFileSystem.
* It creates a new, empty file system in Amazon Elastic File System (Amazon EFS).
* It also creates mount target (AWS::EFS::MountTarget) implicitly to mount the
* EFS file system on an Amazon Elastic Compute Cloud (Amazon EC2) instance or another resource.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-efs-filesystem.html
*
* @resource AWS::EFS::FileSystem
*/
export class FileSystem extends Resource implements IFileSystem {
/**
* The default port File System listens on.
*/
public static readonly DEFAULT_PORT: number = 2049;
/**
* Import an existing File System from the given properties.
*/
public static fromFileSystemAttributes(scope: Construct, id: string, attrs: FileSystemAttributes): IFileSystem {
return new ImportedFileSystem(scope, id, attrs);
}
/**
* The security groups/rules used to allow network connections to the file system.
*/
public readonly connections: ec2.Connections;
/**
* @attribute
*/
public readonly fileSystemId: string;
public readonly mountTargetsAvailable: IDependable;
private readonly _mountTargetsAvailable = new ConcreteDependable();
/**
* Constructor for creating a new EFS FileSystem.
*/
constructor(scope: Construct, id: string, props: FileSystemProps) {
super(scope, id);
if (props.throughputMode === ThroughputMode.PROVISIONED && props.provisionedThroughputPerSecond === undefined) {
throw new Error('Property provisionedThroughputPerSecond is required when throughputMode is PROVISIONED');
}
// we explictly use 'undefined' to represent 'false' to maintain backwards compatibility since
// its considered an actual change in CloudFormations eyes, even though they have the same meaning.
const encrypted = props.encrypted ?? (FeatureFlags.of(this).isEnabled(
cxapi.EFS_DEFAULT_ENCRYPTION_AT_REST) ? true : undefined);
const filesystem = new CfnFileSystem(this, 'Resource', {
encrypted: encrypted,
kmsKeyId: props.kmsKey?.keyArn,
lifecyclePolicies: (props.lifecyclePolicy ? [{ transitionToIa: props.lifecyclePolicy }] : undefined),
performanceMode: props.performanceMode,
throughputMode: props.throughputMode,
provisionedThroughputInMibps: props.provisionedThroughputPerSecond?.toMebibytes(),
backupPolicy: props.enableAutomaticBackups ? { status: 'ENABLED' } : undefined,
});
filesystem.applyRemovalPolicy(props.removalPolicy);
this.fileSystemId = filesystem.ref;
Tags.of(this).add('Name', props.fileSystemName || this.node.path);
const securityGroup = (props.securityGroup || new ec2.SecurityGroup(this, 'EfsSecurityGroup', {
vpc: props.vpc,
}));
this.connections = new ec2.Connections({
securityGroups: [securityGroup],
defaultPort: ec2.Port.tcp(FileSystem.DEFAULT_PORT),
});
const subnets = props.vpc.selectSubnets(props.vpcSubnets ?? { onePerAz: true });
// We now have to create the mount target for each of the mentioned subnet
let mountTargetCount = 0;
this.mountTargetsAvailable = [];
subnets.subnetIds.forEach((subnetId: string) => {
const mountTarget = new CfnMountTarget(this,
'EfsMountTarget' + (++mountTargetCount),
{
fileSystemId: this.fileSystemId,
securityGroups: Array.of(securityGroup.securityGroupId),
subnetId,
});
this._mountTargetsAvailable.add(mountTarget);
});
this.mountTargetsAvailable = this._mountTargetsAvailable;
}
/**
* create access point from this filesystem
*/
public addAccessPoint(id: string, accessPointOptions: AccessPointOptions = {}): AccessPoint {
return new AccessPoint(this, id, {
fileSystem: this,
...accessPointOptions,
});
}
}
class ImportedFileSystem extends Resource implements IFileSystem {
/**
* The security groups/rules used to allow network connections to the file system.
*/
public readonly connections: ec2.Connections;
/**
* @attribute
*/
public readonly fileSystemId: string;
/**
* Dependable that can be depended upon to ensure the mount targets of the filesystem are ready
*/
public readonly mountTargetsAvailable: IDependable;
constructor(scope: Construct, id: string, attrs: FileSystemAttributes) {
super(scope, id);
this.fileSystemId = attrs.fileSystemId;
this.connections = new ec2.Connections({
securityGroups: [attrs.securityGroup],
defaultPort: ec2.Port.tcp(FileSystem.DEFAULT_PORT),
});
this.mountTargetsAvailable = new ConcreteDependable();
}
}