CodexBloom - Programming Q&A Platform

AWS CDK with ECS Fargate: Task Definition Resource Limit Not Applied

๐Ÿ‘€ Views: 0 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-15
aws cdk ecs fargate TypeScript

I'm sure I'm missing something obvious here, but I'm using AWS CDK (version 2.34.0) to deploy a Fargate service, but it seems the resource limits for my task definition aren't being recognized by ECS. I defined a Fargate task with both CPU and memory limits set, but when I check the ECS console, the task definition shows the default values instead of what I specified. Hereโ€™s a snippet of my CDK code: ```typescript import * as cdk from 'aws-cdk-lib'; import * as ecs from 'aws-cdk-lib/aws-ecs'; const app = new cdk.App(); const stack = new cdk.Stack(app, 'MyEcsStack'); const cluster = new ecs.Cluster(stack, 'MyCluster', { vpc: myVpc, }); const taskDefinition = new ecs.FargateTaskDefinition(stack, 'MyTaskDefinition', { memoryLimitMiB: 512, cpu: 256, }); const container = taskDefinition.addContainer('MyContainer', { image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), logging: new ecs.AwsLogDriver({ streamPrefix: 'MyLogs' }), }); new ecs.FargateService(stack, 'MyService', { cluster, taskDefinition, desiredCount: 1, }); app.synth(); ``` Iโ€™ve also verified that my VPC and security group settings are correctly configured, and Iโ€™ve deployed this stack multiple times. While deploying, there are no errors in the console, but the task definition in the AWS Management Console does not reflect the specified CPU and memory limits. It still shows the default values (CPU: 256, Memory: 512) when I check the task details. Iโ€™ve tried explicitly setting the `memoryLimitMiB` and `cpu` in the `FargateTaskDefinition`, but it doesn't seem to have any effect. Is there something I'm missing, or is there a known scenario with the CDK version I'm using? Any advice on how to get the correct resource limits applied would be greatly appreciated! My development environment is Linux. What's the best practice here?