From 0a5114bc0f96fcdd7f3db2b7129372e23a0e5fff Mon Sep 17 00:00:00 2001 From: maz Date: Tue, 28 May 2024 12:26:19 +0900 Subject: [PATCH 01/19] feat: add autoscaling for apprunner --- .../@aws-cdk/aws-apprunner-alpha/README.md | 25 +- .../lib/auto-scaling-configuration.ts | 157 ++++++++++++ .../@aws-cdk/aws-apprunner-alpha/lib/index.ts | 1 + .../aws-apprunner-alpha/lib/service.ts | 16 +- .../test/auto-scaling-configuration.test.ts | 73 ++++++ ...efaultTestDeployAssert4AFB3BF1.assets.json | 19 ++ ...aultTestDeployAssert4AFB3BF1.template.json | 36 +++ .../cdk.out | 1 + ...ner-auto-scaling-configuration.assets.json | 19 ++ ...r-auto-scaling-configuration.template.json | 117 +++++++++ .../integ.json | 12 + .../manifest.json | 176 +++++++++++++ .../tree.json | 232 ++++++++++++++++++ ...nteg.service-auto-scaling-configuration.ts | 33 +++ .../aws-apprunner-alpha/test/service.test.ts | 29 ++- 15 files changed, 942 insertions(+), 4 deletions(-) create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets.json create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.template.json create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.assets.json create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.template.json create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ.json create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/tree.json create mode 100644 packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.ts diff --git a/packages/@aws-cdk/aws-apprunner-alpha/README.md b/packages/@aws-cdk/aws-apprunner-alpha/README.md index 1cc9363c8c139..a3552427311fd 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/README.md +++ b/packages/@aws-cdk/aws-apprunner-alpha/README.md @@ -32,7 +32,7 @@ The `Service` construct allows you to create AWS App Runner services with `ECR P - `Source.fromEcr()` - To define the source repository from `ECR`. - `Source.fromEcrPublic()` - To define the source repository from `ECR Public`. - `Source.fromGitHub()` - To define the source repository from the `Github repository`. -- `Source.fromAsset()` - To define the source from local asset directory. +- `Source.fromAsset()` - To define the source from local asset directory. The `Service` construct implements `IGrantable`. @@ -154,6 +154,27 @@ when required. See [App Runner IAM Roles](https://docs.aws.amazon.com/apprunner/latest/dg/security_iam_service-with-iam.html#security_iam_service-with-iam-roles) for more details. +## Auto Scaling Configuration + +To associate an App Runner service with a custom Auto Scaling Configuration, define `autoScalingConfiguration` for the service. + +```ts +const autoScalingConfiguration = new apprunner.AutoScalingConfiguration(this, 'AutoScalingConfiguration', { + autoScalingConfigurationName: 'MyAutoScalingConfiguration', + maxConcurrency: 150, + maxSize: 20, + minSize: 5, +}); + +new apprunner.Service(this, 'DemoService', { + source: apprunner.Source.fromEcrPublic({ + imageConfiguration: { port: 8000 }, + imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', + }), + autoScalingConfiguration, +}); +``` + ## VPC Connector To associate an App Runner service with a custom VPC, define `vpcConnector` for the service. @@ -183,7 +204,7 @@ new apprunner.Service(this, 'Service', { ## Secrets Manager To include environment variables integrated with AWS Secrets Manager, use the `environmentSecrets` attribute. -You can use the `addSecret` method from the App Runner `Service` class to include secrets from outside the +You can use the `addSecret` method from the App Runner `Service` class to include secrets from outside the service definition. ```ts diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts new file mode 100644 index 0000000000000..ec4ea28fd0ff8 --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -0,0 +1,157 @@ +import * as cdk from 'aws-cdk-lib/core'; +import { Construct } from 'constructs'; +import { CfnAutoScalingConfiguration } from 'aws-cdk-lib/aws-apprunner'; + +/** + * Properties of the App Runner Auto Scaling Configuration. + */ +export interface AutoScalingConfigurationProps { + /** + * The name for the Auto Scaling Configuration. + */ + readonly autoScalingConfigurationName?: string; + + /** + * The maximum number of concurrent requests that an instance processes. + * If the number of concurrent requests exceeds this limit, App Runner scales the service up. + * + * Must be between 1 and 25. + * + * @default 100 + */ + readonly maxConcurrency?: number; + + /** + * The maximum number of instances that a service scales up to. + * At most maxSize instances actively serve traffic for your service. + * + * Must be between 1 and 25. + * + * @default 1 + */ + readonly maxSize?: number; + + /** + * The minimum number of instances that App Runner provisions for a service. + * The service always has at least minSize provisioned instances. + * + * @default 25 + */ + readonly minSize?: number; +} + +/** + * Attributes for the App Runner Auto Scaling Configuration. + */ +export interface AutoScalingConfigurationAttributes { + /** + * The name of the Auto Scaling Configuration. + */ + readonly autoScalingConfigurationName: string; + + /** + * The ARN of the Auto Scaling Configuration. + */ + readonly autoScalingConfigurationArn: string; + + /** + * The revision of the Auto Scaling Configuration. + */ + readonly autoScalingConfigurationRevision: number; +} + +/** + * Represents the App Runner Auto Scaling Configuration. + */ +export interface IAutoScalingConfiguration extends cdk.IResource { + /** + * The ARN of the Auto Scaling Configuration. + * @attribute + */ + readonly autoScalingConfigurationArn: string; + + /** + * The Name of the Auto Scaling Configuration. + * @attribute + */ + readonly autoScalingConfigurationName: string; + + /** + * The revision of the Auto Scaling Configuration. + * @attribute + */ + readonly autoScalingConfigurationRevision: number; +} + +/** + * The App Runner Auto Scaling Configuration. + * + * @resource AWS::AppRunner::AutoScalingConfiguration + */ +export class AutoScalingConfiguration extends cdk.Resource implements IAutoScalingConfiguration { + /** + * Import from Auto Scaling Configuration attributes. + */ + public static fromAutoScalingConfigurationAttributes(scope: Construct, id: string, + attrs: AutoScalingConfigurationAttributes): IAutoScalingConfiguration { + const autoScalingConfigurationArn = attrs.autoScalingConfigurationArn; + const autoScalingConfigurationName = attrs.autoScalingConfigurationName; + const autoScalingConfigurationRevision = attrs.autoScalingConfigurationRevision; + + class Import extends cdk.Resource { + public readonly autoScalingConfigurationArn = autoScalingConfigurationArn + public readonly autoScalingConfigurationName = autoScalingConfigurationName + public readonly autoScalingConfigurationRevision = autoScalingConfigurationRevision + } + + return new Import(scope, id); + } + + /** + * The ARN of the Auto Scaling Configuration. + * @attribute + */ + readonly autoScalingConfigurationArn: string; + + /** + * The name of the Auto Scaling Configuration. + * @attribute + */ + readonly autoScalingConfigurationName: string; + + /** + * The revision of the Auto Scaling Configuration. + * @attribute + */ + readonly autoScalingConfigurationRevision: number; + + public constructor(scope: Construct, id: string, props: AutoScalingConfigurationProps = {}) { + super(scope, id, { + physicalName: props.autoScalingConfigurationName, + }); + + if (props.minSize !== undefined && (props.minSize < 1 || props.minSize > 25)) { + throw new Error(`minSize must be between 1 and 25, got ${props.minSize}`); + } + if (props.maxSize !== undefined && (props.maxSize < 1 || props.maxSize > 25)) { + throw new Error(`maxSize must be between 1 and 25, got ${props.maxSize}`); + } + if (props.minSize !== undefined && props.maxSize !== undefined && !(props.minSize < props.maxSize)) { + throw new Error('maxSize must be greater than minSize'); + } + if (props.maxConcurrency !== undefined && (props.maxConcurrency < 1 || props.maxConcurrency > 200)) { + throw new Error(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}`); + } + + const resource = new CfnAutoScalingConfiguration(this, 'Resource', { + autoScalingConfigurationName: props.autoScalingConfigurationName, + maxConcurrency: props.maxConcurrency, + maxSize: props.maxSize, + minSize: props.minSize, + }); + + this.autoScalingConfigurationArn = resource.attrAutoScalingConfigurationArn; + this.autoScalingConfigurationRevision = resource.attrAutoScalingConfigurationRevision; + this.autoScalingConfigurationName = resource.ref; + } +} diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/index.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/index.ts index e061d5b7ecf80..1908a112b1ae7 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/index.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/index.ts @@ -1,3 +1,4 @@ // AWS::AppRunner CloudFormation Resources: +export * from './auto-scaling-configuration'; export * from './service'; export * from './vpc-connector'; diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts index 9564bbae18cf3..122f5d9087714 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts @@ -8,6 +8,7 @@ import { Lazy } from 'aws-cdk-lib/core'; import { Construct } from 'constructs'; import { CfnService } from 'aws-cdk-lib/aws-apprunner'; import { IVpcConnector } from './vpc-connector'; +import { IAutoScalingConfiguration } from './auto-scaling-configuration'; /** * The image repository types @@ -79,7 +80,7 @@ export class Cpu { * * @param unit The unit of CPU. */ - private constructor(public readonly unit: string) {} + private constructor(public readonly unit: string) { } } /** @@ -655,6 +656,18 @@ export interface ServiceProps { */ readonly autoDeploymentsEnabled?: boolean; + /** + * Specifies an App Runner Auto Scaling Configuration. + * + * A default configuration is either the AWS recommended configuration, + * or the configuration you set as the default. + * + * @see https://docs.aws.amazon.com/apprunner/latest/dg/manage-autoscaling.html + * + * @default - use a default Auto Scaling Configuration. + */ + readonly autoScalingConfiguration?: IAutoScalingConfiguration; + /** * The number of CPU units reserved for each instance of your App Runner service. * @@ -1239,6 +1252,7 @@ export class Service extends cdk.Resource implements iam.IGrantable { this.renderCodeConfiguration(this.source.codeRepository!.codeConfiguration.configurationValues!) : undefined, }, + autoScalingConfigurationArn: this.props.autoScalingConfiguration?.autoScalingConfigurationArn, networkConfiguration: { egressConfiguration: { egressType: this.props.vpcConnector ? 'VPC' : 'DEFAULT', diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts new file mode 100644 index 0000000000000..2ce8ba6cb3f37 --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts @@ -0,0 +1,73 @@ +import { Match, Template } from 'aws-cdk-lib/assertions'; +import * as cdk from 'aws-cdk-lib'; +import { AutoScalingConfiguration } from '../lib'; + +let stack: cdk.Stack; +beforeEach(() => { + stack = new cdk.Stack(); +}); + +test('create an Auto scaling Configuration with all properties', () => { + // WHEN + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + autoScalingConfigurationName: 'MyAutoScalingConfiguration', + maxConcurrency: 150, + maxSize: 20, + minSize: 5, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::AutoScalingConfiguration', { + AutoScalingConfigurationName: 'MyAutoScalingConfiguration', + MaxConcurrency: 150, + MaxSize: 20, + MinSize: 5, + }); +}); + +test('create an Auto scaling Configuration without all properties', () => { + // WHEN + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::AutoScalingConfiguration', { + AutoScalingConfigurationName: Match.absent(), + MaxConcurrency: Match.absent(), + MaxSize: Match.absent(), + MinSize: Match.absent(), + }); +}); + +test.each([0, 26])('invalid minSize', (minSize: number) => { + expect(() => { + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + minSize, + }); + }).toThrow(`minSize must be between 1 and 25, got ${minSize}`); +}); + +test.each([0, 26])('invalid maxSize', (maxSize: number) => { + expect(() => { + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + maxSize, + }); + }).toThrow(`maxSize must be between 1 and 25, got ${maxSize}`); +}); + +test('minSize greater than maxSize', () => { + expect(() => { + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + minSize: 5, + maxSize: 3, + }); + }).toThrow('maxSize must be greater than minSize'); +}); + +test.each([0, 201])('invalid maxConcurrency', (maxConcurrency: number) => { + expect(() => { + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + maxConcurrency, + }); + }).toThrow(`maxConcurrency must be between 1 and 200, got ${maxConcurrency}`); +}); diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets.json b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets.json new file mode 100644 index 0000000000000..aceb9fdaf269b --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.template.json b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/cdk.out b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.assets.json b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.assets.json new file mode 100644 index 0000000000000..020d27008b0a8 --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "363fcfee21919c2d1b81b2bce2345af9e946429f13cfaa0093b8342d674b6ae0": { + "source": { + "path": "integ-apprunner-auto-scaling-configuration.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "363fcfee21919c2d1b81b2bce2345af9e946429f13cfaa0093b8342d674b6ae0.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.template.json b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.template.json new file mode 100644 index 0000000000000..de4478446df05 --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ-apprunner-auto-scaling-configuration.template.json @@ -0,0 +1,117 @@ +{ + "Resources": { + "AutoScalingConfigurationB226C248": { + "Type": "AWS::AppRunner::AutoScalingConfiguration", + "Properties": { + "MaxConcurrency": 150, + "MaxSize": 20, + "MinSize": 5 + } + }, + "ServiceInstanceRoleDFA90CEC": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "tasks.apprunner.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "ServiceDBC79909": { + "Type": "AWS::AppRunner::Service", + "Properties": { + "AutoScalingConfigurationArn": { + "Fn::GetAtt": [ + "AutoScalingConfigurationB226C248", + "AutoScalingConfigurationArn" + ] + }, + "InstanceConfiguration": { + "InstanceRoleArn": { + "Fn::GetAtt": [ + "ServiceInstanceRoleDFA90CEC", + "Arn" + ] + } + }, + "NetworkConfiguration": { + "EgressConfiguration": { + "EgressType": "DEFAULT" + } + }, + "ServiceName": "service", + "SourceConfiguration": { + "AuthenticationConfiguration": {}, + "AutoDeploymentsEnabled": false, + "ImageRepository": { + "ImageConfiguration": { + "Port": "8000" + }, + "ImageIdentifier": "public.ecr.aws/aws-containers/hello-app-runner:latest", + "ImageRepositoryType": "ECR_PUBLIC" + } + } + } + } + }, + "Outputs": { + "URL": { + "Value": { + "Fn::Join": [ + "", + [ + "https://", + { + "Fn::GetAtt": [ + "ServiceDBC79909", + "ServiceUrl" + ] + } + ] + ] + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ.json b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ.json new file mode 100644 index 0000000000000..8652a35fecd22 --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "AppRunnerAutoScalingConfiguration/DefaultTest": { + "stacks": [ + "integ-apprunner-auto-scaling-configuration" + ], + "assertionStack": "AppRunnerAutoScalingConfiguration/DefaultTest/DeployAssert", + "assertionStackName": "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/manifest.json b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/manifest.json new file mode 100644 index 0000000000000..d701cc4701158 --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/manifest.json @@ -0,0 +1,176 @@ +{ + "version": "36.0.0", + "artifacts": { + "integ-apprunner-auto-scaling-configuration.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "integ-apprunner-auto-scaling-configuration.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "integ-apprunner-auto-scaling-configuration": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "integ-apprunner-auto-scaling-configuration.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/363fcfee21919c2d1b81b2bce2345af9e946429f13cfaa0093b8342d674b6ae0.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "integ-apprunner-auto-scaling-configuration.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "integ-apprunner-auto-scaling-configuration.assets" + ], + "metadata": { + "/integ-apprunner-auto-scaling-configuration/AutoScalingConfiguration/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "AutoScalingConfigurationB226C248" + } + ], + "/integ-apprunner-auto-scaling-configuration/Service/InstanceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ServiceInstanceRoleDFA90CEC" + } + ], + "/integ-apprunner-auto-scaling-configuration/Service/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ServiceDBC79909" + } + ], + "/integ-apprunner-auto-scaling-configuration/URL": [ + { + "type": "aws:cdk:logicalId", + "data": "URL" + } + ], + "/integ-apprunner-auto-scaling-configuration/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/integ-apprunner-auto-scaling-configuration/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ], + "ASG1CF037CF4": [ + { + "type": "aws:cdk:logicalId", + "data": "ASG1CF037CF4", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ], + "ASG2409F2238": [ + { + "type": "aws:cdk:logicalId", + "data": "ASG2409F2238", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ], + "ASG3D4584027": [ + { + "type": "aws:cdk:logicalId", + "data": "ASG3D4584027", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ], + "ASG4C27FF86D": [ + { + "type": "aws:cdk:logicalId", + "data": "ASG4C27FF86D", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ], + "ASG5B28C3BE5": [ + { + "type": "aws:cdk:logicalId", + "data": "ASG5B28C3BE5", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ] + }, + "displayName": "integ-apprunner-auto-scaling-configuration" + }, + "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "AppRunnerAutoScalingConfigurationDefaultTestDeployAssert4AFB3BF1.assets" + ], + "metadata": { + "/AppRunnerAutoScalingConfiguration/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/AppRunnerAutoScalingConfiguration/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "AppRunnerAutoScalingConfiguration/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/tree.json b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/tree.json new file mode 100644 index 0000000000000..c2d8604d2275d --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.js.snapshot/tree.json @@ -0,0 +1,232 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "integ-apprunner-auto-scaling-configuration": { + "id": "integ-apprunner-auto-scaling-configuration", + "path": "integ-apprunner-auto-scaling-configuration", + "children": { + "AutoScalingConfiguration": { + "id": "AutoScalingConfiguration", + "path": "integ-apprunner-auto-scaling-configuration/AutoScalingConfiguration", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-apprunner-auto-scaling-configuration/AutoScalingConfiguration/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppRunner::AutoScalingConfiguration", + "aws:cdk:cloudformation:props": { + "maxConcurrency": 150, + "maxSize": 20, + "minSize": 5 + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apprunner.CfnAutoScalingConfiguration", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apprunner-alpha.AutoScalingConfiguration", + "version": "0.0.0" + } + }, + "Service": { + "id": "Service", + "path": "integ-apprunner-auto-scaling-configuration/Service", + "children": { + "InstanceRole": { + "id": "InstanceRole", + "path": "integ-apprunner-auto-scaling-configuration/Service/InstanceRole", + "children": { + "ImportInstanceRole": { + "id": "ImportInstanceRole", + "path": "integ-apprunner-auto-scaling-configuration/Service/InstanceRole/ImportInstanceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "integ-apprunner-auto-scaling-configuration/Service/InstanceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "tasks.apprunner.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "integ-apprunner-auto-scaling-configuration/Service/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppRunner::Service", + "aws:cdk:cloudformation:props": { + "autoScalingConfigurationArn": { + "Fn::GetAtt": [ + "AutoScalingConfigurationB226C248", + "AutoScalingConfigurationArn" + ] + }, + "instanceConfiguration": { + "instanceRoleArn": { + "Fn::GetAtt": [ + "ServiceInstanceRoleDFA90CEC", + "Arn" + ] + } + }, + "networkConfiguration": { + "egressConfiguration": { + "egressType": "DEFAULT" + } + }, + "serviceName": "service", + "sourceConfiguration": { + "authenticationConfiguration": {}, + "autoDeploymentsEnabled": false, + "imageRepository": { + "imageConfiguration": { + "port": "8000" + }, + "imageIdentifier": "public.ecr.aws/aws-containers/hello-app-runner:latest", + "imageRepositoryType": "ECR_PUBLIC" + } + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apprunner.CfnService", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-apprunner-alpha.Service", + "version": "0.0.0" + } + }, + "URL": { + "id": "URL", + "path": "integ-apprunner-auto-scaling-configuration/URL", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnOutput", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "integ-apprunner-auto-scaling-configuration/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "integ-apprunner-auto-scaling-configuration/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "AppRunnerAutoScalingConfiguration": { + "id": "AppRunnerAutoScalingConfiguration", + "path": "AppRunnerAutoScalingConfiguration", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "AppRunnerAutoScalingConfiguration/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "AppRunnerAutoScalingConfiguration/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "AppRunnerAutoScalingConfiguration/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "AppRunnerAutoScalingConfiguration/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "AppRunnerAutoScalingConfiguration/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.ts new file mode 100644 index 0000000000000..7d89aea11dfc6 --- /dev/null +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-auto-scaling-configuration.ts @@ -0,0 +1,33 @@ +import * as cdk from 'aws-cdk-lib'; +import { Service, Source, AutoScalingConfiguration } from '../lib'; +import * as integ from '@aws-cdk/integ-tests-alpha'; + +const app = new cdk.App(); + +const stack = new cdk.Stack(app, 'integ-apprunner-auto-scaling-configuration'); + +const autoScalingConfiguration = new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + maxConcurrency: 150, + maxSize: 20, + minSize: 5, +}); + +const service = new Service(stack, 'Service', { + serviceName: 'service', + source: Source.fromEcrPublic({ + imageConfiguration: { + port: 8000, + }, + imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', + }), + autoDeploymentsEnabled: false, + autoScalingConfiguration, +}); + +new cdk.CfnOutput(stack, 'URL', { value: `https://${service.serviceUrl}` }); + +new integ.IntegTest(app, 'AppRunnerAutoScalingConfiguration', { + testCases: [stack], +}); + +app.synth(); diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts index d4ef80d552bd1..76f04c0f949eb 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts @@ -1579,4 +1579,31 @@ test('timeout must be less than or equal to 20 in healthCheck', () => { }), }); }).toThrow('timeout must be between 1 and 20 seconds, got 21'); -}); \ No newline at end of file +}); + +test('create a service with an AutoScalingConfiguration)', () => { + // GIVEN + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'demo-stack'); + const autoScalingConfiguration = new apprunner.AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + autoScalingConfigurationName: 'MyAutoScalingConfiguration', + }); + + // WHEN + new apprunner.Service(stack, 'DemoService', { + source: apprunner.Source.fromEcrPublic({ + imageConfiguration: { port: 8000 }, + imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', + }), + autoScalingConfiguration, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::AutoScalingConfiguration', { + AutoScalingConfigurationName: 'MyAutoScalingConfiguration', + }); + + Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::Service', { + AutoScalingConfigurationArn: stack.resolve(autoScalingConfiguration.autoScalingConfigurationArn), + }); +}); From bdb85cbe8049371ee9e847eb0bef0f2d2235bf28 Mon Sep 17 00:00:00 2001 From: maz Date: Tue, 28 May 2024 17:49:28 +0900 Subject: [PATCH 02/19] fix: add @default --- .../aws-apprunner-alpha/lib/auto-scaling-configuration.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index ec4ea28fd0ff8..4e83f6e1f6670 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -8,6 +8,8 @@ import { CfnAutoScalingConfiguration } from 'aws-cdk-lib/aws-apprunner'; export interface AutoScalingConfigurationProps { /** * The name for the Auto Scaling Configuration. + * + * @default - a name generated by CloudFormation */ readonly autoScalingConfigurationName?: string; From 4ada3f693850d3f5db87238decf930a2dd4a002f Mon Sep 17 00:00:00 2001 From: maz Date: Wed, 29 May 2024 08:59:44 +0900 Subject: [PATCH 03/19] feat: add fromArn method --- .../lib/auto-scaling-configuration.ts | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 4e83f6e1f6670..1e403e5e6a2cb 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -92,7 +92,7 @@ export interface IAutoScalingConfiguration extends cdk.IResource { */ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScalingConfiguration { /** - * Import from Auto Scaling Configuration attributes. + * Imports an App Runner Auto Scaling Configuration from attributes */ public static fromAutoScalingConfigurationAttributes(scope: Construct, id: string, attrs: AutoScalingConfigurationAttributes): IAutoScalingConfiguration { @@ -109,6 +109,28 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali return new Import(scope, id); } + /** + * Imports an App Runner Auto Scaling Configuration from its ARN + */ + public static fromArn(scope: Construct, id: string, autoScalingConfigurationArn: string): IAutoScalingConfiguration { + const arn = cdk.Stack.of(scope).splitArn(autoScalingConfigurationArn, cdk.ArnFormat.SLASH_RESOURCE_NAME); + + const resourceParts = arn.resourceName?.split('/'); + + if (!resourceParts || resourceParts.length < 3) { + throw new Error(`Unexpected ARN format: ${autoScalingConfigurationArn}`); + } + + const autoScalingConfigurationName = resourceParts[0]; + const autoScalingConfigurationRevision = parseInt(resourceParts[1]); + + return AutoScalingConfiguration.fromAutoScalingConfigurationAttributes(scope, id, { + autoScalingConfigurationArn, + autoScalingConfigurationName, + autoScalingConfigurationRevision, + }); + } + /** * The ARN of the Auto Scaling Configuration. * @attribute From 61a1b0087d7ff969381694d85bc07b13fa43e659 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 11:02:16 +0900 Subject: [PATCH 04/19] Update packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts Co-authored-by: Luca Pizzini --- packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts index b852509c6952a..e5ffea23356a4 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts @@ -1606,7 +1606,7 @@ test('create a service with a customer managed key)', () => { }); }); -test('create a service with an AutoScalingConfiguration)', () => { +test('create a service with an AutoScalingConfiguration', () => { // GIVEN const app = new cdk.App(); const stack = new cdk.Stack(app, 'demo-stack'); From 84a3cce977cf02d0ff454c077145001310853fb0 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 11:02:59 +0900 Subject: [PATCH 05/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts Co-authored-by: Luca Pizzini --- .../aws-apprunner-alpha/lib/auto-scaling-configuration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 1e403e5e6a2cb..f762dd9d3d5ce 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -17,7 +17,7 @@ export interface AutoScalingConfigurationProps { * The maximum number of concurrent requests that an instance processes. * If the number of concurrent requests exceeds this limit, App Runner scales the service up. * - * Must be between 1 and 25. + * Must be between 1 and 200. * * @default 100 */ From 55501a7f8a7809426769c2163ca89439b4497538 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 11:03:16 +0900 Subject: [PATCH 06/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts Co-authored-by: Luca Pizzini --- .../aws-apprunner-alpha/lib/auto-scaling-configuration.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index f762dd9d3d5ce..6efd188f43398 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -37,7 +37,10 @@ export interface AutoScalingConfigurationProps { * The minimum number of instances that App Runner provisions for a service. * The service always has at least minSize provisioned instances. * - * @default 25 + * + * Must be between 1 and 25. + * + * @default 1 */ readonly minSize?: number; } From e999f834f842ae5ee15da8219c675a6d3793ab14 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 11:04:37 +0900 Subject: [PATCH 07/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts Co-authored-by: Luca Pizzini --- packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts index d8fac94b5b4f1..c3c8a03da4bc4 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts @@ -665,7 +665,7 @@ export interface ServiceProps { * * @see https://docs.aws.amazon.com/apprunner/latest/dg/manage-autoscaling.html * - * @default - use a default Auto Scaling Configuration. + * @default - the latest revision of a default auto scaling configuration is used. */ readonly autoScalingConfiguration?: IAutoScalingConfiguration; From 59a6f3293524f8fe168a20f1368ecd32dfc419d1 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 11:04:48 +0900 Subject: [PATCH 08/19] Update packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts Co-authored-by: Luca Pizzini --- .../test/auto-scaling-configuration.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts index 2ce8ba6cb3f37..02bcfbe137a50 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts @@ -27,8 +27,7 @@ test('create an Auto scaling Configuration with all properties', () => { test('create an Auto scaling Configuration without all properties', () => { // WHEN - new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { - }); + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration'); // THEN Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::AutoScalingConfiguration', { From 215e5a9df7103e1ef25753d9129b9e5d100e5dfd Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 11:10:49 +0900 Subject: [PATCH 09/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts Co-authored-by: Luca Pizzini --- .../aws-apprunner-alpha/lib/auto-scaling-configuration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 6efd188f43398..7301356127b6f 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -157,7 +157,7 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali physicalName: props.autoScalingConfigurationName, }); - if (props.minSize !== undefined && (props.minSize < 1 || props.minSize > 25)) { + if (props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize) && (props.minSize < 1 || props.minSize > 25)) { throw new Error(`minSize must be between 1 and 25, got ${props.minSize}`); } if (props.maxSize !== undefined && (props.maxSize < 1 || props.maxSize > 25)) { From b161ed773839c63906f0041f2c651f972ce8f4e1 Mon Sep 17 00:00:00 2001 From: maz Date: Sun, 2 Jun 2024 11:27:02 +0900 Subject: [PATCH 10/19] fix: incorporate review comments --- .../lib/auto-scaling-configuration.ts | 10 ++++++ .../test/auto-scaling-configuration.test.ts | 31 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 7301356127b6f..001f5ab0890ec 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -157,6 +157,16 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali physicalName: props.autoScalingConfigurationName, }); + if (props.autoScalingConfigurationName !== undefined) { + if (props.autoScalingConfigurationName.length < 4 || props.autoScalingConfigurationName.length > 32) { + throw new Error(`autoScalingConfigurationName must be between 4 and 32 characters long, but it has ${props.autoScalingConfigurationName.length} characters.`); + } + + if (!/^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$/.test(props.autoScalingConfigurationName)) { + throw new Error(`autoScalingConfigurationName ${props.autoScalingConfigurationName} must start with a letter or number, and can contain only letters, numbers, hyphens, and underscores.`); + } + } + if (props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize) && (props.minSize < 1 || props.minSize > 25)) { throw new Error(`minSize must be between 1 and 25, got ${props.minSize}`); } diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts index 02bcfbe137a50..eee51ef336a3e 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts @@ -7,10 +7,13 @@ beforeEach(() => { stack = new cdk.Stack(); }); -test('create an Auto scaling Configuration with all properties', () => { +test.each([ + ['MyAutoScalingConfiguration'], + ['my-autoscaling-configuration_1'], +])('create an Auto scaling Configuration with all properties (name: %s)', (autoScalingConfigurationName: string) => { // WHEN new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { - autoScalingConfigurationName: 'MyAutoScalingConfiguration', + autoScalingConfigurationName, maxConcurrency: 150, maxSize: 20, minSize: 5, @@ -18,7 +21,7 @@ test('create an Auto scaling Configuration with all properties', () => { // THEN Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::AutoScalingConfiguration', { - AutoScalingConfigurationName: 'MyAutoScalingConfiguration', + AutoScalingConfigurationName: autoScalingConfigurationName, MaxConcurrency: 150, MaxSize: 20, MinSize: 5, @@ -70,3 +73,25 @@ test.each([0, 201])('invalid maxConcurrency', (maxConcurrency: number) => { }); }).toThrow(`maxConcurrency must be between 1 and 200, got ${maxConcurrency}`); }); + +test.each([ + ['tes'], + ['test-autoscaling-configuration-name-over-limitation'], +])('autoScalingConfigurationName over length limitation (name: %s)', (autoScalingConfigurationName: string) => { + expect(() => { + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + autoScalingConfigurationName, + }); + }).toThrow(`autoScalingConfigurationName must be between 4 and 32 characters long, but it has ${autoScalingConfigurationName.length} characters.`); +}); + +test.each([ + ['-test'], + ['test-?'], +])('invalid autoScalingConfigurationName (name: %s)', (autoScalingConfigurationName: string) => { + expect(() => { + new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + autoScalingConfigurationName, + }); + }).toThrow(`autoScalingConfigurationName ${autoScalingConfigurationName} must start with a letter or number, and can contain only letters, numbers, hyphens, and underscores.`); +}); From 50fc0d32c3314d884bfe4697e99e179b49e04f62 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 23:30:29 +0900 Subject: [PATCH 11/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts Co-authored-by: Luca Pizzini --- .../lib/auto-scaling-configuration.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 001f5ab0890ec..8ad7d6dec89e8 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -157,14 +157,12 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali physicalName: props.autoScalingConfigurationName, }); - if (props.autoScalingConfigurationName !== undefined) { - if (props.autoScalingConfigurationName.length < 4 || props.autoScalingConfigurationName.length > 32) { - throw new Error(`autoScalingConfigurationName must be between 4 and 32 characters long, but it has ${props.autoScalingConfigurationName.length} characters.`); - } - - if (!/^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$/.test(props.autoScalingConfigurationName)) { - throw new Error(`autoScalingConfigurationName ${props.autoScalingConfigurationName} must start with a letter or number, and can contain only letters, numbers, hyphens, and underscores.`); - } + if ( + props.autoScalingConfigurationName !== undefined && + !cdk.Token.isUnresolved(props.autoScalingConfigurationName) && + !/^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$/.test(props.autoScalingConfigurationName) + ) { + throw new Error(`autoScalingConfigurationName must match the ^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$ pattern, got ${props.autoScalingConfigurationName}`); } if (props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize) && (props.minSize < 1 || props.minSize > 25)) { From 0cac270f7bee1aeaec4a4f9601384ebb14165e36 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Sun, 2 Jun 2024 23:31:56 +0900 Subject: [PATCH 12/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts Co-authored-by: Luca Pizzini --- .../lib/auto-scaling-configuration.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 8ad7d6dec89e8..a94584bb9dd2e 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -165,16 +165,22 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali throw new Error(`autoScalingConfigurationName must match the ^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$ pattern, got ${props.autoScalingConfigurationName}`); } - if (props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize) && (props.minSize < 1 || props.minSize > 25)) { + const isMinSizeDefined = props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize) + const isMaxSizeDefined = props.maxSize !== undefined && !cdk.Token.isUnresolved(props.maxSize) + if (isMinSizeDefined && (props.minSize < 1 || props.minSize > 25)) { throw new Error(`minSize must be between 1 and 25, got ${props.minSize}`); } - if (props.maxSize !== undefined && (props.maxSize < 1 || props.maxSize > 25)) { + if (isMaxSizeDefined && (props.maxSize < 1 || props.maxSize > 25)) { throw new Error(`maxSize must be between 1 and 25, got ${props.maxSize}`); } - if (props.minSize !== undefined && props.maxSize !== undefined && !(props.minSize < props.maxSize)) { + if (isMinSizeDefined && isMaxSizeDefined && !(props.minSize < props.maxSize)) { throw new Error('maxSize must be greater than minSize'); } - if (props.maxConcurrency !== undefined && (props.maxConcurrency < 1 || props.maxConcurrency > 200)) { + if ( + props.maxConcurrency !== undefined && + !cdk.Token.isUnresolved(props.maxConcurrency) && + (props.maxConcurrency < 1 || props.maxConcurrency > 200) + ) { throw new Error(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}`); } From f46a7c70ffa7842cd0c965566c589caf58fef8bf Mon Sep 17 00:00:00 2001 From: maz Date: Mon, 3 Jun 2024 00:02:12 +0900 Subject: [PATCH 13/19] fix: unit tests --- .../lib/auto-scaling-configuration.ts | 8 ++++---- .../test/auto-scaling-configuration.test.ts | 11 +---------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index a94584bb9dd2e..0a270f98fc888 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -158,15 +158,15 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali }); if ( - props.autoScalingConfigurationName !== undefined && + props.autoScalingConfigurationName !== undefined && !cdk.Token.isUnresolved(props.autoScalingConfigurationName) && !/^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$/.test(props.autoScalingConfigurationName) ) { throw new Error(`autoScalingConfigurationName must match the ^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$ pattern, got ${props.autoScalingConfigurationName}`); } - const isMinSizeDefined = props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize) - const isMaxSizeDefined = props.maxSize !== undefined && !cdk.Token.isUnresolved(props.maxSize) + const isMinSizeDefined = props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize); + const isMaxSizeDefined = props.maxSize !== undefined && !cdk.Token.isUnresolved(props.maxSize); if (isMinSizeDefined && (props.minSize < 1 || props.minSize > 25)) { throw new Error(`minSize must be between 1 and 25, got ${props.minSize}`); } @@ -177,7 +177,7 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali throw new Error('maxSize must be greater than minSize'); } if ( - props.maxConcurrency !== undefined && + props.maxConcurrency !== undefined && !cdk.Token.isUnresolved(props.maxConcurrency) && (props.maxConcurrency < 1 || props.maxConcurrency > 200) ) { diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts index eee51ef336a3e..57a8a7a6dbb0d 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts @@ -77,15 +77,6 @@ test.each([0, 201])('invalid maxConcurrency', (maxConcurrency: number) => { test.each([ ['tes'], ['test-autoscaling-configuration-name-over-limitation'], -])('autoScalingConfigurationName over length limitation (name: %s)', (autoScalingConfigurationName: string) => { - expect(() => { - new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { - autoScalingConfigurationName, - }); - }).toThrow(`autoScalingConfigurationName must be between 4 and 32 characters long, but it has ${autoScalingConfigurationName.length} characters.`); -}); - -test.each([ ['-test'], ['test-?'], ])('invalid autoScalingConfigurationName (name: %s)', (autoScalingConfigurationName: string) => { @@ -93,5 +84,5 @@ test.each([ new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { autoScalingConfigurationName, }); - }).toThrow(`autoScalingConfigurationName ${autoScalingConfigurationName} must start with a letter or number, and can contain only letters, numbers, hyphens, and underscores.`); + }).toThrow(`autoScalingConfigurationName must match the ^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$ pattern, got ${autoScalingConfigurationName}`); }); From 9bb0dcb14b8f65e2c4bcd7832e103fd0cffcd043 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Tue, 4 Jun 2024 08:28:14 +0900 Subject: [PATCH 14/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts Co-authored-by: Luca Pizzini --- .../aws-apprunner-alpha/lib/auto-scaling-configuration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 0a270f98fc888..2abb3ae4d1ecd 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -29,7 +29,7 @@ export interface AutoScalingConfigurationProps { * * Must be between 1 and 25. * - * @default 1 + * @default 25 */ readonly maxSize?: number; From 2c74142af55ad40fa69e2a171632df546bf24efc Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Wed, 19 Jun 2024 10:14:01 +0900 Subject: [PATCH 15/19] Update packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts Co-authored-by: Pahud Hsieh --- .../aws-apprunner-alpha/lib/auto-scaling-configuration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 2abb3ae4d1ecd..acc86a341f4b5 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -103,7 +103,7 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali const autoScalingConfigurationName = attrs.autoScalingConfigurationName; const autoScalingConfigurationRevision = attrs.autoScalingConfigurationRevision; - class Import extends cdk.Resource { + class Import extends cdk.Resource implements IAutoScalingConfiguration { public readonly autoScalingConfigurationArn = autoScalingConfigurationArn public readonly autoScalingConfigurationName = autoScalingConfigurationName public readonly autoScalingConfigurationRevision = autoScalingConfigurationRevision From 0aef49e9660d71650940ad4311e601be7efdeeec Mon Sep 17 00:00:00 2001 From: maz Date: Wed, 19 Jun 2024 10:45:06 +0900 Subject: [PATCH 16/19] fix: incorporate review comments --- .../lib/auto-scaling-configuration.ts | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index acc86a341f4b5..36bd44f862dfb 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -54,11 +54,6 @@ export interface AutoScalingConfigurationAttributes { */ readonly autoScalingConfigurationName: string; - /** - * The ARN of the Auto Scaling Configuration. - */ - readonly autoScalingConfigurationArn: string; - /** * The revision of the Auto Scaling Configuration. */ @@ -99,14 +94,17 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali */ public static fromAutoScalingConfigurationAttributes(scope: Construct, id: string, attrs: AutoScalingConfigurationAttributes): IAutoScalingConfiguration { - const autoScalingConfigurationArn = attrs.autoScalingConfigurationArn; const autoScalingConfigurationName = attrs.autoScalingConfigurationName; const autoScalingConfigurationRevision = attrs.autoScalingConfigurationRevision; - class Import extends cdk.Resource implements IAutoScalingConfiguration { - public readonly autoScalingConfigurationArn = autoScalingConfigurationArn - public readonly autoScalingConfigurationName = autoScalingConfigurationName - public readonly autoScalingConfigurationRevision = autoScalingConfigurationRevision + class Import extends cdk.Resource implements IAutoScalingConfiguration { + public readonly autoScalingConfigurationName = autoScalingConfigurationName; + public readonly autoScalingConfigurationRevision = autoScalingConfigurationRevision; + public readonly autoScalingConfigurationArn = cdk.Stack.of(this).formatArn({ + resource: 'autoscalingconfiguration', + service: 'apprunner', + resourceName: `${attrs.autoScalingConfigurationName}/${attrs.autoScalingConfigurationRevision}`, + }); } return new Import(scope, id); @@ -116,22 +114,22 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali * Imports an App Runner Auto Scaling Configuration from its ARN */ public static fromArn(scope: Construct, id: string, autoScalingConfigurationArn: string): IAutoScalingConfiguration { - const arn = cdk.Stack.of(scope).splitArn(autoScalingConfigurationArn, cdk.ArnFormat.SLASH_RESOURCE_NAME); - - const resourceParts = arn.resourceName?.split('/'); + const resourceParts = cdk.Fn.split('/', autoScalingConfigurationArn); if (!resourceParts || resourceParts.length < 3) { throw new Error(`Unexpected ARN format: ${autoScalingConfigurationArn}`); } - const autoScalingConfigurationName = resourceParts[0]; - const autoScalingConfigurationRevision = parseInt(resourceParts[1]); + const autoScalingConfigurationName = cdk.Fn.select(0, resourceParts); + const autoScalingConfigurationRevision = Number(cdk.Fn.select(1, resourceParts)); - return AutoScalingConfiguration.fromAutoScalingConfigurationAttributes(scope, id, { - autoScalingConfigurationArn, - autoScalingConfigurationName, - autoScalingConfigurationRevision, - }); + class Import extends cdk.Resource implements IAutoScalingConfiguration { + public readonly autoScalingConfigurationName = autoScalingConfigurationName; + public readonly autoScalingConfigurationRevision = autoScalingConfigurationRevision; + public readonly autoScalingConfigurationArn = autoScalingConfigurationArn; + } + + return new Import(scope, id); } /** From 361ab636ae73770830d2676ac240bb102f06bcee Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Thu, 20 Jun 2024 08:39:50 +0900 Subject: [PATCH 17/19] Update packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts Co-authored-by: Pahud Hsieh --- .../test/auto-scaling-configuration.test.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts index 57a8a7a6dbb0d..3fc9b9131a285 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts @@ -86,3 +86,25 @@ test.each([ }); }).toThrow(`autoScalingConfigurationName must match the ^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$ pattern, got ${autoScalingConfigurationName}`); }); + +test('create an Auto scaling Configuration with tags', () => { + // WHEN + const autoScalingConfiguration = new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { + autoScalingConfigurationName: 'my-autoscaling-config', + maxConcurrency: 150, + maxSize: 20, + minSize: 5, + }); + + new cdk.Tags.of(autoScalingConfiguration).add('Environment', 'production'); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::AutoScalingConfiguration', { + Tags: [ + { + Key: 'Environment', + Value: 'production', + }, + ], + }); +}); From b8c7d61df214fd92e4787eb5fcd7ea995791fe35 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Thu, 20 Jun 2024 08:40:00 +0900 Subject: [PATCH 18/19] Update packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts Co-authored-by: Pahud Hsieh --- .../aws-apprunner-alpha/test/auto-scaling-configuration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts index 3fc9b9131a285..96843dde2acd8 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts @@ -41,7 +41,7 @@ test('create an Auto scaling Configuration without all properties', () => { }); }); -test.each([0, 26])('invalid minSize', (minSize: number) => { +test.each([-1, 0, 26])('invalid minSize', (minSize: number) => { expect(() => { new AutoScalingConfiguration(stack, 'AutoScalingConfiguration', { minSize, From 2633a0d00aaf494eecc5d9fefbab2cc7e9f8eabf Mon Sep 17 00:00:00 2001 From: maz Date: Thu, 20 Jun 2024 08:51:22 +0900 Subject: [PATCH 19/19] fix: incorporate review comments --- .../lib/auto-scaling-configuration.ts | 42 +++++++++++-------- .../test/auto-scaling-configuration.test.ts | 2 +- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts index 36bd44f862dfb..1d3c1ce943fca 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/auto-scaling-configuration.ts @@ -155,6 +155,21 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali physicalName: props.autoScalingConfigurationName, }); + this.validateAutoScalingConfiguration(props); + + const resource = new CfnAutoScalingConfiguration(this, 'Resource', { + autoScalingConfigurationName: props.autoScalingConfigurationName, + maxConcurrency: props.maxConcurrency, + maxSize: props.maxSize, + minSize: props.minSize, + }); + + this.autoScalingConfigurationArn = resource.attrAutoScalingConfigurationArn; + this.autoScalingConfigurationRevision = resource.attrAutoScalingConfigurationRevision; + this.autoScalingConfigurationName = resource.ref; + } + + private validateAutoScalingConfiguration(props: AutoScalingConfigurationProps) { if ( props.autoScalingConfigurationName !== undefined && !cdk.Token.isUnresolved(props.autoScalingConfigurationName) && @@ -163,34 +178,25 @@ export class AutoScalingConfiguration extends cdk.Resource implements IAutoScali throw new Error(`autoScalingConfigurationName must match the ^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$ pattern, got ${props.autoScalingConfigurationName}`); } - const isMinSizeDefined = props.minSize !== undefined && !cdk.Token.isUnresolved(props.minSize); - const isMaxSizeDefined = props.maxSize !== undefined && !cdk.Token.isUnresolved(props.maxSize); + 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 ( - props.maxConcurrency !== undefined && - !cdk.Token.isUnresolved(props.maxConcurrency) && - (props.maxConcurrency < 1 || props.maxConcurrency > 200) - ) { + + if (isMaxConcurrencyDefined && (props.maxConcurrency < 1 || props.maxConcurrency > 200)) { throw new Error(`maxConcurrency must be between 1 and 200, got ${props.maxConcurrency}`); } - - const resource = new CfnAutoScalingConfiguration(this, 'Resource', { - autoScalingConfigurationName: props.autoScalingConfigurationName, - maxConcurrency: props.maxConcurrency, - maxSize: props.maxSize, - minSize: props.minSize, - }); - - this.autoScalingConfigurationArn = resource.attrAutoScalingConfigurationArn; - this.autoScalingConfigurationRevision = resource.attrAutoScalingConfigurationRevision; - this.autoScalingConfigurationName = resource.ref; } + } diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts index 96843dde2acd8..c563e4a5c7745 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/auto-scaling-configuration.test.ts @@ -96,7 +96,7 @@ test('create an Auto scaling Configuration with tags', () => { minSize: 5, }); - new cdk.Tags.of(autoScalingConfiguration).add('Environment', 'production'); + cdk.Tags.of(autoScalingConfiguration).add('Environment', 'production'); // THEN Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::AutoScalingConfiguration', {