Skip to content

Commit

Permalink
Switch to use IVpc so we can import vpc from attributes and add optio…
Browse files Browse the repository at this point in the history
…nal redis database config and env var so we can share redis clusters between mesh instances.
  • Loading branch information
AdamJHall committed Oct 13, 2023
1 parent 53a625f commit dde62e4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
24 changes: 14 additions & 10 deletions packages/graphql-mesh-server/lib/fargate.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -20,7 +20,7 @@ export interface MeshServiceProps {
/**
* VPC to attach Redis instance to
*/
vpc?: Vpc;
vpc?: IVpc;
/**
* Repository to pull the container image from
*/
Expand All @@ -46,17 +46,20 @@ 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
*/
secrets?: { [key: string]: ssm.IStringParameter | ssm.IStringListParameter };
}

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;
Expand Down Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions packages/graphql-mesh-server/lib/graphql-mesh-server.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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')
*/
Expand Down Expand Up @@ -46,17 +46,20 @@ 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
*/
secrets?: { [key: string]: ssm.IStringParameter | ssm.IStringListParameter };
};

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;
Expand All @@ -73,7 +76,7 @@ export class MeshHosting extends Construct {
});

const redis =
props.redis ||
props.redis?.service ||
new RedisService(this, "redis", {
...props,
vpc: this.vpc,
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-mesh-server/lib/redis-construct.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,7 +11,7 @@ export interface RedisServiceProps {
/**
* VPC to attach Redis instance to
*/
vpc: Vpc;
vpc: IVpc;
/**
* Cache node type (default: 'cache.t2.micro')
*/
Expand All @@ -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) {
Expand Down

0 comments on commit dde62e4

Please sign in to comment.