AWS Code Sample
Catalog

integ.event-task.lit.ts

integ.event-task.lit.ts starts an ECS task on an EC2-backed cluster.

// Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. // // This file is licensed under the Apache License, Version 2.0 (the "License"). // You may not use this file except in compliance with the License. A copy of the // License is located at // // http://aws.amazon.com/apache2.0/ // // This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS // OF ANY KIND, either express or implied. See the License for the specific // language governing permissions and limitations under the License. // 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.Ec2EventRuleTarget(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' }] }] }) });

Sample Details

Service: cdk

Last tested: 2019-2-11

Author: Doug-AWS

Type: snippet

On this page: