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(aws-ecs): add support for Event Targets #1571

Merged
merged 4 commits into from
Feb 4, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Review comments
  • Loading branch information
Rico Huijbers committed Jan 21, 2019
commit 0529e7ad183e9177ba3c0984b890a7a8f6d653e5
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
@@ -266,7 +266,7 @@ you can configure on your instances.
### Integration with CloudWatch Events

To start an ECS task on an EC2-backed Cluster, instantiate an
`Ec2TaskEventRuleTarget` intead of an `Ec2Service`:
`Ec2TaskEventRuleTarget` instead of an `Ec2Service`:

[example of CloudWatch Events integration](test/ec2/integ.event-task.lit.ts)

86 changes: 46 additions & 40 deletions packages/@aws-cdk/aws-ecs/test/ec2/integ.event-task.lit.ts
Original file line number Diff line number Diff line change
@@ -4,45 +4,51 @@ import cdk = require('@aws-cdk/cdk');
import ecs = require('../../lib');

const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-ecs-integ-ecs');

const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 1 });

const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
instanceType: new ec2.InstanceType('t2.micro')
});

/// !show
// Create a Task Definition for the container to start
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');
taskDefinition.addContainer('TheContainer', {
image: ecs.ContainerImage.fromAsset(stack, 'EventImage', { directory: 'eventhandler-image' }),
memoryLimitMiB: 256,
logging: new ecs.AwsLogDriver(stack, 'TaskLogging', { streamPrefix: 'EventDemo' })
});

// An EventRule that describes the event trigger (in this case a scheduled run)
const rule = new events.EventRule(stack, 'Rule', {
scheduleExpression: 'rate(1 minute)',
});

// Use Ec2TaskEventRuleTarget as the target of the EventRule
const target = new ecs.Ec2TaskEventRuleTarget(stack, 'EventTarget', {
cluster,
taskDefinition,
taskCount: 1
});

// Pass an environment variable to the container 'TheContainer' in the task
rule.addTarget(target, {
jsonTemplate: JSON.stringify({
containerOverrides: [{
name: 'TheContainer',
environment: [{ name: 'I_WAS_TRIGGERED', value: 'From CloudWatch Events' }]
}]
})
});
/// !hide

class EventStack extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);

const vpc = new ec2.VpcNetwork(this, 'Vpc', { maxAZs: 1 });

const cluster = new ecs.Cluster(this, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
instanceType: new ec2.InstanceType('t2.micro')
});

/// !show
// Create a Task Definition for the container to start
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
taskDefinition.addContainer('TheContainer', {
image: ecs.ContainerImage.fromAsset(this, 'EventImage', { directory: 'eventhandler-image' }),
memoryLimitMiB: 256,
logging: new ecs.AwsLogDriver(this, 'TaskLogging', { streamPrefix: 'EventDemo' })
});

// An EventRule that describes the event trigger (in this case a scheduled run)
const rule = new events.EventRule(this, 'Rule', {
scheduleExpression: 'rate(1 minute)',
});

// Use Ec2TaskEventRuleTarget as the target of the EventRule
const target = new ecs.Ec2TaskEventRuleTarget(this, 'EventTarget', {
cluster,
taskDefinition,
taskCount: 1
});

// Pass an environment variable to the container 'TheContainer' in the task
rule.addTarget(target, {
jsonTemplate: JSON.stringify({
containerOverrides: [{
name: 'TheContainer',
environment: [{ name: 'I_WAS_TRIGGERED', value: 'From CloudWatch Events' }]
}]
})
});
/// !hide
}
}

new EventStack(app, 'aws-ecs-integ-ecs');
app.run();
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/docker.ts
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ async function calculateImageFingerprint(imageId: string) {
// on every push, causing us to miss all cache hits.
delete manifest.Metadata;

// GraphDriver is about running the image, not about
// GraphDriver is about running the image, not about the image itself.
delete manifest.GraphDriver;

return crypto.createHash('sha256').update(JSON.stringify(manifest)).digest('hex');