feat(ecs-patterns): add containerCpu and containerMemoryLimitMiB property to ApplicationLoadBalancedFargateService#30920
Conversation
packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts
Show resolved
Hide resolved
go-to-k
left a comment
There was a problem hiding this comment.
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.
containerCpu and containerMemoryLimitMiB property to a ApplicationLoadBalancedFargateServicecontainerCpu and containerMemoryLimitMiB property to a ApplicationLoadBalancedFargateService
Thank you for the suggestion. I have updated the PR title. Please check the changes at your convenience. |
containerCpu and containerMemoryLimitMiB property to a ApplicationLoadBalancedFargateServicecontainerCpu and containerMemoryLimitMiB property to ApplicationLoadBalancedFargateService
go-to-k
left a comment
There was a problem hiding this comment.
Thanks for this PR! I made some comments.
packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts
Outdated
Show resolved
Hide resolved
packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts
Show resolved
Hide resolved
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
|
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.) |
There was a problem hiding this comment.
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.
// ...As with the validateHealthyPercentage method, it may also be possible to check for values that are not negative. (If CFn fails for negative values.)
| } | ||
| } | ||
|
|
||
| private validateContainerCpu(containerCpu?: number, cpu: number = 256) { // default value for cpu is 256 |
There was a problem hiding this comment.
where are these defaults defined ?
There was a problem hiding this comment.
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 256There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
containerCpuis 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
});
}
} There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Thank you for review and suggestions on a clear implementation policy!
I will modify the implementation on my part!
…20638-feat-add-cpu-memory-props
…ren-yamanashi/aws-cdk into 20638-feat-add-cpu-memory-props
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
shikha372
left a comment
There was a problem hiding this comment.
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.
|
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. |
Pull request has been modified.
…-add-cpu-memory-props
| } | ||
| } | ||
|
|
||
| private validateContainerCpu(containerCpu?: number, cpu: number = 256) { // default value for cpu is 256 |
There was a problem hiding this comment.
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!
…-add-cpu-memory-props
Pull request has been modified.
gracelu0
left a comment
There was a problem hiding this comment.
Thank you for contributing!
|
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
|
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). |
|
@Mergifyio update |
❌ Mergify doesn't have permission to updateDetailsFor security reasons, Mergify can't update this pull request. Try updating locally. |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
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). |
|
Comments on closed issues and PRs are hard for our team to see. |
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
containerCpuproperty toApplicationLoadBalancedFargateServicePropscontainerMemoryLimitMiBproperty toApplicationLoadBalancedFargateServicePropsDescription 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