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 2 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
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ const container = ec2TaskDefinition.addContainer("WebContainer", {

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

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

```ts
container.addPortMappings({
containerPort: 3000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment above both to explain that there are 2 ways of defining the port mapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sure. Just saw this. Let me get right on that.

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
22 changes: 22 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,28 @@ 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 }],
},
],
});
});

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