Skip to content

Commit 52a80bb

Browse files
authored
Merge pull request #11093 from iRoachie/container-name
feat(ecs-patterns): add containerName to QueueProcessingEc2Service
2 parents 6ccd00f + 1d0148a commit 52a80bb

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

packages/@aws-cdk/aws-ecs-patterns/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ const queueProcessingEc2Service = new QueueProcessingEc2Service(stack, 'Service'
264264
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
265265
},
266266
queue,
267-
maxScalingCapacity: 5
267+
maxScalingCapacity: 5,
268+
containerName: 'test',
268269
});
269270
```
270271

@@ -283,7 +284,8 @@ const queueProcessingFargateService = new QueueProcessingFargateService(stack, '
283284
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
284285
},
285286
queue,
286-
maxScalingCapacity: 5
287+
maxScalingCapacity: 5,
288+
containerName: 'test',
287289
});
288290
```
289291

packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export interface QueueProcessingEc2ServiceProps extends QueueProcessingServiceBa
5252
* @default - No memory reserved.
5353
*/
5454
readonly memoryReservationMiB?: number;
55+
56+
/**
57+
* Optional name for the container added
58+
*
59+
* @default - QueueProcessingContainer
60+
*/
61+
readonly containerName?: string;
5562
}
5663

5764
/**
@@ -74,11 +81,13 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase {
7481
constructor(scope: Construct, id: string, props: QueueProcessingEc2ServiceProps) {
7582
super(scope, id, props);
7683

84+
const containerName = props.containerName ?? 'QueueProcessingContainer';
85+
7786
// Create a Task Definition for the container to start
7887
this.taskDefinition = new Ec2TaskDefinition(this, 'QueueProcessingTaskDef', {
7988
family: props.family,
8089
});
81-
this.taskDefinition.addContainer('QueueProcessingContainer', {
90+
this.taskDefinition.addContainer(containerName, {
8291
image: props.image,
8392
memoryLimitMiB: props.memoryLimitMiB,
8493
memoryReservationMiB: props.memoryReservationMiB,

packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export interface QueueProcessingFargateServiceProps extends QueueProcessingServi
5959
* @default Latest
6060
*/
6161
readonly platformVersion?: FargatePlatformVersion;
62+
63+
/**
64+
* Optional name for the container added
65+
*
66+
* @default - QueueProcessingContainer
67+
*/
68+
readonly containerName?: string;
6269
}
6370

6471
/**
@@ -86,7 +93,10 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
8693
cpu: props.cpu || 256,
8794
family: props.family,
8895
});
89-
this.taskDefinition.addContainer('QueueProcessingContainer', {
96+
97+
const containerName = props.containerName ?? 'QueueProcessingContainer';
98+
99+
this.taskDefinition.addContainer(containerName, {
90100
image: props.image,
91101
command: props.command,
92102
environment: this.environment,

packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts

+27
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,31 @@ export = {
287287

288288
test.done();
289289
},
290+
291+
'can set custom containerName'(test: Test) {
292+
// GIVEN
293+
const stack = new cdk.Stack();
294+
const vpc = new ec2.Vpc(stack, 'VPC');
295+
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
296+
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
297+
298+
// WHEN
299+
new ecsPatterns.QueueProcessingEc2Service(stack, 'Service', {
300+
cluster,
301+
memoryLimitMiB: 512,
302+
image: ecs.ContainerImage.fromRegistry('test'),
303+
containerName: 'my-container',
304+
});
305+
306+
// THEN
307+
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
308+
ContainerDefinitions: [
309+
{
310+
Name: 'my-container',
311+
},
312+
],
313+
}));
314+
315+
test.done();
316+
},
290317
};

packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts

+25
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,29 @@ export = {
285285

286286
test.done();
287287
},
288+
289+
'can set custom containerName'(test: Test) {
290+
// GIVEN
291+
const stack = new cdk.Stack();
292+
const vpc = new ec2.Vpc(stack, 'VPC');
293+
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
294+
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
295+
296+
// WHEN
297+
new ecsPatterns.QueueProcessingFargateService(stack, 'Service', {
298+
cluster,
299+
containerName: 'my-container',
300+
image: ecs.ContainerImage.fromRegistry('test'),
301+
});
302+
303+
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
304+
ContainerDefinitions: [
305+
{
306+
Name: 'my-container',
307+
},
308+
],
309+
}));
310+
311+
test.done();
312+
},
288313
};

0 commit comments

Comments
 (0)