Skip to content

Commit

Permalink
feat(stacks.api/stacks): implement CacheStack for provisioning aws …
Browse files Browse the repository at this point in the history
…managed elasticache redis instance/cluster

Signed-off-by: Braden Mars <bradenmars@bradenmars.me>
  • Loading branch information
BradenM committed Aug 21, 2023
1 parent 8e72010 commit c11daca
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions packages/stacks/api/src/stacks/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as cdk from 'aws-cdk-lib'
import * as ec2 from 'aws-cdk-lib/aws-ec2'
import type * as elasticache from 'aws-cdk-lib/aws-elasticache'
import { RedisDB } from 'cdk-redisdb'
import { type Construct } from 'constructs'
import { type CacheConfig } from '../schema'

export interface CacheProps extends CacheConfig {
vpc: ec2.IVpc
}

/**
* AWS Managed Elasticache Redis stack.
*/
export class CacheStack extends cdk.Stack {
readonly securityGroup: ec2.ISecurityGroup
readonly redis: RedisDB

constructor(
scope: Construct,
id: string,
props: CacheProps,
stackProps?: cdk.StackProps,
) {
super(scope, id, stackProps)

this.securityGroup = new ec2.SecurityGroup(this, id + '-security-group', {
vpc: props.vpc,
description: 'Security group for RedisDB',
allowAllOutbound: false,
})

this.securityGroup.addIngressRule(
ec2.Peer.ipv4(props.vpc.vpcCidrBlock),
ec2.Port.tcp(6379),
'Ingress within VPC',
)

this.securityGroup.addEgressRule(
ec2.Peer.ipv4(props.vpc.vpcCidrBlock),
ec2.Port.allTcp(),
'Egress within VPC',
)

this.redis = new RedisDB(this, id + '-redis', {
nodes: props.nodes,
nodeType: props.nodeType,
replicas: props.replicas,
...(typeof props.memoryAutoscalingTarget === 'number'
? { memoryAutoscalingTarget: props.memoryAutoscalingTarget }
: {}),
engineVersion: props.engineVersion,
existingVpc: props.vpc,
existingSecurityGroup: this.securityGroup,
atRestEncryptionEnabled: true,
})
if (!props.clusterMode) {
this.replicationGroup.addOverride(
'Properties.CacheParameterGroupName',
'default.redis7',
)
}
}

get replicationGroup(): elasticache.CfnReplicationGroup {
return this.redis.replicationGroup
}
}

0 comments on commit c11daca

Please sign in to comment.