Skip to content

(ecs-patterns): ScheduledFargateTask - cpu and memoryLimitMiB construct props are ignored #26702

@johnfox

Description

@johnfox

Describe the bug

When constructing ScheduledFargateTask using workerTaskImageOptions the provided cpu and memoryLimitMiB construct props values are ignored and default values are used in the resulting CloudFormation template.

Expected Behavior

The resulting CloudFormation template should use values provided by construct props object.

Current Behavior

Default values are used.

Reproduction Steps

Code snippet:

    const workerTaskImageOptions = {
      image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
      containerName: "worker_container",
    }

    const scheduledWorkerService = new ecs_patterns.ScheduledFargateTask(this, 'ApiEcsFargateScheduledWorkerService', {
      cluster,
      scheduledFargateTaskImageOptions: workerTaskImageOptions,
      cpu: 1024,
      memoryLimitMiB: 2048,
      desiredTaskCount: 1,
      schedule: appScaling.Schedule.expression('cron(0/10 * * * ? *)'),
    });
  }

Resulting CloudFormation template.json file snipped:

"ApiEcsFargateScheduledWorkerServiceScheduledTaskDefEFA35B6E": {
   "Type": "AWS::ECS::TaskDefinition",
   "Properties": {
    "ContainerDefinitions": [
     {
      "Essential": true,
      "Image": "amazon/amazon-ecs-sample",
      "LogConfiguration": {
       "LogDriver": "awslogs",
       "Options": {
        "awslogs-group": {
         "Ref": "ApiEcsFargateScheduledWorkerServiceScheduledTaskDefScheduledContainerLogGroup495C0615"
        },
        "awslogs-stream-prefix": "ApiEcsFargateScheduledWorkerService",
        "awslogs-region": {
         "Ref": "AWS::Region"
        }
       }
      },
      "Name": "ScheduledContainer"
     }
    ],
    "Cpu": "256",
    "ExecutionRoleArn": {
     "Fn::GetAtt": [
      "ApiEcsFargateScheduledWorkerServiceScheduledTaskDefExecutionRoleFFF995A1",
      "Arn"
     ]
    },
    "Family": "FargateEcsScheduledTaskStackApiEcsFargateScheduledWorkerServiceScheduledTaskDef8DA8C987",
    "Memory": "512",
    "NetworkMode": "awsvpc",
    "RequiresCompatibilities": [
     "FARGATE"
    ],
    "TaskRoleArn": {
     "Fn::GetAtt": [
      "ApiEcsFargateScheduledWorkerServiceScheduledTaskDefTaskRole989BFDD3",
      "Arn"
     ]
    }
   },
   "Metadata": {
    "aws:cdk:path": "FargateEcsScheduledTaskStack/ApiEcsFargateScheduledWorkerService/ScheduledTaskDef/Resource"
   }
  },

Possible Solution

No response

Additional Information/Context

When trying to define the scheduled task by taskDefinition (as per documentation) attribute:

    const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDefinition', {
      cpu: 1024,
      memoryLimitMiB: 2048,
    });

    taskDefinition.addContainer('test-service', {
      image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
    });

    const scheduledWorkerService = new ecs_patterns.ScheduledFargateTask(this, 'ApiEcsFargateScheduledWorkerService', {
      cluster,
      taskDefinition,
      desiredTaskCount: 1,
      schedule: appScaling.Schedule.expression('cron(0/10 * * * ? *)'),
    });

I'm getting CDK error:

Error: You must specify one of: taskDefinition or image
    at new ScheduledFargateTask (..../aws-cdk-bug-reports/FargateEcsScheduledTask/node_modules/aws-cdk-lib/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.js:1:1628)
    at new FargateEcsScheduledTaskStack (..../aws-cdk-bug-reports/FargateEcsScheduledTask/lib/fargate_ecs_scheduled_task-stack.ts:67:36)
    at Object.<anonymous> (..../aws-cdk-bug-reports/FargateEcsScheduledTask/bin/fargate_ecs_scheduled_task.ts:7:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module.m._compile (..../aws-cdk-bug-reports/FargateEcsScheduledTask/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .ts] (..../aws-cdk-bug-reports/FargateEcsScheduledTask/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Documentation (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs_patterns.ScheduledFargateTaskDefinitionOptions.html) :

// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import { aws_ecs as ecs } from 'aws-cdk-lib';
import { aws_ecs_patterns as ecs_patterns } from 'aws-cdk-lib';

declare const fargateTaskDefinition: ecs.FargateTaskDefinition;
const scheduledFargateTaskDefinitionOptions: ecs_patterns.ScheduledFargateTaskDefinitionOptions = {
  taskDefinition: fargateTaskDefinition,
};

Possible bug?

CDK CLI Version

2.84.0 (build f7c792f)

Framework Version

2.63.2

Node.js Version

18.16.0

OS

Max OS X

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ecs-patternsRelated to ecs-patterns librarybugThis issue is a bug.effort/smallSmall work item – less than a day of effortgood first issueRelated to contributions. See CONTRIBUTING.mdp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions