Skip to content

Commit

Permalink
Add Redis cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Dec 21, 2022
1 parent 8871932 commit 75186b0
Show file tree
Hide file tree
Showing 4 changed files with 744 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/cache/Redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { CfnCacheCluster, CfnCacheClusterProps, CfnSubnetGroup } from 'aws-cdk-lib/aws-elasticache';
import { Names } from 'aws-cdk-lib';
import { Port, SecurityGroup } from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import { VpcForServerlessApp } from '../vpc/VpcForServerlessApp';

export type RedisProps = Partial<CfnCacheClusterProps> & {
vpc: VpcForServerlessApp;
};

export class Redis extends CfnCacheCluster {
constructor(scope: Construct, id: string, props: RedisProps) {
const securityGroup = new SecurityGroup(scope, `${id}SecurityGroup`, {
vpc: props.vpc,
description: 'Security group for Redis',
allowAllOutbound: false,
allowAllIpv6Outbound: false,
});

const stackId = Names.uniqueResourceName(securityGroup, {
maxLength: 100,
});
const subnetGroup = new CfnSubnetGroup(scope, `${id}SubnetGroup`, {
cacheSubnetGroupName: `${stackId}${id}SubnetGroup`,
description: 'Subnet group for Redis',
// Isolated subnets don't have a route to the internet (unlike private), this is what we want
subnetIds: props.vpc.isolatedSubnets.map((subnet) => subnet.subnetId),
});

props.vpc.appSecurityGroup.connections.allowTo(
securityGroup,
Port.tcp(props.port ?? 6379),
'Allow Lambda functions to connect to Redis'
);

super(scope, id, {
engine: 'redis',
cacheNodeType: 'cache.t3.micro',
numCacheNodes: 1,
vpcSecurityGroupIds: [securityGroup.securityGroupId],
cacheSubnetGroupName: subnetGroup.cacheSubnetGroupName,
...props,
});

this.addDependsOn(subnetGroup);
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Redis } from './cache/Redis';
import { ConsoleFunction } from './function/ConsoleFunction';
import { PhpFpmFunction, PhpFpmFunctionProps } from './function/PhpFpmFunction';
import { PhpFunction, PhpFunctionProps } from './function/PhpFunction';
import { packagePhpCode } from './package';
import { VpcForServerlessApp } from './vpc/VpcForServerlessApp';

export {
PhpFunction,
Expand All @@ -10,4 +12,6 @@ export {
PhpFpmFunctionProps,
ConsoleFunction,
packagePhpCode,
VpcForServerlessApp,
Redis,
};
15 changes: 15 additions & 0 deletions test/cache/Redis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';
import { cleanupTemplate, compileTestStack } from '../helper';
import { Redis, VpcForServerlessApp } from '../../src';

describe('Redis', () => {
it('builds', () => {
const template = compileTestStack((stack) => {
new Redis(stack, 'Redis', {
vpc: new VpcForServerlessApp(stack, 'Vpc'),
});
}).toJSON();

expect(cleanupTemplate(template).Resources).toMatchSnapshot();
});
});
Loading

0 comments on commit 75186b0

Please sign in to comment.