Skip to content

Commit

Permalink
feat(ecs): add port mappings to containers with props (#13262)
Browse files Browse the repository at this point in the history
This PR adds an optional `portMappings` property to `ContainerDefinitionOptions` so that users can add port mappings when they add a container to a `TaskDefinition`.

Closes #13261

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
misterjoshua authored Feb 26, 2021
1 parent ea91aa3 commit f511639
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
14 changes: 13 additions & 1 deletion packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,22 @@ const container = ec2TaskDefinition.addContainer("WebContainer", {

You can specify container properties when you add them to the task definition, or with various methods, e.g.:

To add a port mapping when adding a container to the task definition, specify the `portMappings` option:

```ts
taskDefinition.addContainer("WebContainer", {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
memoryLimitMiB: 1024,
portMappings: [{ containerPort: 3000 }]
});
```

To add port mappings directly to a container definition, call `addPortMappings()`:

```ts
container.addPortMappings({
containerPort: 3000
})
});
```

To add data volumes to a task definition, call `addVolume()`:
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ export interface ContainerDefinitionOptions {
* @default - No GPUs assigned.
*/
readonly gpuCount?: number;

/**
* The port mappings to add to the container definition.
* @default - No ports are mapped.
*/
readonly portMappings?: PortMapping[];
}

/**
Expand Down Expand Up @@ -433,6 +439,10 @@ export class ContainerDefinition extends CoreConstruct {
}

props.taskDefinition._linkContainer(this);

if (props.portMappings) {
this.addPortMappings(...props.portMappings);
}
}

/**
Expand Down
49 changes: 49 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/container-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,55 @@ describe('container definition', () => {

});

test('can add port mappings to the container definition by props', () => {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');

// WHEN
taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
portMappings: [{ containerPort: 80 }],
});

// THEN
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
PortMappings: [{ ContainerPort: 80 }],
},
],
});
});

test('can add port mappings using props and addPortMappings and both are included', () => {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');

// WHEN
const containerDefinition = taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
portMappings: [{ containerPort: 80 }],
});

containerDefinition.addPortMappings({ containerPort: 443 });

// THEN
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
PortMappings: [
{ ContainerPort: 80 },
{ ContainerPort: 443 },
],
},
],
});
});

describe('Environment Files', () => {
describe('with EC2 task definitions', () => {
test('can add asset environment file to the container definition', () => {
Expand Down
11 changes: 5 additions & 6 deletions packages/@aws-cdk/aws-ecs/test/fargate/integ.lb-awsvpc-nw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef', {
cpu: 512,
});

const container = taskDefinition.addContainer('web', {
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

container.addPortMappings({
containerPort: 80,
protocol: ecs.Protocol.TCP,
portMappings: [{
containerPort: 80,
protocol: ecs.Protocol.TCP,
}],
});

const service = new ecs.FargateService(stack, 'Service', {
Expand Down

0 comments on commit f511639

Please sign in to comment.