Skip to content

Commit

Permalink
Merge pull request aws#11093 from iRoachie/container-name
Browse files Browse the repository at this point in the history
feat(ecs-patterns): add containerName to QueueProcessingEc2Service
  • Loading branch information
SoManyHs authored Dec 23, 2020
2 parents 6ccd00f + 1d0148a commit 52a80bb
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ const queueProcessingEc2Service = new QueueProcessingEc2Service(stack, 'Service'
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
},
queue,
maxScalingCapacity: 5
maxScalingCapacity: 5,
containerName: 'test',
});
```

Expand All @@ -283,7 +284,8 @@ const queueProcessingFargateService = new QueueProcessingFargateService(stack, '
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
},
queue,
maxScalingCapacity: 5
maxScalingCapacity: 5,
containerName: 'test',
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export interface QueueProcessingEc2ServiceProps extends QueueProcessingServiceBa
* @default - No memory reserved.
*/
readonly memoryReservationMiB?: number;

/**
* Optional name for the container added
*
* @default - QueueProcessingContainer
*/
readonly containerName?: string;
}

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

const containerName = props.containerName ?? 'QueueProcessingContainer';

// Create a Task Definition for the container to start
this.taskDefinition = new Ec2TaskDefinition(this, 'QueueProcessingTaskDef', {
family: props.family,
});
this.taskDefinition.addContainer('QueueProcessingContainer', {
this.taskDefinition.addContainer(containerName, {
image: props.image,
memoryLimitMiB: props.memoryLimitMiB,
memoryReservationMiB: props.memoryReservationMiB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export interface QueueProcessingFargateServiceProps extends QueueProcessingServi
* @default Latest
*/
readonly platformVersion?: FargatePlatformVersion;

/**
* Optional name for the container added
*
* @default - QueueProcessingContainer
*/
readonly containerName?: string;
}

/**
Expand Down Expand Up @@ -86,7 +93,10 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
cpu: props.cpu || 256,
family: props.family,
});
this.taskDefinition.addContainer('QueueProcessingContainer', {

const containerName = props.containerName ?? 'QueueProcessingContainer';

this.taskDefinition.addContainer(containerName, {
image: props.image,
command: props.command,
environment: this.environment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,31 @@ export = {

test.done();
},

'can set custom containerName'(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'),
containerName: 'my-container',
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Name: 'my-container',
},
],
}));

test.done();
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,29 @@ export = {

test.done();
},

'can set custom containerName'(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,
containerName: 'my-container',
image: ecs.ContainerImage.fromRegistry('test'),
});

expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Name: 'my-container',
},
],
}));

test.done();
},
};

0 comments on commit 52a80bb

Please sign in to comment.