Skip to content

aws_ecs: ec2Service placement strategies use incorrect casing which causes drift #20812

@tea-418

Description

@tea-418

Describe the bug

When adding the packedByCpu() PlacementStrategy to a new ecs service, the cf template generated has the Property

"PlacementStrategies": [{
    "Field": "cpu",
    "Type": "binpack"
}]

However this will cause the Stack to drift as CF only allows CPU spelled in uppercase.
See here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ecs-service-placementstrategy.html

Expected Behavior

CDK should generate the "Field" attribute of "PlacementStrategies" according to CF demands.

In the CF Template PlacementStrategies should look like this:

"PlacementStrategies": [{
    "Field": "CPU",
    "Type": "binpack"
}],

Current Behavior

CDK generates the "Field" attribute of "PlacementStrategies" NOT according to CF demands but instead spells it in lowercase.

In the CF Template PlacementStrategies actually looks like this:

"PlacementStrategies": [{
    "Field": "cpu",
     "Type": "binpack"
}],

Reproduction Steps

  1. Generate a fresh cdk project with cdk init app --language=typescript -j

  2. in the lib/project-name-stack.ts add a ecs service to a cluster with the PlacementStrategy set:

import { aws_ecs, aws_ec2, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';

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

    const vpc = new aws_ec2.Vpc(this, 'Vpc', { maxAzs: 2 });

    const cluster = new aws_ecs.Cluster(this, 'EcsCluster', { vpc });
    cluster.addCapacity('DefaultAutoScalingGroup', {
      instanceType: aws_ec2.InstanceType.of(aws_ec2.InstanceClass.T2, aws_ec2.InstanceSize.MICRO)
    });

    const taskDefinition = new aws_ecs.Ec2TaskDefinition(this, 'TaskDef');
    const container = taskDefinition.addContainer('web', {
      image: aws_ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
      memoryLimitMiB: 256,
    });

    const service = new aws_ecs.Ec2Service(this, "Test_Stack",{
      cluster,
      taskDefinition,
      placementStrategies: [
        aws_ecs.PlacementStrategy.packedByCpu(), // <--- here
      ]
    });
  }
}
  1. run cdk synth

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.28.1 (build d035432)

Framework Version

No response

Node.js Version

v16.14.2

OS

Windows 10 Enterprise 21H2

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-ecsRelated to Amazon Elastic ContainerbugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions