Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecs): add port mappings to containers with props #13262

Merged
merged 6 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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