Skip to content

Commit

Permalink
feat(ecs): add support for start and stop timeout in ContainerDefinit…
Browse files Browse the repository at this point in the history
…ion (aws#4638)

* Add start|stop timeout

* Fix comment
  • Loading branch information
iamhopaul123 authored and mergify[bot] committed Oct 23, 2019
1 parent 80b4ac9 commit b00c0af
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ export interface ContainerDefinitionOptions {
*/
readonly secrets?: { [key: string]: Secret };

/**
* Time duration (in seconds) to wait before giving up on resolving dependencies for a container.
*
* @default - none
*/
readonly startTimeout?: cdk.Duration;

/**
* Time duration (in seconds) to wait before the container is forcefully killed if it doesn't exit normally on its own.
*
* @default - none
*/
readonly stopTimeout?: cdk.Duration;

/**
* Specifies whether the container is marked essential.
*
Expand Down Expand Up @@ -522,6 +536,8 @@ export class ContainerDefinition extends cdk.Construct {
privileged: this.props.privileged,
readonlyRootFilesystem: this.props.readonlyRootFilesystem,
repositoryCredentials: this.imageConfig.repositoryCredentials,
startTimeout: this.props.startTimeout && this.props.startTimeout.toSeconds(),
stopTimeout: this.props.stopTimeout && this.props.stopTimeout.toSeconds(),
ulimits: cdk.Lazy.anyValue({ produce: () => this.ulimits.map(renderUlimit) }, { omitEmptyArray: true }),
user: this.props.user,
volumesFrom: cdk.Lazy.anyValue({ produce: () => this.volumesFrom.map(renderVolumeFrom) }, { omitEmptyArray: true }),
Expand Down
146 changes: 146 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/test.container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,152 @@ export = {
test.done();
},

"add a container using all props"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');
const secret = new secretsmanager.Secret(stack, 'Secret');
new ecs.ContainerDefinition(stack, "Container", {
image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"),
taskDefinition,
memoryLimitMiB: 1024,
memoryReservationMiB: 512,
command: ["CMD-SHELL"],
cpu: 128,
disableNetworking: true,
dnsSearchDomains: ['example.com'],
dnsServers: ['host.com'],
dockerLabels: {
key: 'fooLabel',
value: 'barLabel'
},
dockerSecurityOptions: ['ECS_SELINUX_CAPABLE=true'],
entryPoint: ["top", "-b"],
environment: {
key: "foo",
value: "bar"
},
essential: true,
extraHosts: {
name: 'dev-db.hostname.pvt'
},
gpuCount: 256,
hostname: "host.example.com",
privileged: true,
readonlyRootFilesystem: true,
startTimeout: cdk.Duration.millis(2000),
stopTimeout: cdk.Duration.millis(5000),
user: "rootUser",
workingDirectory: "a/b/c",
healthCheck: {
command: ["curl localhost:8000"]
},
linuxParameters: new ecs.LinuxParameters(stack, 'LinuxParameters'),
logging: new ecs.AwsLogDriver({ streamPrefix: 'prefix' }),
secrets: {
SECRET: ecs.Secret.fromSecretsManager(secret),
}
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Command: [
"CMD-SHELL"
],
Cpu: 128,
DisableNetworking: true,
DnsSearchDomains: [
"example.com"
],
DnsServers: [
"host.com"
],
DockerLabels: {
key: "fooLabel",
value: "barLabel"
},
DockerSecurityOptions: [
"ECS_SELINUX_CAPABLE=true"
],
EntryPoint: [
"top",
"-b"
],
Environment: [
{
Name: "key",
Value: "foo"
},
{
Name: "value",
Value: "bar"
}
],
Essential: true,
ExtraHosts: [
{
Hostname: "name",
IpAddress: "dev-db.hostname.pvt"
}
],
HealthCheck: {
Command: [
"CMD-SHELL",
"curl localhost:8000"
],
Interval: 30,
Retries: 3,
Timeout: 5
},
Hostname: "host.example.com",
Image: "/aws/aws-example-app",
LinuxParameters: {
Capabilities: {}
},
LogConfiguration: {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ContainerLogGroupE6FD74A4"
},
"awslogs-stream-prefix": "prefix",
"awslogs-region": {
Ref: "AWS::Region"
}
}
},
Memory: 1024,
MemoryReservation: 512,
Name: "Container",
Privileged: true,
ReadonlyRootFilesystem: true,
ResourceRequirements: [
{
Type: "GPU",
Value: "256"
}
],
Secrets: [
{
Name: "SECRET",
ValueFrom: {
Ref: "SecretA720EF05"
}
}
],
StartTimeout: 2,
StopTimeout: 5,
User: "rootUser",
WorkingDirectory: "a/b/c"
}
]
}));

test.done();
},

"throws when MemoryLimit is less than MemoryReservationLimit"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit b00c0af

Please sign in to comment.