Skip to content

Commit

Permalink
Defined a new construct to enable creating a scheduled ec2 task using…
Browse files Browse the repository at this point in the history
… cloudwatch.

Update aws-ecs module README
  • Loading branch information
piradeepk committed Apr 25, 2019
1 parent 44e86e0 commit a6d3574
Show file tree
Hide file tree
Showing 6 changed files with 1,606 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ const ecsService = new ecs.LoadBalancedEc2Service(this, 'Service', {
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
});

// Instantiate an Amazon EC2 Task to run at a scheduled interval
const ecsScheduledTask = new ScheduledEc2Task(this, 'ScheduledTask', {
cluster,
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
scheduleExpression: 'rate(1 minute)',
environment: [{ name: 'TRIGGER', value: 'CloudWatch Events' }],
memoryLimitMiB: 256
});
```

## AWS Fargate vs Amazon ECS
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export * from './images/ecr';
export * from './log-drivers/aws-log-driver';
export * from './log-drivers/log-driver';

export * from './scheduled-ecs-task';

// AWS::ECS CloudFormation Resources:
//
export * from './ecs.generated';
108 changes: 108 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/scheduled-ecs-task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import events = require('@aws-cdk/aws-events');
import cdk = require('@aws-cdk/cdk');
import { ICluster } from './cluster';
import { ContainerImage } from './container-image';
import { Ec2EventRuleTarget } from './ec2/ec2-event-rule-target';
import { Ec2TaskDefinition } from './ec2/ec2-task-definition';
import { AwsLogDriver } from './log-drivers/aws-log-driver';

export interface ScheduledEc2TaskProps {
/**
* The cluster where your service will be deployed.
*/
readonly cluster: ICluster;

/**
* The image to start.
*/
readonly image: ContainerImage;

/**
* The schedule or rate (frequency) that determines when CloudWatch Events
* runs the rule. For more information, see Schedule Expression Syntax for
* Rules in the Amazon CloudWatch User Guide.
*
* @see http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
*
* You must specify this property.
*/
readonly scheduleExpression: string;

/**
* The CMD value to pass to the container. A string with commands delimited by commas.
*/
readonly command?: string;

/**
* The minimum number of CPU units to reserve for the container.
*/
readonly cpu?: number;

/**
* Number of desired copies of running tasks.
*
* @default 1
*/
readonly desiredTaskCount?: number;

/**
* The environment variables to pass to the container.
*/
readonly environment?: { [key: string]: string };

/**
* The hard limit (in MiB) of memory to present to the container.
*
* If your container attempts to exceed the allocated memory, the container
* is terminated.
*
* At least one of memoryLimitMiB and memoryReservationMiB is required for non-Fargate services.
*/
readonly memoryLimitMiB?: number;

/**
* The soft limit (in MiB) of memory to reserve for the container.
*
* When system memory is under contention, Docker attempts to keep the
* container memory within the limit. If the container requires more memory,
* it can consume up to the value specified by the Memory property or all of
* the available memory on the container instance—whichever comes first.
*
* At least one of memoryLimitMiB and memoryReservationMiB is required for non-Fargate services.
*/
readonly memoryReservationMiB?: number;
}

/**
* A scheduled Ec2 task that will be initiated off of cloudwatch events.
*/
export class ScheduledEc2Task extends cdk.Construct {
constructor(scope: cdk.Construct, id: string, props: ScheduledEc2TaskProps) {
super(scope, id);

// Create a Task Definition for the container to start, also creates a log driver
const taskDefinition = new Ec2TaskDefinition(this, 'ScheduledTaskDef');
taskDefinition.addContainer('ScheduledContainer', {
image: props.image,
memoryLimitMiB: props.memoryLimitMiB,
memoryReservationMiB: props.memoryReservationMiB,
cpu: props.cpu,
command: props.command !== undefined ? cdk.Fn.split(",", props.command) : undefined,
environment: props.environment,
logging: new AwsLogDriver(this, 'ScheduledTaskLogging', { streamPrefix: this.node.id })
});

// Use Ec2TaskEventRuleTarget as the target of the EventRule
const eventRuleTarget = new Ec2EventRuleTarget(this, 'ScheduledEventRuleTarget', {
cluster: props.cluster,
taskDefinition,
taskCount: props.desiredTaskCount !== undefined ? props.desiredTaskCount : 1
});

// An EventRule that describes the event trigger (in this case a scheduled run)
const eventRule = new events.EventRule(this, 'ScheduledEventRule', {
scheduleExpression: props.scheduleExpression,
});
eventRule.addTarget(eventRuleTarget);
}
}
Loading

0 comments on commit a6d3574

Please sign in to comment.