From dde62e4506f4dfd464035ededf8f4f394c36b3ed Mon Sep 17 00:00:00 2001 From: Adam Hall Date: Fri, 13 Oct 2023 14:02:03 +1030 Subject: [PATCH] Switch to use IVpc so we can import vpc from attributes and add optional redis database config and env var so we can share redis clusters between mesh instances. --- packages/graphql-mesh-server/lib/fargate.ts | 24 +++++++++++-------- .../lib/graphql-mesh-server.ts | 24 ++++++++++++------- .../lib/redis-construct.ts | 6 ++--- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/packages/graphql-mesh-server/lib/fargate.ts b/packages/graphql-mesh-server/lib/fargate.ts index b4a13e25..0fef4854 100644 --- a/packages/graphql-mesh-server/lib/fargate.ts +++ b/packages/graphql-mesh-server/lib/fargate.ts @@ -1,5 +1,5 @@ import { Construct } from "constructs"; -import { Duration, Token } from "aws-cdk-lib"; +import { Duration } from "aws-cdk-lib"; import { RemovalPolicy } from "aws-cdk-lib"; import * as acm from "aws-cdk-lib/aws-certificatemanager"; import * as ecs from "aws-cdk-lib/aws-ecs"; @@ -8,7 +8,7 @@ import * as ecsPatterns from "aws-cdk-lib/aws-ecs-patterns"; import * as iam from "aws-cdk-lib/aws-iam"; import * as ssm from "aws-cdk-lib/aws-ssm"; import * as auto_scaling from "aws-cdk-lib/aws-autoscaling"; -import { Port, SecurityGroup, Vpc } from "aws-cdk-lib/aws-ec2"; +import { Port, SecurityGroup, IVpc, Vpc } from "aws-cdk-lib/aws-ec2"; import { RedisService } from "./redis-construct"; import { ManagedRule, @@ -20,7 +20,7 @@ export interface MeshServiceProps { /** * VPC to attach Redis instance to */ - vpc?: Vpc; + vpc?: IVpc; /** * Repository to pull the container image from */ @@ -46,9 +46,12 @@ export interface MeshServiceProps { */ memory?: number; /** - * Redis instance to use for mesh caching + * Redis configuration to use for mesh caching */ - redis: RedisService; + redis: { + service: RedisService; + database?: string; + }; /** * SSM values to pass through to the container as secrets */ @@ -56,7 +59,7 @@ export interface MeshServiceProps { } export class MeshService extends Construct { - public readonly vpc: Vpc; + public readonly vpc: IVpc; public readonly repository: ecr.Repository; public readonly service: ecs.FargateService; public readonly firewall: WebApplicationFirewall; @@ -131,13 +134,14 @@ export class MeshService extends Construct { // If using Redis configure security group and pass connection string to container if (props.redis) { - props.redis.securityGroup.addIngressRule( + props.redis.service.securityGroup.addIngressRule( securityGroup, - Port.tcp(props.redis.connectionPort) + Port.tcp(props.redis.service.connectionPort) ); - environment["REDIS_ENDPOINT"] = props.redis.connectionEndPoint; - environment["REDIS_PORT"] = props.redis.connectionPort.toString(); + environment["REDIS_ENDPOINT"] = props.redis.service.connectionEndPoint; + environment["REDIS_PORT"] = props.redis.service.connectionPort.toString(); + environment["REDIS_DATABASE"] = props.redis.database ?? "0"; } // Construct secrets from provided ssm values diff --git a/packages/graphql-mesh-server/lib/graphql-mesh-server.ts b/packages/graphql-mesh-server/lib/graphql-mesh-server.ts index 399c2457..fa696280 100644 --- a/packages/graphql-mesh-server/lib/graphql-mesh-server.ts +++ b/packages/graphql-mesh-server/lib/graphql-mesh-server.ts @@ -1,8 +1,8 @@ import { Construct } from "constructs"; -import { MeshService, MeshServiceProps } from "./fargate"; -import { RedisService, RedisServiceProps } from "./redis-construct"; +import { MeshService } from "./fargate"; +import { RedisService } from "./redis-construct"; import { CodePipelineService } from "./pipeline"; -import { SecurityGroup, Vpc } from "aws-cdk-lib/aws-ec2"; +import { SecurityGroup, IVpc, Vpc } from "aws-cdk-lib/aws-ec2"; import { Repository } from "aws-cdk-lib/aws-ecr"; import { FargateService } from "aws-cdk-lib/aws-ecs"; import { CfnCacheCluster } from "aws-cdk-lib/aws-elasticache"; @@ -12,7 +12,7 @@ export type MeshHostingProps = { /** * VPC to attach Redis and Fargate instances to (default: create a vpc) */ - vpc?: Vpc; + vpc?: IVpc; /** * If no VPC is provided create one with this name (default: 'graphql-server-vpc') */ @@ -46,9 +46,12 @@ export type MeshHostingProps = { */ memory?: number; /** - * Redis instance to use for mesh caching + * Redis configuration to use for mesh caching */ - redis?: RedisService; + redis?: { + service: RedisService; + database?: string; + }; /** * SSM values to pass through to the container as secrets */ @@ -56,7 +59,7 @@ export type MeshHostingProps = { }; export class MeshHosting extends Construct { - public readonly vpc: Vpc; + public readonly vpc: IVpc; public readonly repository: Repository; public readonly service: FargateService; public readonly cacheCluster: CfnCacheCluster; @@ -73,7 +76,7 @@ export class MeshHosting extends Construct { }); const redis = - props.redis || + props.redis?.service || new RedisService(this, "redis", { ...props, vpc: this.vpc, @@ -85,7 +88,10 @@ export class MeshHosting extends Construct { const mesh = new MeshService(this, "mesh", { ...props, vpc: this.vpc, - redis, + redis: { + service: redis, + database: props.redis?.database, + }, }); this.service = mesh.service; diff --git a/packages/graphql-mesh-server/lib/redis-construct.ts b/packages/graphql-mesh-server/lib/redis-construct.ts index 5a9ba5b0..b450ca37 100644 --- a/packages/graphql-mesh-server/lib/redis-construct.ts +++ b/packages/graphql-mesh-server/lib/redis-construct.ts @@ -1,4 +1,4 @@ -import { SecurityGroup, Vpc } from "aws-cdk-lib/aws-ec2"; +import { SecurityGroup, IVpc } from "aws-cdk-lib/aws-ec2"; import { CfnCacheCluster, CfnSubnetGroup, @@ -11,7 +11,7 @@ export interface RedisServiceProps { /** * VPC to attach Redis instance to */ - vpc: Vpc; + vpc: IVpc; /** * Cache node type (default: 'cache.t2.micro') */ @@ -20,7 +20,7 @@ export interface RedisServiceProps { export class RedisService extends Construct { public readonly cacheCluster: CfnCacheCluster; - public readonly vpc: Vpc; + public readonly vpc: IVpc; public readonly securityGroup: SecurityGroup; constructor(scope: Construct, id: string, props: RedisServiceProps) {