feat(apprunner): add AutoScalingConfiguration for AppRunner Service#30358
feat(apprunner): add AutoScalingConfiguration for AppRunner Service#30358mergify[bot] merged 24 commits intoaws:mainfrom
Conversation
1e25c91 to
0a5114b
Compare
lpizzinidev
left a comment
There was a problem hiding this comment.
Thanks 👍 Left some comments for minor adjustments
| /** | ||
| * Properties of the App Runner Auto Scaling Configuration. | ||
| */ | ||
| export interface AutoScalingConfigurationProps { |
There was a problem hiding this comment.
Looking at the other L2 Constructs, it seemed like there were few resources adding tags as properties, so I didn't add them.
Since tags can also be added through aspects, I thought they might be unnecessary like other resources. What do you think?
I would appreciate your opinion on the criteria for adding tags, as I'm not sure about it.
There was a problem hiding this comment.
Makes sense 👍 Thanks for clarifying!
| super(scope, id, { | ||
| physicalName: props.autoScalingConfigurationName, | ||
| }); | ||
|
|
There was a problem hiding this comment.
Can you also add validation for the autoScalingConfigurationName property (docs)?
There was a problem hiding this comment.
Thanks. I added validation and unit tests.
Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
…ration.ts Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
…ration.ts Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
…uration.test.ts Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
…ration.ts Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
|
@lpizzinidev |
lpizzinidev
left a comment
There was a problem hiding this comment.
Thanks 👍 Left suggestions for minor adjustments on validation
| /** | ||
| * Properties of the App Runner Auto Scaling Configuration. | ||
| */ | ||
| export interface AutoScalingConfigurationProps { |
There was a problem hiding this comment.
Makes sense 👍 Thanks for clarifying!
…ration.ts Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
…ration.ts Co-authored-by: Luca Pizzini <lpizzini7@gmail.com>
e5067dd to
f46a7c7
Compare
|
@lpizzinidev |
|
@pahud |
pahud
left a comment
There was a problem hiding this comment.
some minor suggested changes
| * The revision of the Auto Scaling Configuration. | ||
| * @attribute | ||
| */ | ||
| readonly autoScalingConfigurationRevision: number; |
There was a problem hiding this comment.
I guess we probably should make it string. Just like ecs task definition.
Also in the doc
AutoScalingConfigurationRevision
The revision of this auto scaling configuration. It's unique among all the active configurations that share the same AutoScalingConfigurationName.
It's unclear to me if it would be number. I am afraid some day it might support revision like latest just like ecs task revision and this would be a breaking change in CDK.
With that being said, I don't see the benefits using number over string. wdyt?
There was a problem hiding this comment.
Thank you. Personally, I'm in favor of making the revision a string.
However, I have some concerns.
The revision in the Cfn (L1 construct) is a number, but would it be appropriate to change it to a string in the L2 construct? I'm not sure if there's a possibility of it changing, so if you know anything about it, please let me know.
Additionally, since the already implemented VpcConnector treats the revision as a number, I'm also concerned about having a different approach from that.
There was a problem hiding this comment.
Thank you for the callout.
I checked the cfnspec and yes the type is Integer, which is number in TS.
"Attributes": {
"AutoScalingConfigurationRevision": {
"PrimitiveType": "Integer"
},
I am fine having it as number for consistency here but I kind of feel if apprunner supports latest revision someday, which would be a breaking change to CFN then CDK would have breaking change as well.
@GavinZZ @paulhcsun what do you think?
There was a problem hiding this comment.
Discussed with team internally. I think we don't need to over-optimize the type here. CFN schema type changes are not supposed to happen especially after it's already in CloudFormation resource schema registry.
| ) { | ||
| throw new Error(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}`); | ||
| } | ||
|
|
There was a problem hiding this comment.
Consider the code like this
function validateAutoScalingConfiguration(props: {
minSize?: number;
maxSize?: number;
maxConcurrency?: number;
}) {
const isMinSizeDefined = typeof props.minSize === 'number';
const isMaxSizeDefined = typeof props.maxSize === 'number';
const isMaxConcurrencyDefined = typeof props.maxConcurrency === 'number';
if (isMinSizeDefined && (props.minSize < 1 || props.minSize > 25)) {
throw new Error(`minSize must be between 1 and 25, got ${props.minSize}`);
}
if (isMaxSizeDefined && (props.maxSize < 1 || props.maxSize > 25)) {
throw new Error(`maxSize must be between 1 and 25, got ${props.maxSize}`);
}
if (isMinSizeDefined && isMaxSizeDefined && !(props.minSize < props.maxSize)) {
throw new Error('maxSize must be greater than minSize');
}
if (isMaxConcurrencyDefined && (props.maxConcurrency < 1 || props.maxConcurrency > 200)) {
throw new Error(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}`);
}
}The main changes are:
-
Extracted the validation logic into a separate function
validateAutoScalingConfigurationto make the code more modular and easier to test. -
Used
typeofto check if the properties are defined and a number as well as!cdk.Token.isUnresolvedbecause if it is unresolved it would not be number? I am not 100% sure here maybe you still need
typeof props.minSize === 'number' && !cdk.Token.isUnresolved(props.minSize);- Moved the
isMaxConcurrencyDefinedcheck into a separate condition to make the code more readable.
There are a few benefits to using typeof to check if the properties are defined and a number, instead of using
undefined and cdk.Token.isUnresolved:
-
Simplicity and Readability: The
typeofchecks are more straightforward and easier to understand at a glance. They directly check the type of the value, which makes the code more self-documenting and easier to maintain. -
Handling Unresolved Tokens: The
cdk.Token.isUnresolvedcheck is specifically for handling unresolved tokens in the AWS CDK. However, in this case, we're not just checking for unresolved tokens, but also ensuring the property is defined and a number. Usingtypeofhandles all of these cases in a single, simple check. [1] -
Robustness: The
typeofchecks are more robust because they handle a wider range of cases. For example, if the property is set tonull, thetypeofcheck will still correctly identify it as not a number, whereas the
undefinedcheck would not catch this case. -
Performance: The
typeofchecks are generally faster than usingundefinedandcdk.Token.isUnresolved
, as they are simpler operations. This can be especially beneficial in performance-critical code. -
Consistency: Using
typeofto check the type of a value is a common and widely-accepted pattern in JavaScript/TypeScript development. It aligns with the language's standard practices and makes the code more familiar to other developers.
In summary, the typeof checks provide a more straightforward, robust, and performant way to validate the properties, while also making the code more readable and consistent with standard JavaScript/TypeScript practices. This can lead to better maintainability and easier understanding of the validation logic.
This optimized version of the code is more concise, easier to read, and easier to maintain. It also follows best practices for input validation, such as using clear error messages and keeping the validation logic separate from the main application logic.
There was a problem hiding this comment.
I would consider to make it a construct private method and use something like this.validateAutoScalingConfiguration() for all the validations, it's very opinionated and not mandatory though.
There was a problem hiding this comment.
Thanks. I like your suggested your approach.
I've created a private method including autoScalingConfigurationName validation.
…uration.test.ts Co-authored-by: Pahud Hsieh <pahudnet@gmail.com>
…uration.test.ts Co-authored-by: Pahud Hsieh <pahudnet@gmail.com>
|
@pahud |
|
LGTM now! I'll let other maintainers to take a final review. |
|
Thanks Pahud for the thorough reviews and thanks @mazyu36 for the PR and quick responses. |
|
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 |
☑️ Nothing to doDetails
|
|
@Mergifyio refresh |
✅ Pull request refreshed |
|
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). |
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). |
…ws#30358) ### Issue # (if applicable) Closes aws#30353 . ### Reason for this change At the moment, L2 Construct does not support a custom auto scaling configuration for the AppRunner Service. ### Description of changes * Add `AutoScalingConfiguration` Class * Add `autoScalingConfiguration` property to the `Service` Class ### Description of how you validated changes Add unit tests and integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
…ws#30358) ### Issue # (if applicable) Closes aws#30353 . ### Reason for this change At the moment, L2 Construct does not support a custom auto scaling configuration for the AppRunner Service. ### Description of changes * Add `AutoScalingConfiguration` Class * Add `autoScalingConfiguration` property to the `Service` Class ### Description of how you validated changes Add unit tests and integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
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. |
Issue # (if applicable)
Closes #30353 .
Reason for this change
At the moment, L2 Construct does not support a custom auto scaling configuration for the AppRunner Service.
Description of changes
AutoScalingConfigurationClassautoScalingConfigurationproperty to theServiceClassDescription of how you validated changes
Add unit tests and integ tests.
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license