Skip to content

feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to ApplicationLoadBalancedFargateService#30920

Merged
mergify[bot] merged 36 commits intoaws:mainfrom
ren-yamanashi:20638-feat-add-cpu-memory-props
Feb 3, 2025
Merged

feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to ApplicationLoadBalancedFargateService#30920
mergify[bot] merged 36 commits intoaws:mainfrom
ren-yamanashi:20638-feat-add-cpu-memory-props

Conversation

@ren-yamanashi
Copy link
Copy Markdown
Contributor

@ren-yamanashi ren-yamanashi commented Jul 22, 2024

Issue #20638

Closes #20638

Reason for this change

ApplicationLoadBalancedFargateService did not allow you to specify the CPU and memory of the container.

Description of changes

  • Add containerCpu property to ApplicationLoadBalancedFargateServiceProps
  • Add containerMemoryLimitMiB property to ApplicationLoadBalancedFargateServiceProps

Description of how you validated changes

Added both unit and integration tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 labels Jul 22, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team July 22, 2024 13:33
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jul 22, 2024
Copy link
Copy Markdown
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change the PR title from "feat(application-load-balanced-fargate-service): ..." to "feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to ApplicationLoadBalancedFargateService"? Because we should specify the module name, not the construct name.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jul 23, 2024
@ren-yamanashi ren-yamanashi changed the title feat(application-load-balanced-fargate-service): add containerCpu and containerMemoryLimitMiB property to a ApplicationLoadBalancedFargateService feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to a ApplicationLoadBalancedFargateService Jul 23, 2024
@ren-yamanashi
Copy link
Copy Markdown
Contributor Author

ren-yamanashi commented Jul 23, 2024

@go-to-k

Could you change the PR title from "feat(application-load-balanced-fargate-service): ..." to "feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to ApplicationLoadBalancedFargateService"? Because we should specify the module name, not the construct name.

Thank you for the suggestion. I have updated the PR title. Please check the changes at your convenience.

@ren-yamanashi ren-yamanashi changed the title feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to a ApplicationLoadBalancedFargateService feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to ApplicationLoadBalancedFargateService Jul 23, 2024
Copy link
Copy Markdown
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! I made some comments.

@ren-yamanashi
Copy link
Copy Markdown
Contributor Author

ren-yamanashi commented Jul 25, 2024

@go-to-k

Thanks for the review! I have fixed it, so please check it out!

(A link to the fix commit is provided in reply to the thread.)

Copy link
Copy Markdown
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the containerCpu and the containerMemoryLimitMiB are larger than (or equal to) the cpu and the memoryLimitMiB, will CloudFormation deploy errors occur?
If so, it is good to add validations for them. (and also good to add corresponding unit tests)

for example:

      if (
        props.containerCpu &&
        !Token.isUnresolved(props.containerCpu) &&
        props.cpu &&
        !Token.isUnresolved(props.cpu) &&
        props.containerCpu > props.cpu // or `props.containerCpu >= props.cpu`
      ) {
        throw new Error('containerCpu must be less than or equal to cpu');
      }
      // The same applies to memory.
      // ...

see: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts#L108-L119

As with the validateHealthyPercentage method, it may also be possible to check for values that are not negative. (If CFn fails for negative values.)

This was referenced Jul 29, 2024
}
}

private validateContainerCpu(containerCpu?: number, cpu: number = 256) { // default value for cpu is 256
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are these defaults defined ?

Copy link
Copy Markdown
Contributor Author

@ren-yamanashi ren-yamanashi Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shikha372

I used this default value as defined in FargateTaskDefinitionProps.

See:

Should I write the code as follows?

  private validateContainerCpu(containerCpu?: number, cpu: number = 256) { // default value for FargateTaskDefinitionProps.cpu is 256

Copy link
Copy Markdown
Contributor

@shikha372 shikha372 Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ren-yamanashi , I see the default values are in fargate task definition and not being set under props.cpu of ApplicationLoadBalancer class, I would prefer if we keep the check without default value as we might miss the update in case it changes in future as well.

Copy link
Copy Markdown
Contributor Author

@ren-yamanashi ren-yamanashi Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shikha372

Thank you for your comment.

In the current validateContainerCpu method, it compares the values of containerCpu and cpu and returns an error if containerCpu exceeds cpu.

As you pointed out, referencing the default value of FargateServiceBaseProps.cpu in ApplicationLoadBalancedFargateService’s validateContainerCpu could indeed lead to an issue where we might miss the update in case it changes in future as well.

However, avoiding the use of the default value of FargateServiceBaseProps.cpu within ApplicationLoadBalancedFargateService to address this concern could lead to the following problem:

validateContainerCpu method might not validate properly

If we don’t reference the default value of FargateServiceBaseProps.cpu, the cpu param in validateContainerCpu would need to be optional.
(because props.cpu is optional)

  private validateContainerCpu(containerCpu?: number, cpu?: number) {
    /**
     * ... Implementation of the method
     */
  }

In this scenario, it becomes challenging to validate cases where:

  • containerCpu is set, but cpu is not.

In this cases, an error should be returned if containerCpu exceeds the default value of FargateServiceBaseProps.cpu (256), but without referencing the default value within the method, determining if an error should be returned becomes difficult.


Possible Approaches for Resolution

Taking these considerations into account, I suggest the following approach. What could you think?
Also, if there are any other good ways, could you please let me know?

Note: this approaches would result in referencing the default value of FargateServiceBaseProps.cpu in ApplicationLoadBalancedFargateService, but I thought it could be more appropriate than the current implementation, so I am suggesting them.

Override props.cpu in ApplicationLoadBalancedFargateServiceProps

override props.cpu in ApplicationLoadBalancedFargateServiceProps and pass it to the FargateTaskDefinition argument within ApplicationLoadBalancedFargateService, as shown below:

export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationLoadBalancedServiceBaseProps, FargateServiceBaseProps {
  /**
   * ... some comments
   *
   * @default 256
   */
  readonly cpu?: number;

 /**
  * ... some props
  */
}

export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalancedServiceBase {
  // ... some implementation
  constructor(scope: Construct, id: string, props: ApplicationLoadBalancedFargateServiceProps = {}) {
    // ... some implementation

      this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', {
        cpu: props.cpu ?? 256,
        // ... some props
      });
  }
}   

