AwsLogDriver Class
A log driver that sends log information to CloudWatch Logs.
Example
ts
declare const cluster: ecs.Cluster;
// Create a Task Definition for the container to start
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
taskDefinition.addContainer('TheContainer', {
image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, '..', 'eventhandler-image')),
memoryLimitMiB: 256,
logging: new ecs.AwsLogDriver({ streamPrefix: 'EventDemo', mode: ecs.AwsLogDriverMode.NON_BLOCKING }),
});
// An Rule that describes the event trigger (in this case a scheduled run)
const rule = new events.Rule(this, 'Rule', {
schedule: events.Schedule.expression('rate(1 minute)'),
});
// Pass an environment variable to the container 'TheContainer' in the task
rule.addTarget(new targets.EcsTask({
cluster,
taskDefinition,
taskCount: 1,
containerOverrides: [{
containerName: 'TheContainer',
environment: [{
name: 'I_WAS_TRIGGERED',
value: 'From CloudWatch Events'
}],
}],
}));
python
# cluster: ecs.Cluster
# Create a Task Definition for the container to start
task_definition = ecs.Ec2TaskDefinition(self, "TaskDef")
task_definition.add_container("TheContainer",
image=ecs.ContainerImage.from_asset(path.resolve(__dirname, "..", "eventhandler-image")),
memory_limit_mi_b=256,
logging=ecs.AwsLogDriver(stream_prefix="EventDemo", mode=ecs.AwsLogDriverMode.NON_BLOCKING)
)
# An Rule that describes the event trigger (in this case a scheduled run)
rule = events.Rule(self, "Rule",
schedule=events.Schedule.expression("rate(1 minute)")
)
# Pass an environment variable to the container 'TheContainer' in the task
rule.add_target(targets.EcsTask(
cluster=cluster,
task_definition=task_definition,
task_count=1,
container_overrides=[targets.ContainerOverride(
container_name="TheContainer",
environment=[targets.TaskEnvironmentVariable(
name="I_WAS_TRIGGERED",
value="From CloudWatch Events"
)]
)]
))
java
Cluster cluster;
// Create a Task Definition for the container to start
Ec2TaskDefinition taskDefinition = new Ec2TaskDefinition(this, "TaskDef");
taskDefinition.addContainer("TheContainer", ContainerDefinitionOptions.builder()
.image(ContainerImage.fromAsset(resolve(__dirname, "..", "eventhandler-image")))
.memoryLimitMiB(256)
.logging(AwsLogDriver.Builder.create().streamPrefix("EventDemo").mode(AwsLogDriverMode.NON_BLOCKING).build())
.build());
// An Rule that describes the event trigger (in this case a scheduled run)
Rule rule = Rule.Builder.create(this, "Rule")
.schedule(Schedule.expression("rate(1 minute)"))
.build();
// Pass an environment variable to the container 'TheContainer' in the task
rule.addTarget(EcsTask.Builder.create()
.cluster(cluster)
.taskDefinition(taskDefinition)
.taskCount(1)
.containerOverrides(List.of(ContainerOverride.builder()
.containerName("TheContainer")
.environment(List.of(TaskEnvironmentVariable.builder()
.name("I_WAS_TRIGGERED")
.value("From CloudWatch Events")
.build()))
.build()))
.build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
Cluster cluster;
// Create a Task Definition for the container to start
var taskDefinition = new Ec2TaskDefinition(this, "TaskDef");
taskDefinition.AddContainer("TheContainer", new ContainerDefinitionOptions {
Image = ContainerImage.FromAsset(Resolve(__dirname, "..", "eventhandler-image")),
MemoryLimitMiB = 256,
Logging = new AwsLogDriver(new AwsLogDriverProps { StreamPrefix = "EventDemo", Mode = AwsLogDriverMode.NON_BLOCKING })
});
// An Rule that describes the event trigger (in this case a scheduled run)
var rule = new Rule(this, "Rule", new RuleProps {
Schedule = Schedule.Expression("rate(1 minute)")
});
// Pass an environment variable to the container 'TheContainer' in the task
rule.AddTarget(new EcsTask(new EcsTaskProps {
Cluster = cluster,
TaskDefinition = taskDefinition,
TaskCount = 1,
ContainerOverrides = new [] { new ContainerOverride {
ContainerName = "TheContainer",
Environment = new [] { new TaskEnvironmentVariable {
Name = "I_WAS_TRIGGERED",
Value = "From CloudWatch Events"
} }
} }
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var cluster Cluster
// Create a Task Definition for the container to start
taskDefinition := ecs.NewEc2TaskDefinition(this, jsii.String("TaskDef"))
taskDefinition.AddContainer(jsii.String("TheContainer"), &ContainerDefinitionOptions{
Image: ecs.ContainerImage_FromAsset(path.resolve(__dirname, jsii.String(".."), jsii.String("eventhandler-image"))),
MemoryLimitMiB: jsii.Number(256),
Logging: ecs.NewAwsLogDriver(&AwsLogDriverProps{
StreamPrefix: jsii.String("EventDemo"),
Mode: ecs.AwsLogDriverMode_NON_BLOCKING,
}),
})
// An Rule that describes the event trigger (in this case a scheduled run)
rule := events.NewRule(this, jsii.String("Rule"), &RuleProps{
Schedule: events.Schedule_Expression(jsii.String("rate(1 minute)")),
})
// Pass an environment variable to the container 'TheContainer' in the task
rule.AddTarget(targets.NewEcsTask(&EcsTaskProps{
Cluster: Cluster,
TaskDefinition: TaskDefinition,
TaskCount: jsii.Number(1),
ContainerOverrides: []ContainerOverride{
&ContainerOverride{
ContainerName: jsii.String("TheContainer"),
Environment: []TaskEnvironmentVariable{
&TaskEnvironmentVariable{
Name: jsii.String("I_WAS_TRIGGERED"),
Value: jsii.String("From CloudWatch Events"),
},
},
},
},
}))
Initializer
ts
new AwsLogDriver(props: AwsLogDriverProps)
python
AwsLogDriver(props: AwsLogDriverProps)
java
new AwsLogDriver(AwsLogDriverProps props)
csharp
new AwsLogDriver(AwsLogDriverProps props)
go
NewAwsLogDriver(Props AwsLogDriverProps)
Properties
Methods
awsLogs(props: AwsLogDriverProps): LogDriver@staticmethod def aws_logs(props: AwsLogDriverProps) -> LogDriverpublic static LogDriver awsLogs(AwsLogDriverProps props)public static LogDriver AwsLogs(AwsLogDriverProps props)func AwsLogs(Props AwsLogDriverProps) LogDriverstatic#Creates a log driver configuration that sends log information to CloudWatch Logs.
bind(scope: Construct, containerDefinition: ContainerDefinition): LogDriverConfigdef bind(self, scope: Construct, container_definition: ContainerDefinition) -> LogDriverConfigpublic LogDriverConfig bind(Construct scope, ContainerDefinition containerDefinition)public LogDriverConfig Bind(Construct scope, ContainerDefinition containerDefinition)func Bind(Scope Construct, ContainerDefinition ContainerDefinition) LogDriverConfig#Called when the log driver is configured on a container.