-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
744 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.