Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
569 changes: 525 additions & 44 deletions API.md

Large diffs are not rendered by default.

30 changes: 18 additions & 12 deletions src/aws-elasticache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ declare const vpc: ec2.Vpc;
declare const userGroup: UserGroup;

const serverlessCache = new ServerlessCache(this, 'ServerlessCache', {
engine: Engine.VALKEY,
majorEngineVersion: MajorVersion.VER_8,
serverlessCacheEngine: ServerlessCacheEngine.valkey({
engineVersion: ValkeyEngineVersion.VER_8,
}),
serverlessCacheName: 'my-serverless-cache',
vpc,
// assign User Group
Expand Down Expand Up @@ -228,9 +229,10 @@ Setup required properties and create:
declare const vpc: ec2.Vpc;

const serverlessCache = new ServerlessCache(this, 'ServerlessCache', {
engine: Engine.VALKEY,
serverlessCacheEngine: ServerlessCacheEngine.valkey({
engineVersion: ValkeyEngineVersion.VER_8,
}),
vpc,
majorEngineVersion: MajorVersion.VER_8,
});
```

Expand Down Expand Up @@ -297,13 +299,14 @@ To enable automatic backups, set the `snapshotRetentionLimit` property. You can
declare const vpc: ec2.Vpc;

const serverlessCache = new ServerlessCache(this, 'ServerlessCache', {
engine: Engine.VALKEY,
serverlessCacheEngine: ServerlessCacheEngine.valkey({
engineVersion: ValkeyEngineVersion.VER_8,
}),
// enable automatic backups and set the retention period to 6 days
snapshotRetentionLimit: 6,
// set the backup window to 12:00 AM UTC
dailySnapshotTime: new DailySnapshotTime({ hour: 12, minute: 0 }),
vpc,
majorEngineVersion: MajorVersion.VER_8,
});
```

Expand All @@ -313,11 +316,12 @@ You can create a final backup by setting `finalSnapshotName` property.
declare const vpc: ec2.Vpc;

const serverlessCache = new ServerlessCache(this, 'ServerlessCache', {
engine: Engine.VALKEY,
serverlessCacheEngine: ServerlessCacheEngine.valkey({
engineVersion: ValkeyEngineVersion.VER_8,
}),
// set the final snapshot name
finalSnapshotName: 'my-finalsnapshot',
vpc,
majorEngineVersion: MajorVersion.VER_8,
});
```

Expand All @@ -327,11 +331,12 @@ You can restore from snapshots by setting snapshot ARNs to `snapshotArnsToRestor
declare const vpc: ec2.Vpc;

const serverlessCache = new ServerlessCache(this, 'ServerlessCache', {
engine: Engine.VALKEY,
serverlessCacheEngine: ServerlessCacheEngine.valkey({
engineVersion: ValkeyEngineVersion.VER_8,
}),
// set the snapshot to restore
snapshotArnsToRestore: ['arn:aws:elasticache:us-east-1:123456789012:serverlesscachesnapshot:my-final-snapshot'],
vpc,
majorEngineVersion: MajorVersion.VER_8,
});
```

Expand All @@ -347,12 +352,13 @@ To use CMK, set your CMK to the `kmsKey` property:
declare const kmsKey: kms.Key;

const serverlessCache = new ServerlessCache(this, 'ServerlessCache', {
engine: Engine.VALKEY,
serverlessCacheEngine: ServerlessCacheEngine.valkey({
engineVersion: ValkeyEngineVersion.VER_8,
}),
serverlessCacheName: 'my-serverless-cache',
vpc,
// set Customer Managed Key
kmsKey,
majorEngineVersion: MajorVersion.VER_8,
});
```

Expand Down
1 change: 1 addition & 0 deletions src/aws-elasticache/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './daily-snapshot-time';
export * from './serverless-cache';
export * from './serverless-cache-engine';
export * from './util';
export * from './user';
export * from './user-group';
185 changes: 185 additions & 0 deletions src/aws-elasticache/serverless-cache-engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { Engine } from './util';

interface EngineVersionBaseProps {
/**
* The major version of the engine.
*/
readonly majorVersion: string;
}
Comment on lines +3 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created the props instead of directly using majorVersion as arguments for the EngineVersion in case properties such as fullVersion are added in the future.


abstract class EngineVersionBase {
/**
* The major version of the engine.
*/
public readonly majorVersion: string;

protected constructor(props: EngineVersionBaseProps) {
this.majorVersion = props.majorVersion;
}
}

/**
* Properties for the Valkey engine version.
*/
export interface ValkeyEngineVersionProps extends EngineVersionBaseProps {}
Comment on lines +21 to +24
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the possibility that properties such as fullVersion may be added only for partial engines in the future, I have prepared props for each engine: ValkeyEngineVersionProps, RedisEngineVersionProps and MemcachedEngineVersionProps.


/**
* Valkey engine version for serverless cache.
*/
export class ValkeyEngineVersion extends EngineVersionBase {
/**
* Version 7
*/
public static readonly VER_7 = new ValkeyEngineVersion({ majorVersion: '7' });

/**
* Version 8
*/
public static readonly VER_8 = new ValkeyEngineVersion({ majorVersion: '8' });

/**
* Creates a ValkeyEngineVersion.
* @param props The properties for the Valkey engine version.
* @returns A ValkeyEngineVersion.
*/
public static of(props: ValkeyEngineVersionProps): ValkeyEngineVersion {
return new ValkeyEngineVersion(props);
}

private constructor(props: ValkeyEngineVersionProps) {
super(props);
}
}

/**
* Properties of the Valkey engine for serverless cache.
*/
export interface ValkeyEngineProps {
/**
* The engine version of the Valkey engine.
*/
readonly engineVersion: ValkeyEngineVersion;
}
Comment on lines +54 to +62
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EngineVersion class (property) is wrapped in the EngineProps for factory methods of the ServerlessCacheEngine to allow for the possibility of additional elements other than the version in the future.


/**
* Properties for the Redis engine version.
*/
export interface RedisEngineVersionProps extends EngineVersionBaseProps {}

/**
* Redis engine version for serverless cache.
*/
export class RedisEngineVersion extends EngineVersionBase {
/**
* Version 7
*/
public static readonly VER_7 = new RedisEngineVersion({ majorVersion: '7' });

/**
* Creates a RedisEngineVersion.
* @param props The properties for the Redis engine version.
* @returns A RedisEngineVersion.
*/
public static of(props: RedisEngineVersionProps): RedisEngineVersion {
return new RedisEngineVersion(props);
}

private constructor(props: RedisEngineVersionProps) {
super(props);
}
}

/**
* Properties of the Redis engine for serverless cache.
*/
export interface RedisEngineProps {
/**
* The engine version of the Redis engine.
*/
readonly engineVersion: RedisEngineVersion;
}

/**
* Properties for the Memcached engine version.
*/
export interface MemcachedEngineVersionProps extends EngineVersionBaseProps {}

/**
* Memcached engine version for serverless cache.
*/
export class MemcachedEngineVersion extends EngineVersionBase {
/**
* Version 1.6
*/
public static readonly VER_1_6 = new MemcachedEngineVersion({ majorVersion: '1.6' });

/**
* Creates a MemcachedEngineVersion.
* @param props The properties for the Memcached engine version.
* @returns A MemcachedEngineVersion.
*/
public static of(props: MemcachedEngineVersionProps): MemcachedEngineVersion {
return new MemcachedEngineVersion(props);
}

private constructor(props: MemcachedEngineVersionProps) {
super(props);
}
}

/**
* Properties of the Memcached engine for serverless cache.
*/
export interface MemcachedEngineProps {
/**
* The engine version of the Memcached engine.
*/
readonly engineVersion: MemcachedEngineVersion;
}

/**
* Engine class for serverless cache.
*/
export class ServerlessCacheEngine {
/**
* Creates a ServerlessCacheEngine for Redis.
* @param props The properties for the Redis engine.
* @returns A ServerlessCacheEngine for Redis.
*/
public static redis(props: RedisEngineProps): ServerlessCacheEngine {
return new ServerlessCacheEngine(Engine.REDIS, props.engineVersion.majorVersion);
}

/**
* Creates a ServerlessCacheEngine for Valkey.
* @param props The properties for the Valkey engine.
* @returns A ServerlessCacheEngine for Valkey.
*/
public static valkey(props: ValkeyEngineProps): ServerlessCacheEngine {
return new ServerlessCacheEngine(Engine.VALKEY, props.engineVersion.majorVersion);
}

/**
* Creates a ServerlessCacheEngine for Memcached.
* @param props The properties for the Memcached engine.
* @returns A ServerlessCacheEngine for Memcached.
*/
public static memcached(props: MemcachedEngineProps): ServerlessCacheEngine {
return new ServerlessCacheEngine(Engine.MEMCACHED, props.engineVersion.majorVersion);
}

/**
* The engine type of the serverless cache.
*/
public readonly engine: Engine;

/**
* The major engine version of the serverless cache.
*/
public readonly majorEngineVersion: string;

private constructor(engine: Engine, majorEngineVersion: string) {
this.engine = engine;
this.majorEngineVersion = majorEngineVersion;
}
}
33 changes: 8 additions & 25 deletions src/aws-elasticache/serverless-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Metric, MetricOptions } from 'aws-cdk-lib/aws-cloudwatch';
import { IKey } from 'aws-cdk-lib/aws-kms';
import { Construct } from 'constructs';
import { DailySnapshotTime } from './daily-snapshot-time';
import { ServerlessCacheEngine } from './serverless-cache-engine';
import { IUserGroup } from './user-group';
import { Engine } from './util';

Expand Down Expand Up @@ -241,29 +242,14 @@ export interface IServerlessCache extends IResource, aws_ec2.IConnectable {
metric(metricName: string, props?: MetricOptions): Metric;
}

/**
* The version number of the engine the serverless cache is compatible with.
*/
export enum MajorVersion {
/**
* Version 7
*/
VER_7 = '7',

/**
* Version 8
*/
VER_8 = '8',
}

/**
* Properties for defining an ElastiCache Serverless Cache.
*/
export interface ServerlessCacheProps {
/**
* The engine the serverless cache is compatible with.
*/
readonly engine: Engine;
readonly serverlessCacheEngine: ServerlessCacheEngine;

/**
* The unique identifier of the serverless cache.
Expand Down Expand Up @@ -311,11 +297,6 @@ export interface ServerlessCacheProps {
*/
readonly kmsKey?: IKey;

/**
* The version number of the engine the serverless cache is compatible with.
*/
readonly majorEngineVersion: MajorVersion;

/**
* The security groups to associate with the serverless cache.
*
Expand Down Expand Up @@ -599,14 +580,14 @@ export class ServerlessCache extends ServerlessCacheBase {
this.validateUserGroup();

const serverlessCache = this.createResource(this, 'Resource', {
engine: this.props.engine,
engine: this.props.serverlessCacheEngine.engine,
serverlessCacheName: this.physicalName,
cacheUsageLimits: this.renderCacheUsageLimits(),
dailySnapshotTime: props.dailySnapshotTime?.toTimestamp(),
description: this.props.description,
finalSnapshotName: this.props.finalSnapshotName,
kmsKeyId: this.props.kmsKey?.keyArn,
majorEngineVersion: this.props.majorEngineVersion,
majorEngineVersion: this.props.serverlessCacheEngine.majorEngineVersion,
securityGroupIds: this.securityGroups.map(sg => sg.securityGroupId),
subnetIds: this.props.vpc.selectSubnets(this.vpcSubnets).subnetIds,
snapshotArnsToRestore: this.props.snapshotArnsToRestore,
Expand Down Expand Up @@ -748,8 +729,10 @@ export class ServerlessCache extends ServerlessCacheBase {
private validateUserGroup(): void {
if (this.props.userGroup === undefined) return;

if (![Engine.REDIS, Engine.VALKEY].includes(this.props.engine)) {
throw new Error(`\`userGroup\` is available for Valkey and Redis OSS only, got engine: ${this.props.engine}.`);
if (![Engine.REDIS, Engine.VALKEY].includes(this.props.serverlessCacheEngine.engine)) {
throw new Error(
`\`userGroup\` is available for Valkey and Redis OSS only, got engine: ${this.props.serverlessCacheEngine.engine}.`,
);
}
}
}
Loading