Skip to content

(aws-ecs-patterns): support AvailabilityZoneRebalancing #34442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks
MorelSerge opened this issue May 13, 2025 · 1 comment · May be fixed by #34529
Open
2 tasks

(aws-ecs-patterns): support AvailabilityZoneRebalancing #34442

MorelSerge opened this issue May 13, 2025 · 1 comment · May be fixed by #34529
Labels
@aws-cdk/aws-ecs-patterns Related to ecs-patterns library feature-request A feature should be added or improved. p2

Comments

@MorelSerge
Copy link

Describe the feature

The current ecs patterns, such as ApplicationLoadBalancedFargateService, have their rebalancing set to the default, which is disabled. It cannot be updated on the FargateService itself.

Use Case

AWS just spread an email around, saying that new ECS services will automatically have this set to enabled. It'd be good that we can also enable this for existing services, or explicitly disable for new ones if that's not desired.

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS CDK Library version (aws-cdk-lib)

2.187.0

AWS CDK CLI version

2.1003.0

Environment details (OS name and version, etc.)

MacOS 14

@MorelSerge MorelSerge added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels May 13, 2025
@github-actions github-actions bot added the @aws-cdk/aws-ecs-patterns Related to ecs-patterns library label May 13, 2025
@ykethan
Copy link
Contributor

ykethan commented May 13, 2025

Hey @MorelSerge, thank you for reaching out. I believe you are referring to this announcement.

The AvailabilityZoneRebalancing feature is supported in the underlying Fargate service but is not exposed through any of the ECS patterns (like ApplicationLoadBalancedFargateService).

Current Implementation

The underlying FargateService construct supports availabilityZoneRebalancing:

// In packages/aws-cdk-lib/aws-ecs/lib/fargate/fargate-service.ts
export interface FargateServiceProps extends BaseServiceOptions {
  // ... other props
  /**
   * Whether to use Availability Zone rebalancing for the service.
   *
   * @default AvailabilityZoneRebalancing.DISABLED
   */
  readonly availabilityZoneRebalancing?: AvailabilityZoneRebalancing;
}

Workaround

On a quick test, it does appear users can access the underlying service property after creating the pattern, but this requires using escape hatches:

// Create the service pattern
const fargateService = new ApplicationLoadBalancedFargateService(/* props */);

// Access the CloudFormation resource of the underlying service
const cfnService = fargateService.service.node.defaultChild as CfnService;

// Set the availability zone rebalancing property
cfnService.availabilityZoneRebalancing = 'ENABLED';

Implementation

Of the top the implementation approach could be:

  1. Add availabilityZoneRebalancing to FargateServiceBaseProps interface

First, we would modify packages/aws-cdk-lib/aws-ecs-patterns/lib/base/fargate-service-base.ts:

// packages/aws-cdk-lib/aws-ecs-patterns/lib/base/fargate-service-base.ts

import { FargatePlatformVersion, FargateTaskDefinition, RuntimePlatform, AvailabilityZoneRebalancing } from '../../../aws-ecs';

export interface FargateServiceBaseProps {
  /**
   * The task definition to use for tasks in the service. TaskDefinition or TaskImageOptions must be specified, but not both.
   *
   * [disable-awslint:ref-via-interface]
   *
   * @default - none
   */
  readonly taskDefinition?: FargateTaskDefinition;

  // ... existing properties ...

  /**
   * Whether to use Availability Zone rebalancing for the service.
   *
   * If enabled, `maxHealthyPercent` must be greater than 100, and the service must not be a target
   * of a Classic Load Balancer.
   *
   * @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-rebalancing.html
   * @default AvailabilityZoneRebalancing.DISABLED
   */
  readonly availabilityZoneRebalancing?: AvailabilityZoneRebalancing;
}
  1. Update ApplicationLoadBalancedFargateService to pass the property

Then modify packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts:

// packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts

constructor(scope: Construct, id: string, props: ApplicationLoadBalancedFargateServiceProps = {}) {
  super(scope, id, props);

  this.assignPublicIp = props.assignPublicIp ?? false;

  // ... existing code ...

  const desiredCount = FeatureFlags.of(this).isEnabled(cxapi.ECS_REMOVE_DEFAULT_DESIRED_COUNT) ? this.internalDesiredCount : this.desiredCount;

  this.service = new FargateService(this, 'Service', {
    cluster: this.cluster,
    desiredCount: desiredCount,
    taskDefinition: this.taskDefinition,
    assignPublicIp: this.assignPublicIp,
    serviceName: props.serviceName,
    healthCheckGracePeriod: props.healthCheckGracePeriod,
    minHealthyPercent: props.minHealthyPercent,
    maxHealthyPercent: props.maxHealthyPercent,
    propagateTags: props.propagateTags,
    enableECSManagedTags: props.enableECSManagedTags,
    cloudMapOptions: props.cloudMapOptions,
    platformVersion: props.platformVersion,
    deploymentController: props.deploymentController,
    circuitBreaker: props.circuitBreaker,
    securityGroups: props.securityGroups,
    vpcSubnets: props.taskSubnets,
    enableExecuteCommand: props.enableExecuteCommand,
    capacityProviderStrategies: props.capacityProviderStrategies,
    availabilityZoneRebalancing: props.availabilityZoneRebalancing, // Pass through the property
  });
  this.addServiceAsTarget(this.service);
}

Similar changes may need to be made to other ECS patterns that create Fargate services, like:

  • NetworkLoadBalancedFargateService
  • QueueProcessingFargateService
  • ScheduledFargateTask

Marking this as P2 feature request.
We greatly appreciate community contributions! If you or someone else is interested in implementing this, we'd be happy to review a Pull Request. Please check our contribution guidelines before starting.

We'll leave this issue open to track the request. Meanwhile, welcome to add 👍 to help us prioritize.

@ykethan ykethan added p2 and removed needs-triage This issue or PR still needs to be triaged. labels May 13, 2025
@dave9011 dave9011 linked a pull request May 22, 2025 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs-patterns Related to ecs-patterns library feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants