1

I am trying to add an existing log group to my ECS scheduled task to avoid creation of new log groups with random name. But the aws-cdk (typescript) keeps giving me error that the types are not assignable.

const myLogGroup =  LogGroup.fromLogGroupArn(this, 'MyLogGroup', 'log-group-arn')

const logging = ecs.LogDrivers.awsLogs({
      streamPrefix: 'mylg',
      logGroup: myLogGroup
});

logGroup: myLogGroup gives the error:

Type 'import("/Users/praveen/code/cron-cdk/node_modules/@aws-cdk/aws-logs/lib/log-group").ILogGroup' is not assignable to type 'import("/Users/praveen/code/cron-cdk/node_modules/@aws-cdk/aws-ec2/node_modules/@aws-cdk/aws-logs/lib/log-group").ILogGroup'.
  The types of 'addStream(...).stack.tags' are incompatible between these types.
    Type 'import("/Users/praveen/code/cron-cdk/node_modules/@aws-cdk/aws-logs/node_modules/@aws-cdk/core/lib/tag-manager").TagManager' is not assignable to type 'import("/Users/praveen/code/cron-cdk/node_modules/@aws-cdk/core/lib/tag-manager").TagManager'.
      Types have separate declarations of a private property 'tags'.ts(2322)
aws-log-driver.d.ts(51, 14): The expected type comes from property 'logGroup' which is declared here on type 'AwsLogDriverProps'
(property) AwsLogDriverProps.logGroup?: ILogGroup | undefined

Any thoughts how can I use my existing log group for this? My scheduled task definition is this -

new ScheduledFargateTask(this, 'cleanup', {
      cluster,
      scheduledFargateTaskImageOptions: {
        image: ecs.ContainerImage.fromAsset('scripts/cleanup'),
        memoryLimitMiB: 512,
        logDriver: logging,
      },
      schedule: events.Schedule.expression('rate(12 hours)'),
      platformVersion: ecs.FargatePlatformVersion.LATEST,
    });

1 Answers1

2

The below code compiles and creates the CloudFormation correctly via cdk synth.

I created the below from a basic CDK project using cdk init app --language typescript and then added the required dependencies to the package.json. I'd recommend verify all your CDK dependencies are of the same version and that your imports are correct.

Dependencies:

"@aws-cdk/aws-ec2": "^1.117.0",
"@aws-cdk/aws-ecs": "^1.117.0",
"@aws-cdk/aws-ecs-patterns": "^1.117.0",
"@aws-cdk/aws-events": "^1.117.0",
"@aws-cdk/aws-logs": "^1.118.0",
"@aws-cdk/core": "1.117.0"

Code:

import { ContainerImage, FargatePlatformVersion, LogDrivers, Cluster } from '@aws-cdk/aws-ecs';
import { ScheduledFargateTask } from '@aws-cdk/aws-ecs-patterns';
import { Schedule } from '@aws-cdk/aws-events';
import { LogGroup } from '@aws-cdk/aws-logs';

import * as cdk from '@aws-cdk/core';

export class LogGroupsStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myLogGroup = LogGroup.fromLogGroupArn(this, 'MyLogGroup', 'arn:partition:service:region:account-id:resource-id')

    const logging = LogDrivers.awsLogs({
      streamPrefix: 'mylg',
      logGroup: myLogGroup
    });

    const cluster = new Cluster(this, "MyCluster")

    new ScheduledFargateTask(this, 'cleanup', {
      cluster,
      scheduledFargateTaskImageOptions: {
        image: ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
        memoryLimitMiB: 512,
        logDriver: logging,
      },
      schedule: Schedule.expression('rate(12 hours)'),
      platformVersion: FargatePlatformVersion.LATEST
    });
  }
}
Garreth Golding
  • 985
  • 2
  • 11
  • 19
  • 1
    Thanks, this works perfectly. Just needed to delete the `package-lock.json` and `node_modules` folder and running `npm i` to get everything working again. – Praveen Kumar Aug 14 '21 at 10:20