Skip to content

Commit

Permalink
feat(aws-ecs-patterns): setup associated DLQ when Queue created by CD…
Browse files Browse the repository at this point in the history
…K for QueueProcessingFargateService
  • Loading branch information
SeekerWing committed Feb 20, 2020
1 parent 3bddd35 commit 4c18e46
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
Expand Up @@ -80,6 +80,81 @@ export = {
test.done();
},

'test ECS queue worker service construct - with optional props for queues'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

// WHEN
new ecsPatterns.QueueProcessingEc2Service(stack, 'Service', {
cluster,
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry('test'),
maxReceiveCount: 42,
retentionPeriod: cdk.Duration.days(7)
});

// THEN - QueueWorker is of EC2 launch type, an SQS queue is created and all default properties are set.
expect(stack).to(haveResource("AWS::ECS::Service", {
DesiredCount: 1,
LaunchType: "EC2",
}));

expect(stack).to(haveResource("AWS::SQS::Queue", {
RedrivePolicy: {
deadLetterTargetArn: {
"Fn::GetAtt": [
"ServiceEcsProcessingDeadLetterQueue4A89196E",
"Arn"
]
},
maxReceiveCount: 42
}
}));

expect(stack).to(haveResource("AWS::SQS::Queue", {
MessageRetentionPeriod: 604800
}));

expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [
{
Name: "QUEUE_NAME",
Value: {
"Fn::GetAtt": [
"ServiceEcsProcessingQueueC266885C",
"QueueName"
]
}
}
],
LogConfiguration: {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ServiceQueueProcessingTaskDefQueueProcessingContainerLogGroupD52338D1"
},
"awslogs-stream-prefix": "Service",
"awslogs-region": {
Ref: "AWS::Region"
}
}
},
Essential: true,
Image: "test",
Memory: 512
}
],
Family: "ServiceQueueProcessingTaskDef83DB34F1"
}));

test.done();
},

'test ECS queue worker service construct - with optional props'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Expand Up @@ -102,6 +102,103 @@ export = {
test.done();
},

'test fargate queue worker service construct - with optional props for queues'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

// WHEN
new ecsPatterns.QueueProcessingFargateService(stack, 'Service', {
cluster,
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry('test'),
maxReceiveCount: 42,
retentionPeriod: cdk.Duration.days(7)
});

// THEN - QueueWorker is of FARGATE launch type, an SQS queue is created and all default properties are set.
expect(stack).to(haveResource("AWS::ECS::Service", {
DesiredCount: 1,
LaunchType: "FARGATE",
}));

expect(stack).to(haveResource("AWS::SQS::Queue", {
RedrivePolicy: {
deadLetterTargetArn: {
"Fn::GetAtt": [
"ServiceEcsProcessingDeadLetterQueue4A89196E",
"Arn"
]
},
maxReceiveCount: 42
}
}));

expect(stack).to(haveResource("AWS::SQS::Queue", {
MessageRetentionPeriod: 604800
}));

expect(stack).to(haveResource("AWS::IAM::Policy", {
PolicyDocument: {
Statement: [
{
Action: [
"sqs:ReceiveMessage",
"sqs:ChangeMessageVisibility",
"sqs:GetQueueUrl",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes"
],
Effect: "Allow",
Resource: {
"Fn::GetAtt": [
"ServiceEcsProcessingQueueC266885C",
"Arn"
]
}
}
],
Version: "2012-10-17"
}
}));

expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [
{
Name: "QUEUE_NAME",
Value: {
"Fn::GetAtt": [
"ServiceEcsProcessingQueueC266885C",
"QueueName"
]
}
}
],
LogConfiguration: {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ServiceQueueProcessingTaskDefQueueProcessingContainerLogGroupD52338D1"
},
"awslogs-stream-prefix": "Service",
"awslogs-region": {
Ref: "AWS::Region"
}
}
},
Image: "test",
}
],
Family: "ServiceQueueProcessingTaskDef83DB34F1"
}));

test.done();
},

'test Fargate queue worker service construct - with optional props'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 4c18e46

Please sign in to comment.