Skip to content

Commit 75186b0

Browse files
committed
Add Redis cache
1 parent 8871932 commit 75186b0

File tree

4 files changed

+744
-0
lines changed

4 files changed

+744
-0
lines changed

src/cache/Redis.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { CfnCacheCluster, CfnCacheClusterProps, CfnSubnetGroup } from 'aws-cdk-lib/aws-elasticache';
2+
import { Names } from 'aws-cdk-lib';
3+
import { Port, SecurityGroup } from 'aws-cdk-lib/aws-ec2';
4+
import { Construct } from 'constructs';
5+
import { VpcForServerlessApp } from '../vpc/VpcForServerlessApp';
6+
7+
export type RedisProps = Partial<CfnCacheClusterProps> & {
8+
vpc: VpcForServerlessApp;
9+
};
10+
11+
export class Redis extends CfnCacheCluster {
12+
constructor(scope: Construct, id: string, props: RedisProps) {
13+
const securityGroup = new SecurityGroup(scope, `${id}SecurityGroup`, {
14+
vpc: props.vpc,
15+
description: 'Security group for Redis',
16+
allowAllOutbound: false,
17+
allowAllIpv6Outbound: false,
18+
});
19+
20+
const stackId = Names.uniqueResourceName(securityGroup, {
21+
maxLength: 100,
22+
});
23+
const subnetGroup = new CfnSubnetGroup(scope, `${id}SubnetGroup`, {
24+
cacheSubnetGroupName: `${stackId}${id}SubnetGroup`,
25+
description: 'Subnet group for Redis',
26+
// Isolated subnets don't have a route to the internet (unlike private), this is what we want
27+
subnetIds: props.vpc.isolatedSubnets.map((subnet) => subnet.subnetId),
28+
});
29+
30+
props.vpc.appSecurityGroup.connections.allowTo(
31+
securityGroup,
32+
Port.tcp(props.port ?? 6379),
33+
'Allow Lambda functions to connect to Redis'
34+
);
35+
36+
super(scope, id, {
37+
engine: 'redis',
38+
cacheNodeType: 'cache.t3.micro',
39+
numCacheNodes: 1,
40+
vpcSecurityGroupIds: [securityGroup.securityGroupId],
41+
cacheSubnetGroupName: subnetGroup.cacheSubnetGroupName,
42+
...props,
43+
});
44+
45+
this.addDependsOn(subnetGroup);
46+
}
47+
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Redis } from './cache/Redis';
12
import { ConsoleFunction } from './function/ConsoleFunction';
23
import { PhpFpmFunction, PhpFpmFunctionProps } from './function/PhpFpmFunction';
34
import { PhpFunction, PhpFunctionProps } from './function/PhpFunction';
45
import { packagePhpCode } from './package';
6+
import { VpcForServerlessApp } from './vpc/VpcForServerlessApp';
57

68
export {
79
PhpFunction,
@@ -10,4 +12,6 @@ export {
1012
PhpFpmFunctionProps,
1113
ConsoleFunction,
1214
packagePhpCode,
15+
VpcForServerlessApp,
16+
Redis,
1317
};

test/cache/Redis.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { cleanupTemplate, compileTestStack } from '../helper';
3+
import { Redis, VpcForServerlessApp } from '../../src';
4+
5+
describe('Redis', () => {
6+
it('builds', () => {
7+
const template = compileTestStack((stack) => {
8+
new Redis(stack, 'Redis', {
9+
vpc: new VpcForServerlessApp(stack, 'Vpc'),
10+
});
11+
}).toJSON();
12+
13+
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
14+
});
15+
});

0 commit comments

Comments
 (0)