-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(celery): add elasticache, celery worker and celery beat constructs
- Loading branch information
Brian Caffey
committed
May 14, 2021
1 parent
c1c1a49
commit 634b268
Showing
5 changed files
with
145 additions
and
14 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,42 @@ | ||
import * as ecs from '@aws-cdk/aws-ecs'; | ||
import * as logs from '@aws-cdk/aws-logs'; | ||
import * as cdk from '@aws-cdk/core'; | ||
|
||
export interface CeleryBeatProps { | ||
readonly image: ecs.ContainerImage; | ||
readonly command: string[]; | ||
readonly environment: { [key: string]: string }; | ||
readonly cluster: ecs.ICluster; | ||
} | ||
|
||
export class CeleryBeat extends cdk.Construct { | ||
constructor(scope: cdk.Construct, id: string, props: CeleryBeatProps) { | ||
super(scope, id); | ||
|
||
|
||
const taskDefinition = new ecs.TaskDefinition(scope, `TaskDefinitionFor${id}`, { | ||
compatibility: ecs.Compatibility.FARGATE, | ||
cpu: '256', | ||
memoryMiB: '512', | ||
}); | ||
|
||
taskDefinition.addContainer(`TaskContainerFor${id}`, { | ||
image: props.image, | ||
command: props.command, | ||
environment: props.environment, | ||
logging: ecs.LogDriver.awsLogs( | ||
{ | ||
logRetention: logs.RetentionDays.ONE_DAY, | ||
streamPrefix: `${id}Container`, | ||
}, | ||
), | ||
}); | ||
|
||
new ecs.FargateService(scope, `FargateService${id}`, { | ||
cluster: props.cluster, | ||
taskDefinition, | ||
// only run one instance of celery beat | ||
desiredCount: 1, | ||
}); | ||
} | ||
} |
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,2 @@ | ||
export * from './beat'; | ||
export * from './worker'; |
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,43 @@ | ||
import * as ecs from '@aws-cdk/aws-ecs'; | ||
import * as logs from '@aws-cdk/aws-logs'; | ||
import * as cdk from '@aws-cdk/core'; | ||
|
||
export interface CeleryWorkerProps { | ||
readonly image: ecs.ContainerImage; | ||
readonly command: string[]; | ||
readonly environment: { [key: string]: string }; | ||
readonly cluster: ecs.ICluster; | ||
} | ||
|
||
export class CeleryWorker extends cdk.Construct { | ||
constructor(scope: cdk.Construct, id: string, props: CeleryWorkerProps) { | ||
super(scope, id); | ||
|
||
|
||
const taskDefinition = new ecs.TaskDefinition(scope, `TaskDefinitionFor${id}`, { | ||
compatibility: ecs.Compatibility.FARGATE, | ||
cpu: '256', | ||
memoryMiB: '512', | ||
}); | ||
|
||
taskDefinition.addContainer(`TaskContainerFor${id}`, { | ||
image: props.image, | ||
command: props.command, | ||
environment: props.environment, | ||
logging: ecs.LogDriver.awsLogs( | ||
{ | ||
logRetention: logs.RetentionDays.ONE_DAY, | ||
streamPrefix: `${id}Container`, | ||
}, | ||
), | ||
}); | ||
|
||
new ecs.FargateService(scope, `FargateService${id}`, { | ||
cluster: props.cluster, | ||
taskDefinition, | ||
desiredCount: 1, | ||
}); | ||
|
||
// TODO: scaling options | ||
} | ||
} |
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,41 @@ | ||
import { Port, SecurityGroup, SubnetType, IVpc } from '@aws-cdk/aws-ec2'; | ||
import { CfnCacheCluster, CfnSubnetGroup } from '@aws-cdk/aws-elasticache'; | ||
import * as cdk from '@aws-cdk/core'; | ||
|
||
export interface ElastiCacheRedisProps { | ||
vpc: IVpc; | ||
appSecurityGroup: SecurityGroup; | ||
} | ||
|
||
export class ElastiCacheCluster extends cdk.Construct { | ||
readonly elastiCacheCluster: CfnCacheCluster; | ||
readonly elastiCacheClusterSecurityGroup: SecurityGroup; | ||
constructor(scope: cdk.Construct, id: string, props: ElastiCacheRedisProps) { | ||
super(scope, id); | ||
|
||
const elastiCacheClusterSecurityGroup = new SecurityGroup(scope, 'ElastiCacheSecurityGroup', { | ||
vpc: props.vpc, | ||
description: 'ElastiCache Security Group', | ||
}); | ||
this.elastiCacheClusterSecurityGroup = elastiCacheClusterSecurityGroup; | ||
|
||
const elastiCacheClusterSubnetGroup = new CfnSubnetGroup(scope, 'ElastiCacheRedisSubnetGroup', { | ||
subnetIds: props.vpc.selectSubnets({ | ||
subnetType: SubnetType.ISOLATED, | ||
}).subnetIds, | ||
description: 'ElastiCache Subnet Group', | ||
}); | ||
|
||
this.elastiCacheCluster = new CfnCacheCluster(this, 'ElastiCacheCluster', { | ||
clusterName: `${id}-elstiCacheCluster`, | ||
cacheNodeType: 'cache.t2.micro', | ||
engine: 'redis', | ||
numCacheNodes: 1, | ||
vpcSecurityGroupIds: [elastiCacheClusterSecurityGroup.securityGroupId], | ||
cacheSubnetGroupName: elastiCacheClusterSubnetGroup.ref, | ||
}); | ||
|
||
this.elastiCacheCluster.addDependsOn(elastiCacheClusterSubnetGroup); | ||
elastiCacheClusterSecurityGroup.connections.allowFrom(props.appSecurityGroup, Port.tcp(6379)); | ||
} | ||
} |