-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat(apprunner): add AutoScalingConfiguration for AppRunner Service #30358
Conversation
1e25c91
to
0a5114b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍 Left some comments for minor adjustments
packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts
Outdated
Show resolved
Hide resolved
packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts
Outdated
Show resolved
Hide resolved
/** | ||
* Properties of the App Runner Auto Scaling Configuration. | ||
*/ | ||
export interface AutoScalingConfigurationProps { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we allow to specify tags
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense 👍 Thanks for clarifying!
packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts
Outdated
Show resolved
Hide resolved
super(scope, id, { | ||
physicalName: props.autoScalingConfigurationName, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add validation for the autoScalingConfigurationName
property (docs)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I added validation and unit tests.
packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Luca Pizzini <[email protected]>
…ration.ts Co-authored-by: Luca Pizzini <[email protected]>
…ration.ts Co-authored-by: Luca Pizzini <[email protected]>
Co-authored-by: Luca Pizzini <[email protected]>
…uration.test.ts Co-authored-by: Luca Pizzini <[email protected]>
…ration.ts Co-authored-by: Luca Pizzini <[email protected]>
@lpizzinidev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense 👍 Thanks for clarifying!
packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts
Outdated
Show resolved
Hide resolved
packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts
Outdated
Show resolved
Hide resolved
…ration.ts Co-authored-by: Luca Pizzini <[email protected]>
…ration.ts Co-authored-by: Luca Pizzini <[email protected]>
e5067dd
to
f46a7c7
Compare
@lpizzinidev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts
Outdated
Show resolved
Hide resolved
@pahud |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some minor suggested changes
* The revision of the Auto Scaling Configuration. | ||
* @attribute | ||
*/ | ||
readonly autoScalingConfigurationRevision: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we probably should make it string
. Just like ecs task definition.
public readonly revision: string; |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
validateAutoScalingConfiguration
to make the code more modular and easier to test. -
Used
typeof
to check if the properties are defined and a number as well as!cdk.Token.isUnresolved
because 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
isMaxConcurrencyDefined
check 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
typeof
checks 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.isUnresolved
check 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. Usingtypeof
handles all of these cases in a single, simple check. [1] -
Robustness: The
typeof
checks are more robust because they handle a wider range of cases. For example, if the property is set tonull
, thetypeof
check will still correctly identify it as not a number, whereas the
undefined
check would not catch this case. -
Performance: The
typeof
checks are generally faster than usingundefined
andcdk.Token.isUnresolved
, as they are simpler operations. This can be especially beneficial in performance-critical code. -
Consistency: Using
typeof
to 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I like your suggested your approach.
I've created a private method including autoScalingConfigurationName validation.
packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts
Outdated
Show resolved
Hide resolved
packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts
Show resolved
Hide resolved
…uration.test.ts Co-authored-by: Pahud Hsieh <[email protected]>
…uration.test.ts Co-authored-by: Pahud Hsieh <[email protected]>
@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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks 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). |
@Mergifyio update |
☑️ Nothing to do
|
@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
AutoScalingConfiguration
ClassautoScalingConfiguration
property to theService
ClassDescription 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