Copy link
Copy Markdown
Contributor

@shikha372 shikha372 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ren-yamanashi for defining the alternate approach in detail, I would not modify the current behaviour of the construct by setting the default for props.cpu, I believe in case, value of containerCpu and cpu doesn't align with each other, this would result in a failed deployment (please let me know if that is not the case), given that if intention by adding this validation is to fail fast before the deployment itself, can we add a warning instead of an error in the function so that in case these values change in near future it will not block customer deployments.

Copy link
Copy Markdown
Contributor Author

@ren-yamanashi ren-yamanashi Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shikha372

I apologize for the delay in responding to you.

If the values of containerCpu and cpu do not match, no error occurs, and the error occurs if the value specified for containerCpu is greater than cpu.

given that if intention by adding this validation is to fail fast before the deployment itself, can we add a warning instead of an error in the function so that in case these values change in near future it will not block customer deployments.

Perhaps the assumed behavior is,

  • When the default value of cpu is changed and the containerCpu value becomes an incorrect value, a warning is issued without changing the implementation.

I think it, is this correct?

Currently, if the default value of cpu is changed, it is unclear what the cpu value actually specified when this validation is executed.

Therefore, I think that such an implementation would be difficult (I just haven't thought of it, but there may be a better idea...)

As an alternative, Considering the concerns you mentioned, I thought it would be better to allow containerCpu to be specified only if cpu is specified. What are your thoughts on this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ren-yamanashi , discussed with @shikha372 and the suggestion is to validate using this.taskDefinition.cpu since FargateTaskDefinition sets a default for cpu. i.e.

this.validateContainerCpu(props.containerCpu, this.taskDefinition.cpu);

It looks like we will need to surface cpu in the FargateTaskDefinition class as a public readonly member in order to do this. Let us know if you're still planning to work on this, otherwise we can take over to get it merged (we will make sure to credit you as a co-author :) ). Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gracelu0

Thank you for review and suggestions on a clear implementation policy!
I will modify the implementation on my part!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gracelu0

I have fixed it, so please confirm!

40fb61f

@shikha372 shikha372 self-assigned this Sep 25, 2024
@codecov
Copy link
Copy Markdown

codecov bot commented Nov 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.84%. Comparing base (fd9462c) to head (f6993ab).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #30920   +/-   ##
=======================================
  Coverage   80.84%   80.84%           
=======================================
  Files         236      236           
  Lines       14230    14230           
  Branches     2487     2487           
=======================================
  Hits        11504    11504           
  Misses       2442     2442           
  Partials      284      284           
Flag Coverage Δ
suite.unit 80.84% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 79.64% <ø> (ø)
packages/aws-cdk-lib/core 82.14% <ø> (ø)

Copy link
Copy Markdown
Contributor

@shikha372 shikha372 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ren-yamanashi for defining the alternate approach in detail, I would not modify the current behaviour of the construct by setting the default for props.cpu, I believe in case, value of containerCpu and cpu doesn't align with each other, this would result in a failed deployment (please let me know if that is not the case), given that if intention by adding this validation is to fail fast before the deployment itself, can we add a warning instead of an error in the function so that in case these values change in near future it will not block customer deployments.

@shikha372 shikha372 removed their assignment Dec 9, 2024
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 9, 2024
@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@mergify mergify bot dismissed shikha372’s stale review January 6, 2025 00:26

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jan 6, 2025
}
}

private validateContainerCpu(containerCpu?: number, cpu: number = 256) { // default value for cpu is 256
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ren-yamanashi , discussed with @shikha372 and the suggestion is to validate using this.taskDefinition.cpu since FargateTaskDefinition sets a default for cpu. i.e.

this.validateContainerCpu(props.containerCpu, this.taskDefinition.cpu);

It looks like we will need to surface cpu in the FargateTaskDefinition class as a public readonly member in order to do this. Let us know if you're still planning to work on this, otherwise we can take over to get it merged (we will make sure to credit you as a co-author :) ). Thanks!

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Feb 1, 2025
@mergify mergify bot dismissed gracelu0’s stale review February 1, 2025 02:54

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Feb 1, 2025
gracelu0
gracelu0 previously approved these changes Feb 3, 2025
Copy link
Copy Markdown
Contributor

@gracelu0 gracelu0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing!

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 3, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

1 similar comment
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 3, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@gracelu0
Copy link
Copy Markdown
Contributor

gracelu0 commented Feb 3, 2025

@Mergifyio update

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 3, 2025

update

❌ Mergify doesn't have permission to update

Details

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/analytics-metadata-updater.yml without workflows permission

@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: f6993ab
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 3, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 3, 2025

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(aws_ecs_patterns): container-level cpu & memory props for ApplicationLoadBalancedFargateService

6 participants